gpt4 book ai didi

asp.net-core - 具有 Asp.net Core 依赖注入(inject)的 Service Fabric 有状态服务

转载 作者:行者123 更新时间:2023-12-03 04:46:41 25 4
gpt4 key购买 nike

我在无状态服务中正确使用了 Asp.net core DI,因为它基本上是一个带有 Controller 的普通 WebApi 应用程序。

我不知道如何在有状态服务中使用依赖注入(inject)。这是有状态服务的默认构造函数:

 public MyService(StatefulServiceContext context): base(context)
{
}

并在 Program.cs 中被调用

ServiceRuntime.RegisterServiceAsync("MyStatefulType",context => new MyService(context)).GetAwaiter().GetResult();

我想在有状态服务中使用类似的东西:

  private readonly IHelpers _storageHelpers;
public MyService(IHelpers storageHelpers)
{
_storageHelpers = storageHelpers;
}

我已经在有状态服务的配置部分中注册了它,但是如果我尝试使用上面的代码,则会出现错误:

StatefulService 不包含采用 0 个参数的构造函数

如何让它发挥作用?

最佳答案

该错误与 StatefulService 的构造函数有关,它至少需要一个 ServiceContext 参数。您的代码仅提供 Storagehelper。

这将是使其工作的最简单方法:

服务:

private readonly IHelpers _storageHelpers;
public MyService(StatefulServiceContext context, IHelpers storageHelpers)
: base(context)
{
_storageHelpers = storageHelpers;
}

计划:

ServiceRuntime.RegisterServiceAsync("MyStatefulType",context => new MyService(context, new StorageHelper())).GetAwaiter().GetResult();

在“程序”中,您还可以使用 IOC 容器来获取存储帮助程序实例。

关于asp.net-core - 具有 Asp.net Core 依赖注入(inject)的 Service Fabric 有状态服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40957336/

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