IOptions 不适用于 TinyIOC/NancyFX
IOptions not working with TinyIOC/NancyFX
我正在尝试在具有 NancyFX
/TinyIOC
的项目上实施选项模式(推荐 here),但它不起作用。
我正在 Startup.cs.ConfigureServices
方法上注册选项,但是当我尝试在我的 class 上注入设置时 TinyIoc
抛出:
Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: AppSettings.
我认为这是因为选项模式使用 Microsoft.Extensions.DependencyInjection
但 Nancy
默认使用 TinyIoc
所以 TinyIoc
试图解析 IOptions<AppSettings>
但失败了。
有没有办法将 IOptions<>
与 TinyIoc
一起使用?
这是我的代码:
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
}
MyService.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 上注册已解析的依赖项:
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>>());
}
我正在尝试在具有 NancyFX
/TinyIOC
的项目上实施选项模式(推荐 here),但它不起作用。
我正在 Startup.cs.ConfigureServices
方法上注册选项,但是当我尝试在我的 class 上注入设置时 TinyIoc
抛出:
Nancy.TinyIoc.TinyIoCResolutionException: Unable to resolve type: AppSettings.
我认为这是因为选项模式使用 Microsoft.Extensions.DependencyInjection
但 Nancy
默认使用 TinyIoc
所以 TinyIoc
试图解析 IOptions<AppSettings>
但失败了。
有没有办法将 IOptions<>
与 TinyIoc
一起使用?
这是我的代码:
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
}
MyService.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 上注册已解析的依赖项:
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>>());
}