带有 ASP.Net Core 2.0 IOptionsSnapshot 注入的简单注入器

Simple Injector w/ ASP.Net Core 2.0 IOptionsSnapshot injection

我正在尝试错误 #429 中的建议,但遇到了他在那里报告的相同错误,但从未提供堆栈跟踪。我还阅读了您关于不使用 IOptions 和相关 class 的指南,并且直到最近才开始使用。我们正处于真正需要 IOptionsSnapshot 的时刻,因为我们是 Azure 中的 运行 东西,并且需要能够在我们达到限制并重新启动服务时即时翻转选项 on/off不是一个选项,因为由于我们需要一些第三方组件,最初需要超过 5 分钟才能启动。

这是我们的设置:

接口 ISearchSettings -> class SearchSettings
(基本上这里的所有属性,除了 1 个布尔值,如果需要我们可以单例。一个布尔值有点告诉我们是使用内部搜索还是 azure 搜索)

应用程序启动时,我收到以下错误:

System.InvalidOperationException: The configuration is invalid. Creating the instance for type IOptionsSnapshot<SearchSettings> failed. The registered delegate for type IOptionsSnapshot<SearchSettings> threw an exception. Unable to request service 'IOptionsSnapshot<SearchSettings> from ASP.NET Core request services. Please make sure this method is called within the context of an active HTTP request.

在配置服务中:

services.AddOptions();  
services.Configure<ISearchSettings>(
    this.Configuration.GetSection("AzureSearchSettings"));  
services.Configure<SearchSettings>(
    this.Configuration.GetSection("AzureSearchSettings"));  
// The next line was added trying some other suggestions from similar
// errors. It didn't resolve the issue  
services.AddScoped(
    cfg => cfg.GetService<IOptionsSnapshot<SearchSettings>>().Value);  
...  
services.AddMvc();  
...  
IntegrateSimpleInjector();  

在 IntegrateSimpleInjector 中:

this.container.Options.DefaultScopedLifestyle =
    new AsyncScopedLifestyle();

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IControllerActivator>(
    new SimpleInjectorControllerActivator(this.container));
services.AddSingleton<IViewComponentActivator>(
    new SimpleInjectorViewComponentActivator(this.container));

services.EnableSimpleInjectorCrossWiring(this.container);
services.UseSimpleInjectorAspNetRequestScoping(this.container);

在 InitializeContainer 中:

// I have tried both Lifestyle Transient and Scoped
this.container.Register<IOptionsSnapshot<SearchSettings>>(
    () => app.GetRequestService<IOptionsSnapshot<SearchSettings>>(),
    Lifestyle.Transient);
...
this.container.AutoCrossWireAspNetComponents(app);

堆栈跟踪:

at SimpleInjector.SimpleInjectorAspNetCoreIntegrationExtensions.GetRequestServiceProvider(IApplicationBuilder builder, Type serviceType)
at SimpleInjector.SimpleInjectorAspNetCoreIntegrationExtensions.GetRequestService[T](IApplicationBuilder builder)
at QuotingService.Startup.<>c__DisplayClass9_0.<InitializeContainer>b__0() in E:\Repos\QuotingService\QuotingService\Startup.cs:line 299
at lambda_method(Closure )
at SimpleInjector.InstanceProducer.BuildAndReplaceInstanceCreatorAndCreateFirstInstance()
at SimpleInjector.InstanceProducer.GetInstance()
--- End of inner exception stack trace ---
at SimpleInjector.InstanceProducer.GetInstance()
at SimpleInjector.InstanceProducer.VerifyInstanceCreation()
--- End of inner exception stack trace ---
at SimpleInjector.InstanceProducer.VerifyInstanceCreation()
at SimpleInjector.Container.VerifyInstanceCreation(InstanceProducer[] producersToVerify)
at SimpleInjector.Container.VerifyInternal(Boolean suppressLifestyleMismatchVerification)
at SimpleInjector.Container.Verify()
at QuotingService.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime) in E:\Repos\QuotingService\QuotingService\Startup.cs:line 229
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

关于需要更改哪些内容才能使其正常工作有什么想法吗?
感谢您提供的出色产品和提供的任何帮助。

您应该防止在简单注入器注册的委托中调用 GetRequestService,因为这种调用需要存在活动的 HTTP 请求,而在应用程序启动期间该请求将不可用。

相反,依靠 AutoCrossWireAspNetComponents 从 ASP.NET 核心获得 IOptionsSnapshot<SearchSettings>

但是,要使其正常工作,您需要调用 services.Configure<SearchSettings>

这是一个有效的配置:

public class Startup
{
    private Container container = new Container();

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json");
        this.Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        // ASP.NET default stuff here
        services.AddMvc();

        this.IntegrateSimpleInjector(services);

        services.Configure<SearchSettings>(
            Configuration.GetSection("SearchSettings"));
    }

    private void IntegrateSimpleInjector(IServiceCollection services)
    {
        container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

        services.AddSingleton<IControllerActivator>(
            new SimpleInjectorControllerActivator(container));

        services.EnableSimpleInjectorCrossWiring(container);
        services.UseSimpleInjectorAspNetRequestScoping(container);
    }

    public void Configure(IApplicationBuilder app)
    {
        container.AutoCrossWireAspNetComponents(app);
        container.RegisterMvcControllers(app);

        container.Verify();

        // ASP.NET default stuff here
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

有了这个配置,你可以在任何地方注入IOptionsSnapshot<T>。例如在你的 HomeController:

public class HomeController : Controller
{
    private readonly IOptionsSnapshot<SearchSettings> snapshot;

    public HomeController(
        IOptionsSnapshot<SearchSettings> snapshot)
    {
        this.snapshot = snapshot;
    }
}