【问题标题】:Setting up TCP on Azure Service Fabric在 Azure Service Fabric 上设置 TCP
【发布时间】:2016-12-08 21:43:50
【问题描述】:

我需要设置一个有状态的 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();
    }
}

我还有一个StatefulService,叫做ListenerService

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方法中做点什么

【问题讨论】:

    标签: c# .net azure tcp azure-service-fabric


    【解决方案1】:

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

    【讨论】:

    • 对,我假设在配置中指定一个端点会自动打开一个监听器。
    • 不行,你必须打开你自己的监听器。端点配置做了两件事:1)如果您指定一个端口,它将允许它通过防火墙,2)如果您使用基于 http.sys 的侦听器,它将 ACL 侦听器的 URL。
    猜你喜欢
    • 1970-01-01
    • 2018-11-06
    • 2019-11-30
    • 2016-07-20
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 2017-04-09
    相关资源
    最近更新 更多