我正在尝试让两个微服务之间进行简单的通信。目前作为接收方。
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任务的返回值。
partitionKey: ServicePartitionKey.Singleton
会有什么不同吗? - Oliver