Nancy: 无法在 Nancy/TinyIoc 中使用 IOptions 模式

创建于 2016-01-25  ·  6评论  ·  资料来源: NancyFx/Nancy

我正在尝试在 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

非常感谢。

最有用的评论

@RTodorov创建一个使用Microsoft.Extensions.DependencyInjection作为容器代替 TinyIoC 的 Nancy 引导程序库可能是合适的,这已经为许多不同的容器完成了。

所有6条评论

你没有注册 IOptions到 Nancy 的容器中的任何地方,所以它不会工作,我不确定那是什么神奇的“services.Configure(Configuration.GetSection("AppSettings"));" 在幕后进行,但您需要通过以下任一方式将该类型注册到 Nancy 的容器中:

  • 创建自己的引导程序,传递 IOptions进入它的 ctor,使用你正在配置 Nancy 使用的引导程序,然后在引导程序中注册 ConfigureApplicationContainer
  • 如果要共享更多类型,请执行与上述相同的操作,但不是传入类型,而是传入新配置的 TinyIocContainer,然后在派生引导程序中为 GetApplicationContainer 返回该实例。

希望这是有道理的。

我尝试通过这样做注册:

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 DI. 我还意识到所有 Microsoft.* dll 都在 TinyIoc 的忽略列表中,这是否意味着当我要求Microsoft.Extensions.OptionsModel.IOptions时它不应该“回复”?

就像我最初说的那样,您需要传入该类型实例或配置的容器,仅注册类型本身是没有用的,因为它需要由 GetService 工厂构造。

嘿,现在我明白你说的了,它奏效了! 我将IApplicationBuilder发送到自定义引导程序,并从那里注册了IOptions<AppSettings>ApplicationServices.GetService()返回的值。

我不确定这是最好的解决方案,因为 TinyIoc 将充当我的服务和Microsoft.Extensions.DependencyInjection之间的网关。

@RTodorov创建一个使用Microsoft.Extensions.DependencyInjection作为容器代替 TinyIoC 的 Nancy 引导程序库可能是合适的,这已经为许多不同的容器完成了。

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

Radzhab picture Radzhab  ·  11评论

thecodejunkie picture thecodejunkie  ·  8评论

thecodejunkie picture thecodejunkie  ·  4评论

jchannon picture jchannon  ·  7评论

lgabryel picture lgabryel  ·  7评论