我正在尝试基于Azure Service Fabric的StatefulService构建tcp/ip服务器。问题出在本地运行时。Visual Studio无法安装该服务,并出现以下错误:
fabric:/TCPTestServerFabric/TCPTestService is not ready, 1 partitions remaining.
字符串
和Service Fabric Explorer:
Partition is below target replica or instance count.
fabric:/TCPTestServerFabric/TCPTestService 1 1 61eee22a-c5d4-46e8-ab57-8d41009abc3d
N/P InBuild _Node_0 133444395888851522
For more information see: <a href='https://aka.ms/sfhealth' target='_blank'>https://aka.ms/sfhealth</a>
型
但是.无论如何安装它并调试它,就像没有问题一样。从TCP客户端应用程序,我有些如何能够连接到服务器的IP和端口号,并发送消息到服务器,并得到响应回来。我已经尝试删除TCPTestService中OpenAsync方法中的代码,这也没有帮助。上面的错误仍然发生。
我检查了我的磁盘空间,这不是一个问题,我有大约250 GB的可用空间。还在服务结构属性中使用了这些选项,也没有帮助。
x1c 0d1x的数据
有人知道我做错了什么吗?先谢谢你。
internal sealed class TCPTestService : StatefulService
{
public TCPTestService(StatefulServiceContext context) : base(context) { }
protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
{
return new[] { new ServiceReplicaListener(context =>
new TCPTestServer(context, StateManager), "ServiceEndpoint") };
}
protected override async Task RunAsync(CancellationToken cancellationToken)
{
//cancellationToken.WaitHandle.WaitOne();
}
}
public class TCPTestServer : ICommunicationListener
{
private readonly StatefulServiceContext _context;
private readonly IReliableStateManager _stateManager;
public TCPTestServer(StatefulServiceContext context, IReliableStateManager stateManager)
{
_context = context;
_stateManager = stateManager;
}
public void Abort()
{
ServiceEventSource.Current.ServiceMessage(_context, $"Request aborted");
}
public Task CloseAsync(CancellationToken cancellationToken)
{
ServiceEventSource.Current.ServiceMessage(_context, $"Server closed");
return Task.FromCanceled(cancellationToken);
}
public async Task<string> OpenAsync(CancellationToken cancellationToken)
{
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
try
{
EndpointResourceDescription serviceEndpoint = _context.CodePackageActivationContext.GetEndpoint("ServiceEndpoint");
int port = serviceEndpoint.Port;
IPEndPoint ipEndPoint = new( IPAddress.Parse( "127.0.0.1" ), port);
using Socket listener = new(
ipEndPoint.AddressFamily,
SocketType.Stream,
ProtocolType.Tcp);
listener.Bind(ipEndPoint);
listener.Listen();
ServiceEventSource.Current.ServiceMessage(_context, $"Server started");
Socket handler = await listener.AcceptAsync();
ServiceEventSource.Current.ServiceMessage(_context, $"Incoming connection: {handler.AddressFamily}");
while (true)
{
//cancellationToken.ThrowIfCancellationRequested();
// Receive message.
var buffer = new byte[1_024];
var received = await handler.ReceiveAsync(buffer, SocketFlags.None);
var response = Encoding.UTF8.GetString(buffer, 0, received);
if (!string.IsNullOrWhiteSpace(response))
{
ServiceEventSource.Current.ServiceMessage(_context, $"Incoming message: {response}");
var ackMessage = "<ACK/>";
var echoBytes = Encoding.UTF8.GetBytes(ackMessage);
int bytesSent = await handler.SendAsync(echoBytes, 0);
ServiceEventSource.Current.ServiceMessage(_context, $"Sent message: {ackMessage}");
}
else
{
ServiceEventSource.Current.ServiceMessage(_context, $"Disconnected");
break; //Disconnected
}
}
}
catch (Exception ex)
{
ServiceEventSource.Current.ServiceMessage(_context, $"Error: {ex.Message}");
}
}
}
}
的字符串
1条答案
按热度按时间1rhkuytd1#
只是回答我自己的问题,以防万一你有一天会需要它。摆脱ICommunicationServer实现,并将OpenAsync方法的内容复制到Stateful服务实现。此外,你需要检查侦听器是否已经创建。