简体   繁体   中英

Service Fabric wcf communication between microservices

I'm trying to make a simple communication between two microservices. So far as a receiver

protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
    {

        return new[]
        {
            new ServiceInstanceListener((context) =>
                new WcfCommunicationListener<ITest>(
                    wcfServiceObject: new Test(),
                    serviceContext: context,
                    endpointResourceName: "ProgramTestEndpoint",
                    listenerBinding: WcfUtility.CreateTcpListenerBinding()
                ),
                name: "ProgramTestListener"
            )
        };
    }

    public class Test : ITest
{
    public async Task<int> ReturnsInt()
    {
        return 2;
    }
}

 [ServiceContract]
public interface ITest
{
    [OperationContract]
    Task<int> ReturnsInt();


}

And I did add to service manifest the endpoint.

<Endpoint Name ="ProgramTestEndpoint"/>

The microservice that wants to communicate has this code

 protected override async Task RunAsync(CancellationToken cancellationToken)
    {
        // TODO: Replace the following sample code with your own logic 
        //       or remove this RunAsync override if it's not needed in your service.

        await Task.Delay(5000);

        CloudClient<ITest> transactionCoordinator = new CloudClient<ITest>
       (
           serviceUri: new Uri($"{Context.CodePackageActivationContext.ApplicationName}/MyStateless"),
           partitionKey: new ServicePartitionKey(0),
           clientBinding: WcfUtility.CreateTcpClientBinding(),
           listenerName: "MyStateless"
       );


        int iterations = await transactionCoordinator.InvokeWithRetryAsync(client => client.Channel.ReturnsInt());
        ServiceEventSource.Current.ServiceMessage(this.Context, "Test-{0}", ++iterations);
        while (true)
        {
            cancellationToken.ThrowIfCancellationRequested();

            ServiceEventSource.Current.ServiceMessage(this.Context, "Working-{0}", ++iterations);

            await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
        }
    }

This is my first project in service fabric, I'm not sure what I'm doing wrong, but with this code the application can't receive the return value of the ReturnsInt task.

When creating a connection to a stateless service, you should use the ServicePartitionKey.Singleton partition key. In some cases you you don't need to specify one at all, for example when using ServiceProxyFactory to create a connection to a stateless service.

Using new ServicePartitionKey(0) was causing the client to try and connect to an endpoint that didn't exist.

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