服务布局中微服务之间的WCF通信

4

我正在尝试让两个微服务之间进行简单的通信。目前作为接收方。

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


}

我在服务清单中添加了端点。

<Endpoint Name ="ProgramTestEndpoint"/>

想要通信的微服务有以下代码:
 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);
        }
    }

这是我在Service Fabric中的第一个项目,我不确定我做错了什么,但是使用这段代码,应用程序无法接收ReturnsInt任务的返回值。

你会收到什么?如果有的话,会返回什么状态码和错误信息? - ColinM
我尝试进行调试,似乎它无法连接到其他微服务,看起来它陷入了一个无限循环中,试图调用该方法。 - Anđela Krstić
1
使用 partitionKey: ServicePartitionKey.Singleton 会有什么不同吗? - Oliver
@AnđelaKrstić 看来你猜对了! - Oliver
1个回答

1
当创建到无状态服务的连接时,应该使用ServicePartitionKey.Singleton分区键。在某些情况下,您根本不需要指定分区键,例如使用ServiceProxyFactory创建到无状态服务的连接。
使用new ServicePartitionKey(0)会导致客户端尝试连接到不存在的端点。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接