【问题标题】:Service Fabric service with WCF communication deployment使用 WCF 通信部署的 Service Fabric 服务
【发布时间】:2018-03-13 11:34:58
【问题描述】:

这是一个带有 WCF 通信的简单 Service Fabric 无状态服务及其客户端 - 一个控制台应用。它在本地集群上运行良好,客户端从服务中获得响应。但是如果我将它部署在云中,我不知道如何与服务通信。我应该怎么做才能从控制台应用程序访问它?

带有 WCF 通信的 SF 无状态服务:

合同:

 [ServiceContract]
    public interface IPresidentialService
    {
        [OperationContract]
        Task<string> GetInfo();
    }

服务:

internal sealed class PresidentialService : StatelessService, IPresidentialService
{
    public PresidentialService(StatelessServiceContext context) : base(context)
    {
    }

    public Task<string> GetInfo() => Task.FromResult($"Node {Context.NodeContext.NodeName} operating");

    protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
    {
        return new[]
        {
            new ServiceInstanceListener(context =>
                new WcfCommunicationListener<IPresidentialService>(wcfServiceObject: this, serviceContext: context,
                    endpointResourceName: "WcfServiceEndpoint",
                    listenerBinding: WcfUtility.CreateTcpListenerBinding()))
        };
    }
}

}

客户端控制台应用:

WCF 客户端:

public class PresidentialServiceClient : ServicePartitionClient<WcfCommunicationClient<IPresidentialService>>
    {
        public PresidentialServiceClient(
            ICommunicationClientFactory<WcfCommunicationClient<IPresidentialService>> communicationClientFactory,
            Uri serviceUri, ServicePartitionKey partitionKey = null,
            TargetReplicaSelector targetReplicaSelector = TargetReplicaSelector.Default, string listenerName = null,
            OperationRetrySettings retrySettings = null) : base(communicationClientFactory, serviceUri, partitionKey,
            targetReplicaSelector, listenerName, retrySettings)
        {
        }

        public Task<string> GetInfo() => InvokeWithRetryAsync(client => client.Channel.GetInfo());
    }

客户端应用:

private static void Main(string[] args)
        {
            var binding = WcfUtility.CreateTcpClientBinding();
            var partitionResolver = ServicePartitionResolver.GetDefault();
            var wcfClientFactory =
                new WcfCommunicationClientFactory<IPresidentialService>(binding,
                    servicePartitionResolver: partitionResolver);
            var serviceUri = new Uri("fabric:/Application5/PresidentialService");
            var client = new PresidentialServiceClient(wcfClientFactory, serviceUri, ServicePartitionKey.Singleton);
            do
            {
                Console.WriteLine(client.GetInfo().Result);
                Console.ReadKey();
            } while (true);
        }

添加到ServiceManifest.xml:

<Endpoints>
      <Endpoint Name="WcfServiceEndpoint" />
</Endpoints>

更新

已更改ServicePartitionResolver:

var partitionResolver = new ServicePartitionResolver("sfapp.westeurope.cloudapp.azure.com:19000");

还是不行。

更新

为 TCP 端口 777 添加了负载均衡器规则。

【问题讨论】:

    标签: c# wcf deployment cloud azure-service-fabric


    【解决方案1】:

    服务在云端运行时,不能使用默认解析器。

    默认的 ServicePartitionResolver 假定客户端是 与服务在同一个集群中运行。如果不是这样, 创建一个ServicePartitionResolver对象并传入集群 连接端点。

    试试something点赞

    ServicePartitionResolver resolver = new ServicePartitionResolver("mycluster.cloudapp.azure.com:19000");
    

    https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-reliable-services-communication-wcf

    【讨论】:

    • 请举例说明如何使用另一个解析器。
    • 您使用的是安全集群吗?如果是这样,您需要提供凭据。
    • 不,集群不安全。
    • 服务在哪个端口运行,你在负载均衡器中打开了吗?
    • 是的,TCP 端口 777。我是否应该像这样更改ServiceManifest.xls 中的端点:&lt;Endpoint Name="WcfServiceEndpoint" Protocol="tcp" Port="777" /&gt;
    猜你喜欢
    • 2016-08-30
    • 2020-10-24
    • 2018-12-13
    • 2016-12-07
    • 2017-10-30
    • 2019-06-21
    • 2018-04-27
    • 2021-03-01
    • 1970-01-01
    相关资源
    最近更新 更多