gpt4 book ai didi

c# - 无法解析非 ASP.NET Core 应用程序中类型 IOptions [DataAccessConfiguration] 的服务

转载 作者:太空狗 更新时间:2023-10-29 17:31:57 25 4
gpt4 key购买 nike

我们所有的业务服务之前都设置为使用带 IOptions 的依赖注入(inject),因为它们被 ASP.NET Core 应用程序使用,如下所示:

NotificationDataAccess.cs:

public class NotificationDataAccess : BaseDataAccess, INotificationDac<Notification>
{
public NotificationDataAccess(IOptions<DataAccessConfiguration> options, IClaimsAccessor claimsAccessor) :
base(options, claimsAccessor)
{
}
}

NotificationBusinessService.cs:

public class NotificationBusinessServices : INotificationServices<Notification>
{
private readonly INotificationDac<Notification> _notificationDataAccess;

public NotificationBusinessServices(
INotificationDac<Notification> notifcationDataAccess)
{
_notificationDataAccess = notifcationDataAccess;
}
}

现在,我面临着一项艰巨的任务,即试图弄清楚如何从 Windows 服务中利用相同的模式,而这并没有从用于处理 DI 的内置 ASP.NET Core 功能中获益。当服务启动时,我执行以下代码:

// Set up configuration, services, and logging.
IServiceCollection services = new ServiceCollection();
var startup = new Startup();
startup.ConfigureServices(services);
IServiceProvider serviceProvider = services.BuildServiceProvider();

var configuration = serviceProvider.GetService<IConfigurationRoot>();
var notificationService = serviceProvider.GetService<INotificationServices<Notification>>();// TODO: This errors!
processor = new Processor(configuration, notificationService);

这里是 Startup.cs 代码,应该配置服务:

public class Startup
{
IConfigurationRoot Configuration { get; }

public Startup()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Path.Combine(AppContext.BaseDirectory))
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}

public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IConfigurationRoot>(Configuration);
//services.AddMvc();

// Add application services.
Listings.Business.Configuration.Instance = new BusinessLayerConfiguration();
services.Configure<DataAccessConfiguration>(options => Configuration.GetSection("Data").Bind(options));
services.AddScoped(typeof(INotificationDac<Notification>), typeof(NotificationDataAccess));
services.AddScoped(typeof(INotificationServices<Notification>), typeof(NotificationBusinessServices));
}
}

不幸的是,当我运行 Windows 服务时,它在尝试获取通知服务时抛出异常:

var notificationService = serviceProvider.GetService<INotificationServices<Notification>>();

异常(exception)情况是:

System.InvalidOperationException: 'Unable to resolve service for type 'Microsoft.Extensions.Options.IOptions`1[Rpr.Listings.DataAccess.DataAccessConfiguration]' while attempting to activate 'Rpr.Listings.DataAccess.NotificationDataAccess'.'

我希望我的“services.Configure”代码能够解决这个问题,但遗憾的是没有。显然,我需要在我的 Startup.cs 中注册 IOptions,但我不知道该怎么做。这是 ASP.NET MVC 经常发生的事情吗? “services.AddMvc();”通常正确注册此绑定(bind)?我可以调用它,但需要将大量 ASP.NET MVC 包导入到我的 Windows 服务中,我不愿意这样做。

请告诉我如何正确注册 IOptions 绑定(bind),谢谢!

最佳答案

事实证明,我所缺少的只是:

services.AddOptions();

一旦我添加了它,IOptions 绑定(bind)就被正确注册了!

关于c# - 无法解析非 ASP.NET Core 应用程序中类型 IOptions [DataAccessConfiguration] 的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48069829/

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