我正在跟随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();
}
在
MyStatelessService
语句后用以下命令更改Program.cs
的try
部分:ServiceRuntime.RegisterServiceAsync("MyStatelessServiceType",
context => new CalculatorService(context)).GetAwaiter().GetResult();
ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(CalculatorService).Name);
您引用的是
CalculatorService
,而不是MyStatelessService
。关于c# - FabricException:分区的主实例或无状态实例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44061267/