我正在跟随tutorial中的[this book]


我收到以下异常:
c# - FabricException:分区的主实例或无状态实例-LMLPHP

毫无疑问,我的节点运行良好:c# - FabricException:分区的主实例或无状态实例-LMLPHP

这是我的无状态服务的设置方式:

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);
            }
        }
    }


我部署并获取此异常的方式:c# - FabricException:分区的主实例或无状态实例-LMLPHP

我究竟做错了什么?如何使客户端连接到群集?

可以查看整个解决方案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.cstry部分:

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/

10-10 21:40