gpt4 book ai didi

c# - IOptions 不适用于 TinyIOC/NancyFX

转载 作者:太空宇宙 更新时间:2023-11-03 23:22:20 28 4
gpt4 key购买 nike

我正在尝试在一个带有 NancyFX 的项目上实现选项模式(如推荐的 here )/TinyIOC但它不起作用。

我正在 Startup.cs.ConfigureServices 上注册选项方法,但是当我尝试在我的类上注入(inject)设置时 TinyIoc抛出:

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

我认为这是因为选项模式使用了 Microsoft.Extensions.DependencyInjection但是Nancy使用 TinyIoc默认为 TinyIoc试图解决 IOptions<AppSettings>并失败了。

有没有办法使用IOptions<>TinyIoc

这是我的代码:

启动.cs

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

我的服务.cs

public SearchService(IOptions<AppSettings> config)
{
}

错误:

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

一些额外的信息:

"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 运行时版本:

1.0.0-rc1-update1    mono

非常感谢。

最佳答案

其实我找到了答案。我必须创建一个自定义 Bootstrap 并在 TinyIoc 上注册已解析的依赖项:

启动.cs:

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

自定义 Bootstrap .cs:

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

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

关于c# - IOptions 不适用于 TinyIOC/NancyFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34991016/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com