我正在尝试在两个微服务之间进行简单的通信。就接收者而言
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/