gpt4 book ai didi

c# - 在 Azure Service Fabric 上设置 TCP

转载 作者:可可西里 更新时间:2023-11-01 02:33:57 28 4
gpt4 key购买 nike

我需要设置一个有状态 Service Fabric 应用来监听 TCP 请求,然后将消息弹出到可靠队列中。

有很多关于 HTTP 和 WCF 端点的示例,但我找不到任何简单的 TCP 示例。

在我的 ServiceManifest.xml 中,我有这个

 <Endpoints>
<!-- This endpoint is used by the communication listener to obtain the port on which to
listen. Please note that if your service is partitioned, this port is shared with
replicas of different partitions that are placed in your code. -->
<Endpoint Name="ServiceEndpoint" />

<!-- This endpoint is used by the replicator for replicating the state of your service.
This endpoint is configured through a ReplicatorSettings config section in the Settings.xml
file under the ConfigPackage. -->
<Endpoint Name="ReplicatorEndpoint" />
<Endpoint Name="tcpEndpoint" Protocol="tcp" Port="10100"/>
</Endpoints>

我有一个实现 ICommunicationListener 的监听器,名为 TcpCommunicationListener

 public class TcpCommunicationListener : ICommunicationListener
{
private readonly ServiceEventSource eventSource;
private readonly ServiceContext serviceContext;
private readonly string endpointName;
private string listeningAddress;
private string hostAddress;


public TcpCommunicationListener(ServiceContext serviceContext, ServiceEventSource eventSource, string endpointName)
{

if (serviceContext == null)
{
throw new ArgumentNullException(nameof(serviceContext));
}

if (endpointName == null)
{
throw new ArgumentNullException(nameof(endpointName));
}

if (eventSource == null)
{
throw new ArgumentNullException(nameof(eventSource));
}

this.serviceContext = serviceContext;
this.endpointName = endpointName;
this.eventSource = eventSource;
}

public Task<string> OpenAsync(CancellationToken cancellationToken)
{
var serviceEndpoint = this.serviceContext.CodePackageActivationContext.GetEndpoint(this.endpointName);
var protocol = serviceEndpoint.Protocol;
int port = serviceEndpoint.Port;


//StatefulServiceContext statefulServiceContext = this.serviceContext as StatefulServiceContext;

this.hostAddress = FabricRuntime.GetNodeContext().IPAddressOrFQDN;

this.listeningAddress = string.Format(
CultureInfo.InvariantCulture,
"{0}://{1}:{2}",
protocol,
hostAddress,
port
);

try
{
this.eventSource.Message("Starting tcp listener " + this.listeningAddress);

return Task.FromResult(this.hostAddress);
}
catch (Exception ex)
{
this.eventSource.Message("Tcp Listener failed to open endpoint {0}. {1}", this.endpointName, ex.ToString());

throw;
}
}

public Task CloseAsync(CancellationToken cancellationToken)
{
throw new NotImplementedException();
}

public void Abort()
{
throw new NotImplementedException();
}
}

我还有一个名为 ListenerServiceStatefulService

internal sealed class ListenerService : StatefulService
{

public ListenerService(StatefulServiceContext context)
: base(context)
{

}


protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
{
var endpoints = Context.CodePackageActivationContext.GetEndpoints()
.Where(endpoint => endpoint.Protocol == EndpointProtocol.Tcp)
.Select(endpoint => endpoint.Name);

return endpoints.Select(endpoint => new ServiceReplicaListener(
serviceContext => new TcpCommunicationListener(serviceContext, ServiceEventSource.Current, endpoint), endpoint));
}


protected override async Task RunAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

//do i spin up a TcpListener here?
}
}

所以我的问题是如何获取正在接收的消息?

我是否必须在 ListenerServiceRunAsync 方法中创建 TcpListener?如果是这种情况,那么在 ServiceManifest.xml 中指定端点有什么意义?

或者

我需要在 TcpCommunicationListener 的 OpenAsync 方法中执行某些操作吗?

最佳答案

现在,您的通信监听器代码仅在调用 OpenAsync 时发布 Uri。您还需要实际开始监听该端点。例如,您可以打开 Socket当时。您还可以使用WCFNetTcpBinding .

关于c# - 在 Azure Service Fabric 上设置 TCP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38739024/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com