我正在尝试在 NancyFX / TinyIOC 的项目上实施选项模式(如此处推荐:https://docs.asp.net/en/latest/fundamentals/configuration.html#options-config-objects),但它不是在职的。
我正在 Startup.cs/ConfigureServices 方法上注册选项,但是当我尝试在我的类 TinyIoc 上注入设置时,会抛出 Nancy.TinyIoc.TinyIoCResolutionException:无法解析类型:AppSettings。
我认为这是因为 Options Pattern 使用 Microsoft.Extensions.DependencyInjection 但 Nancy 使用 TinyIoc 作为默认值,因此 TinyIoc 尝试解析 IOptions 并失败。
有没有办法将 IOptions<> 与 TinyIoc 一起使用?
最坏的情况,我可以用 TinyIoc 替换 MS DependencyInjection 吗?
这是我的代码:
启动.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
非常感谢。
你没有注册 IOptions
希望这是有道理的。
我尝试通过这样做注册:
Nancy.TinyIoc.TinyIoCContainer.Current.Register(typeof(IOptions<>), typeof(OptionsManager<>));
顺便说一句, services.AddOptions();
为Microsoft.Extensions.DependencyInjection
你是这个意思吗?
services.Configure() 将采用加载的配置并在有人需要 AppSettings 时在 MS DI 上注册。
我在自定义引导程序上添加了这个:
container.Register<IOptions<ElasticSearchConfig>, OptionsManager<ElasticSearchConfig>>();
现在错误不再发生,但不幸的是该对象被创建为空。
调试时,我意识到这将返回正确的填充对象:
`var appSettings = app.ApplicationServices.GetService
但这将返回一个空对象:
var appSettings = Nancy.TinyIoc.TinyIoCContainer.Current.Resolve<IOptions<AppSettings>>();
尽管如此,当我请求 IOptions 时,TinyIoc 正在取代 Ms Framework DIMicrosoft.Extensions.OptionsModel.IOptions
时它不应该“回复”?
就像我最初说的那样,您需要传入该类型实例或配置的容器,仅注册类型本身是没有用的,因为它需要由 GetService 工厂构造。
嘿,现在我明白你说的了,它奏效了! 我将IApplicationBuilder
发送到自定义引导程序,并从那里注册了IOptions<AppSettings>
与ApplicationServices.GetService()
返回的值。
我不确定这是最好的解决方案,因为 TinyIoc 将充当我的服务和Microsoft.Extensions.DependencyInjection
之间的网关。
@RTodorov创建一个使用Microsoft.Extensions.DependencyInjection
作为容器代替 TinyIoC 的 Nancy 引导程序库可能是合适的,这已经为许多不同的容器完成了。
最有用的评论
@RTodorov创建一个使用
Microsoft.Extensions.DependencyInjection
作为容器代替 TinyIoC 的 Nancy 引导程序库可能是合适的,这已经为许多不同的容器完成了。