gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:30:53 25 4
gpt4 key购买 nike

我正在关注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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com