简体   繁体   English

无法在非ASP.NET核心应用程序中解析类型IOptions [DataAccessConfiguration]的服务

[英]Unable to resolve service for type IOptions[DataAccessConfiguration] in non-ASP.NET Core app

All of our business services were previously set up to use Dependency Injection with IOptions because they were being consumed by ASP.NET Core apps, like so: 我们以前的所有业务服务都设置为使用依赖注入和IOptions,因为它们被ASP.NET核心应用程序使用,如下所示:

NotificationDataAccess.cs: NotificationDataAccess.cs:

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

NotificationBusinessService.cs: NotificationBusinessService.cs:

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

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

Now I'm left with the unenviable task of trying to figure out how to leverage the same pattern from a windows service, which doesn't benefit from the built-in ASP.NET Core features for handling DI. 现在我离开了一个不值得羡慕的任务,试图找出如何从Windows服务中利用相同的模式,这不会受益于内置的ASP.NET核心功能来处理DI。 When the service starts up, I execute the following code: 服务启动时,我执行以下代码:

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

And here is the Startup.cs code, which is supposed to configure the services: 这是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));
        }
    }

Unfortunately, when I run the windows service it throws an exception when trying to get the notificationService: 不幸的是,当我运行Windows服务时,它会在尝试获取notificationService时抛出异常:

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

The exception is: 例外是:

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'.' System.InvalidOperationException:'尝试激活'Rpr.Listings.DataAccess.NotificationDataAccess'时,无法解析类型'Microsoft.Extensions.Options.IOptions`1 [Rpr.Listings.DataAccess.DataAccessConfiguration]'的服务。

I was hoping my "services.Configure" code would resolve this, but alas no. 我希望我的“services.Configure”代码可以解决这个问题,但是没有。 Clearly I need to register IOptions in my Startup.cs, however I have no idea how to do so. 显然我需要在我的Startup.cs中注册IOptions,但是我不知道如何这样做。 Is this something that usually happens out of the box with ASP.NET MVC? 这通常是ASP.NET MVC开箱即用的吗? Does "services.AddMvc();" 是“services.AddMvc();” normally register this binding correctly? 通常正确注册此绑定? I can call that, but would need to import a ton of ASP.NET MVC packages into my windows service, which I'm reluctant to do. 我可以调用它,但是需要将大量的ASP.NET MVC包导入到我的Windows服务中,我不愿意这样做。

Please let me know how to register the IOptions binding correctly, thanks! 请告诉我如何正确注册IOptions绑定,谢谢!

It turns out that all I was missing was: 事实证明,我所缺少的是:

services.AddOptions();

Once I added that, the IOptions binding was registered correctly! 一旦我添加了,就正确注册了IOptions绑定!

In case it helps anyone, I had this issue in a console app and it was caused by creating the service provider 如果它可以帮助任何人,我在控制台应用程序中遇到此问题,这是由创建服务提供程序引起的

IServiceProvider serviceProvider = services.BuildServiceProvider();

before I'd registered my config 在我注册我的配置之前

services.Configure<DataAccessConfiguration>(Configuration.GetSection("Data"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.Net Core 2无法解析类型服务 - ASP.Net Core 2 Unable to resolve service for type Net Core 2控制台应用程序-DI&#39;无法解析类型的服务...&#39; - Net Core 2 Console App - DI 'Unable to resolve service for type…' InvalidOperationException:无法解析.Net Core类型的服务 - InvalidOperationException: Unable to resolve service for type .Net Core 将非 ASP.NET 应用程序与 IIS 集成? - Integrating a non-ASP.NET application with IIS? ASP.NET 核心依赖注入错误 - 尝试激活“服务”时无法解析“存储库”类型的服务 - ASP.NET Core Dependency Injection error - Unable to resolve service for type “Repository” while attempting to activate “Service” ASP.NET Core 2 无法解析 Microsoft EntityFrameworkCore DbContext 类型的服务 - ASP.NET Core 2 Unable to resolve service for type Microsoft EntityFrameworkCore DbContext ASP.NET Core 错误:尝试激活时无法解析类型服务 - ASP.NET Core error: Unable to resolve service for type while attempting to activate 依赖注入错误:尝试激活时无法解析类型的服务,而 class 已注册 - Dependency Injection error: Unable to resolve service for type while attempting to activate, while class is registered ASP.NET 核心 - System.InvalidOperationException:尝试激活时无法解析服务类型 - ASP.NET Core - System.InvalidOperationException: Unable to resolve service for type while attempting to activate ASP.Net Core - 尝试激活 *Controller 时无法解析 *Context 类型的服务 - ASP.Net Core - Unable to resolve service for type *Context while attempting to activate *Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM