【问题标题】:castle windsor exception when using in selfhosted web api在自托管 Web api 中使用时城堡温莎异常
【发布时间】:2017-11-10 08:27:11
【问题描述】:

我在自托管 Web Api(使用 OwinSelfHost)中使用 Castle Windsor,但是当我调用例如控制器的 Get 方法(它只是返回一个字符串)时,我在 GetService WindsorDependencyScope 类的方法(实现 IDependencyScope ):

看起来您忘记注册 http 模块 Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule 要修复此添加 到 web.config 上的部分。 如果您计划在集成管道模式下在 IIS 上运行,您还需要将模块添加到 . 或者确保您的 GAC 中有 Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 程序集(由 ASP.NET MVC3 或 WebMatrix 安装),Windsor 将能够自动注册该模块无需向配置文件中添加任何内容。

有什么解决办法吗?

【问题讨论】:

    标签: c# asp.net-web-api owin castle-windsor self-hosting


    【解决方案1】:

    我可以找到问题所在。在 ControllerInstaller 类中,使用了导致问题的 LifestylePerWebRequest 生活方式。我不知道确切的技术原因,但是当在“Web Api 2 + self hosts with Owin”的组合中使用温莎城堡时,我们应该使用 LifestyleScoped 而不是 LifestylePerWebRequest 。这是安装程序代码(正确的): public class ControllerInstaller : IWindsorInstaller { public void Install(IWindsorContainer container, IConfigurationStore store) { container.Register(Classes.FromThisAssembly() .Pick().If(t => t.Name.EndsWith("Controller")) .Configure(configurer => configurer.Named(configurer.Implementation.Name)) //.LifestylePerWebRequest() //---this caused the bug .LifestyleScoped() //the correct one ); } }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多