编程语言
首页 > 编程语言> > c#-在Azure Service Fabric上设置TCP

c#-在Azure Service Fabric上设置TCP

作者:互联网

我需要设置一个有状态的服务结构应用程序,该应用程序侦听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();
    }
}

我还有一个名为ListenerService的StatefulService

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?
    }
}

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

我必须在ListenerService的RunAsync方法中创建TcpListener吗?如果是这种情况,那么在ServiceManifest.xml中指定终结点有什么意义?

要么

我需要在TcpCommunicationListener的OpenAsync方法中做些什么吗?

解决方法:

现在,您的通信侦听器代码仅在调用OpenAsync时发布Uri.您还需要真正开始在该端点上侦听.因此,例如,您当时可以打开Socket.
您也可以将WCFNetTcpBinding一起使用.

标签:azure,tcp,azure-service-fabric,c,net
来源: https://codeday.me/bug/20191118/2027392.html