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

时间:2018-01-03 00:52:08

标签: c# dependency-injection .net-core

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

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服务中利用相同的模式,这不会受益于内置的ASP.NET核心功能来处理DI 。服务启动时,我执行以下代码:

// 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服务时,它会在尝试获取notificationService时抛出异常:

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

例外是:

  

System.InvalidOperationException:&#39;无法解析类型的服务   &#39; Microsoft.Extensions.Options.IOptions`1 [Rpr.Listings.DataAccess.DataAccessConfiguration]&#39;   在尝试激活时   &#39; Rpr.Listings.DataAccess.NotificationDataAccess&#39;&#39;

我希望我的&#34; services.Configure&#34;代码可以解决这个问题,但是没有。显然我需要在我的Startup.cs中注册IOptions,但是我不知道如何这样做。这通常是ASP.NET MVC开箱即用的吗?是&#34; services.AddMvc();&#34;通常正确注册此绑定?我可以调用它,但是需要将大量的ASP.NET MVC包导入到我的Windows服务中,我不愿意这样做。

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

2 个答案:

答案 0 :(得分:21)

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

services.AddOptions();

一旦我添加了,就正确注册了IOptions绑定!

答案 1 :(得分:1)

万一它对任何人都有帮助,我在控制台应用程序中遇到了这个问题,这是由创建服务提供商引起的

IServiceProvider serviceProvider = services.BuildServiceProvider();

在我注册配置之前

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