简体   繁体   English

与TinyIOC / NancyFX无关的IOptions

[英]IOptions not working with TinyIOC/NancyFX

I'm trying to implement the Options Pattern (as recommended here ) on a project with NancyFX / TinyIOC but it's not working. 我试图执行选项模式(推荐在这里 )与项目NancyFX / TinyIOC ,但它不工作。

I'm registering the Options on the Startup.cs.ConfigureServices method but when I try to inject the settings on my class TinyIoc throws: 我在Startup.cs.ConfigureServices方法上注册了选项,但是当我尝试在我的类上注入设置时, TinyIoc抛出:

Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: AppSettings. Nancy.TinyIoc.TinyIoCResolutionException:无法解析类型:AppSettings。

I think this is because the Options Pattern uses Microsoft.Extensions.DependencyInjection but Nancy uses TinyIoc as default so TinyIoc tries to resolve IOptions<AppSettings> and fails. 我认为这是因为选项模式使用Microsoft.Extensions.DependencyInjectionNancy使用TinyIoc作为默认值,因此TinyIoc尝试解析IOptions<AppSettings>并失败。

Is there a way to use IOptions<> with TinyIoc ? 有没有办法使用的方式IOptions<>TinyIoc

Here's my code: 这是我的代码:

Startup.cs Startup.cs

public void ConfigureServices(IServiceCollection services)
{
        services.AddOptions();
        services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
}

MyService.cs MyService.cs

public SearchService(IOptions<AppSettings> config)
{
}

Error: 错误:

Application startup exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. 应用程序启动异常:System.Reflection.TargetInvocationException:调用目标已抛出异常。

System.InvalidOperationException: Something went wrong when trying to satisfy one of the dependencies during composition, make sure that you've registered all new dependencies in the container and inspect the innerexception for more details. System.InvalidOperationException:尝试在组合期间满足其中一个依赖项时出错,请确保已在容器中注册所有新依赖项并检查innerexception以获取更多详细信息。

Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: Nancy.NancyEngine Nancy.TinyIoc.TinyIoCResolutionException:无法解析类型:Nancy.NancyEngine

Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: Nancy.Routing.DefaultRequestDispatcher Nancy.TinyIoc.TinyIoCResolutionException:无法解析类型:Nancy.Routing.DefaultRequestDispatcher

Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: Nancy.Routing.DefaultRouteResolver Nancy.TinyIoc.TinyIoCResolutionException:无法解析类型:Nancy.Routing.DefaultRouteResolver

Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: Nancy.Routing.RouteCache Nancy.TinyIoc.TinyIoCResolutionException:无法解析类型:Nancy.Routing.RouteCache

Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: MyProject.MyService Nancy.TinyIoc.TinyIoCResolutionException:无法解析类型:MyProject.MyService

Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: Microsoft.Extensions.OptionsModel.IOptions`1[[MyProject.AppSettings, MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] Nancy.TinyIoc.TinyIoCResolutionException:无法解析类型:Microsoft.Extensions.OptionsModel.IOptions`1 [[MyProject.AppSettings,MyProject,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]

Some extra info: 一些额外的信息:

"dependencies": {
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.Owin": "1.0.0-rc1-final",
    "Nancy": "1.4.3",
    "Microsoft.Framework.ConfigurationModel": "1.0.0-beta4",
    "Microsoft.Framework.ConfigurationModel.Json": "1.0.0-beta4",
    "Microsoft.Extensions.OptionsModel": "1.0.0-rc1-final"
},

DNX runtime version: DNX运行时版本:

1.0.0-rc1-update1    mono

Thank you very much. 非常感谢你。

Actually I found the answer. 其实我找到了答案。 I had to create a custom bootstrap and register the resolved dependency on TinyIoc: 我必须创建一个自定义引导程序并在TinyIoc上注册已解析的依赖项:

Startup.cs: Startup.cs:

    public void Configure(IApplicationBuilder app)
    {
        app.UseOwin(x => x.UseNancy(new NancyOptions
        {
            Bootstrapper = new CustomBootstrapper(app)
        }));
    }

CustomBootstrapper.cs: CustomBootstrapper.cs:

    protected override void ConfigureApplicationContainer(TinyIoCContainer container)
    {
        base.ConfigureApplicationContainer(container);

        container.Register<IOptions<AppSettings>>(_app.ApplicationServices.GetService<IOptions<AppSettings>>());
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM