我正在关注 tutorial来自[this book] :
以下是我的无状态服务的设置方式:
internal sealed class MyStatelessService : StatelessService
{
public MyStatelessService(StatelessServiceContext context)
: base(context)
{ }
/// <summary>
/// Optional override to create listeners (e.g., TCP, HTTP) for this service replica to handle client or user requests.
/// </summary>
/// <returns>A collection of listeners.</returns>
protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
{
return new ServiceInstanceListener[0];
}
/// <summary>
/// This is the main entry point for your service instance.
/// </summary>
/// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service instance.</param>
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.
long iterations = 0;
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
ServiceEventSource.Current.ServiceMessage(this.Context, "Working-{0}", ++iterations);
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
}
}
}
我做错了什么?如何让我的客户端连接到我的集群?
整个解决方案可以查看here.
最佳答案
有两件事:
您的
ICalculatorService
必须在另一个库项目中定义,以便在无状态服务库和客户端项目之间共享。所以:- 创建一个新的库项目
MyStatelessService.Interfaces
- 添加 NuGet 包:
Microsoft.ServiceFabric.Services.Remoting
- 在此库中定义您的
ICalculatorService
- 从其他两个项目中删除
ICalculatorService
- 将对
MyStatelessService.Interfaces
的引用添加到您的客户端应用程序和无状态服务库。 - 修复对
ICalculatorService
的引用 - 所有
ICalculatorService
都应从同一个库引用
- 创建一个新的库项目
您的客户将是:
using Microsoft.ServiceFabric.Services.Remoting.Client;
using MyStatelessService.Interfaces;
using System;
static void Main(string[] args)
{
var calculatorClient = ServiceProxy.Create<ICalculatorService>
(new Uri("fabric:/CalculatorService/MyStatelessService"));
var result = calculatorClient.Add(1, 2).Result;
Console.WriteLine(result);
Console.ReadKey();
}
在
try
语句之后更改MyStatelessService
的Program.cs
部分:ServiceRuntime.RegisterServiceAsync("MyStatelessServiceType", context => new CalculatorService(context)).GetAwaiter().GetResult(); ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(CalculatorService).Name);
您引用的是 MyStatelessService
,而不是 CalculatorService
。
关于c# - Fabric 异常 : The primary or stateless instance for the partition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44061267/