c# - Fabric 异常 : The primary or stateless instance for the partition

标签 c# .net azure visual-studio-2017 azure-service-fabric

我正在关注 tutorial来自[this book] : enter image description here

我收到以下异常: enter image description here

毫无疑问,我的节点运行良好:enter image description here

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

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

我部署并获得此异常的方式:enter image description here

我做错了什么?如何让我的客户端连接到我的集群?

整个解决方案可以查看here.

最佳答案

有两件事:

  1. 您的 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 语句之后更改 MyStatelessServiceProgram.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/

    相关文章:

    c# - 编译器为匿名方法生成了不正确的代码 [MS BUG FIXED]

    c# - ASP/VB/C# .NET 动态计算和引用

    wcf - Azure 服务总线中继响应缓慢

    c# - 如何使用存储过程从 SQL Server 获取数据

    c# - 是否可以从 Excel 电子表格 (VBA) 中获取 WinForms "Button"?

    c# - 当我实现使用 HandlerAttribute 的接口(interface)时,是否可以拦截它?

    c# - 是否可以在类中拥有 GUID 私有(private)属性以便在 GetHashCode 覆盖中使用它?

    .net - 解决方案中的 msbuild 和多个 Web 项目

    Azure API 管理无法使用 azure b2c 进行授权 : Signature Failed

    powershell - 如何使用具有集成安全性的 Powershell 连接 Azure Paas 数据库