简体   繁体   中英

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.

I'm registering the Options on the Startup.cs.ConfigureServices method but when I try to inject the settings on my class TinyIoc throws:

Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: 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.

Is there a way to use IOptions<> with TinyIoc ?

Here's my code:

Startup.cs

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

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.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.

Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: Nancy.NancyEngine

Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: Nancy.Routing.DefaultRequestDispatcher

Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: Nancy.Routing.DefaultRouteResolver

Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: Nancy.Routing.RouteCache

Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: 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]]

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:

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:

Startup.cs:

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

CustomBootstrapper.cs:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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