简体   繁体   中英

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. 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:

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:

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 :

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

UPDATE

Changed ServicePartitionResolver :

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

Still not works.

UPDATE

Added a load balancer rule for TCP port 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. If that is not the case, create a ServicePartitionResolver object and pass in the cluster connection endpoints.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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