c# - 微服务之间的 Service Fabric wcf 通信

标签 c# wcf azure-service-fabric service-fabric-stateless

我正在尝试在两个微服务之间进行简单的通信。就接收者而言

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


}
我确实在服务 list 中添加了端点。
<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);
        }
    }
这是我在服务结构中的第一个项目,我不确定我做错了什么,但是使用此代码应用程序无法接收 ReturnsInt 任务的返回值。

最佳答案

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

关于c# - 微服务之间的 Service Fabric wcf 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62722604/

相关文章:

c# - C# 中的 Tessnet2 错误

c# - 小数位的 XAML StringFormat 语法

wcf - 如何处理 WCF REST 服务中的压缩请求

c# - 通过 RPC 与元数据进行服务结构通信

azure - 无法将管理客户端 key 添加到 Service Fabric 群集

c# - 一个事件监听器是否可以被限制为只有一个订阅者?

c# - Windows 身份基础系统.Xml.XmlException : Unexpected end of file

jquery - 在 JQuery 中解析 JSON 对象

wcf - SSL 错误 + WCF

azure - 对 Service Fabric 服务部署进行故障排除