简体   繁体   English

具有 WCF 通信部署的 Service Fabric 服务

[英]Service Fabric service with WCF communication deployment

Here is a simple Service Fabric stateless service with WCF communication and it's client - a console app.这是一个带有 WCF 通信的简单 Service Fabric 无状态服务,它是一个客户端 - 一个控制台应用程序。 It works well on the local cluster, client gets responce from the service.它在本地集群上运行良好,客户端从服务获得响应。 But I don't know how to communicate with a service if I deploy it in the cloud.但是如果我将它部署在云中,我不知道如何与服务通信。 What should I do to access it from console app?我应该怎么做才能从控制台应用程序访问它?

SF Stateless service with WCF communications: SF 无状态服务与 WCF 通信:

Contract:合同:

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

Service:服务:

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()))
        };
    }
}

} }

Client console app:客户端控制台应用程序:

WCF client: 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());
    }

Client App:客户端应用程序:

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);
        }

Added to ServiceManifest.xml :添加到ServiceManifest.xml

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

UPDATE更新

Changed ServicePartitionResolver :更改了ServicePartitionResolver

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

Still not works.还是不行。

UPDATE更新

Added a load balancer rule for TCP port 777.为 TCP 端口 777 添加了负载均衡器规则。

在此处输入图片说明

When the service is running in the cloud, you can't use the default resolver.当服务在云中运行时,您无法使用默认解析器。

The default ServicePartitionResolver assumes that the client is running in same cluster as the service.默认的 ServicePartitionResolver 假定客户端与服务在同一个集群中运行。 If that is not the case, create a ServicePartitionResolver object and pass in the cluster connection endpoints.如果不是这种情况,请创建一个 ServicePartitionResolver 对象并传入集群连接端点。

Try something like尝试类似的东西

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM