【发布时间】:2020-10-24 14:28:08
【问题描述】:
我正在尝试在两个微服务之间进行简单的通信。就当一个接收者
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);
}
}
这是我在服务结构中的第一个项目,我不确定自己做错了什么,但是使用此代码,应用程序无法接收 ReturnsInt 任务的返回值。
【问题讨论】:
-
你收到了什么?有什么状态码和错误?
-
我尝试调试它,它似乎无法连接到其他微服务,看起来它陷入了试图命中方法的无限循环。
-
使用
partitionKey: ServicePartitionKey.Singleton会有什么不同吗? -
@AnđelaKrstić 看来不错!
标签: c# wcf azure-service-fabric service-fabric-stateless