gpt4 book ai didi

c# - 是什么导致错误 "Looks like you forgot to register the http module CaSTLe.MicroKernel.Lifestyle.PerWebRequestLifestyleModule"?

转载 作者:行者123 更新时间:2023-11-30 15:28:36 28 4
gpt4 key购买 nike

我使用 caSTLe.windsor 和 owin 在 webapi2 中获得了一些 DI。这是相关代码:

[assembly: OwinStartup(typeof(Bla.Startup))]
namespace Bla
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
//...
var container = new WindsorContainer().Install(new ControllerInstaller());
var httpDependencyResolver = new WindsorHttpDependencyResolver(container);
config.DependencyResolver = httpDependencyResolver;
//...
}
}

public class ControllerInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(AllTypes.FromThisAssembly()
.Pick().If(t => t.Name.EndsWith("Controller"))
.Configure(configurer => configurer.Named(configurer.Implementation.Name))
.LifestylePerWebRequest());

//...
}
}

internal class WindsorDependencyScope : IDependencyScope
{
private readonly IWindsorContainer _container;
private readonly IDisposable _scope;

public WindsorDependencyScope(IWindsorContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}

_container = container;
_scope = container.BeginScope();
}

public object GetService(Type t)
{
return _container.Kernel.HasComponent(t)
? _container.Resolve(t) : null;
}

public IEnumerable<object> GetServices(Type t)
{
return _container.ResolveAll(t)
.Cast<object>().ToArray();
}

public void Dispose()
{
_scope.Dispose();
}
}

internal sealed class WindsorHttpDependencyResolver : IDependencyResolver
{
private readonly IWindsorContainer _container;

public WindsorHttpDependencyResolver(IWindsorContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}

_container = container;
}

public object GetService(Type t)
{
return _container.Kernel.HasComponent(t)
? _container.Resolve(t) : null;
}

public IEnumerable<object> GetServices(Type t)
{
return _container.ResolveAll(t)
.Cast<object>().ToArray();
}

public IDependencyScope BeginScope()
{
return new WindsorDependencyScope(_container);
}

public void Dispose()
{
}
}

owin 的一大优点是它消除了对 system.web 的依赖。这允许从其他事物中执行相当有效的(在内存中?)集成测试。我面临的问题是这一行:

config.DependencyResolver = httpDependencyResolver;

引入对系统的依赖。所以当我尝试运行这样一个简单的测试时:

private TestServer _server;

[TestFixtureSetUp]
public void FixtureInit()
{
_server = TestServer.Create<Startup>();
}

[Test]
public voidSomeTest()
{
using (var server = TestServer.Create<Startup>())
{
HttpResponseMessage response = server.HttpClient.GetAsync("/api/bladibla/test").Result;

// ...
}
}

我得到:

{"Looks like you forgot to register the http module Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule\r\nTo fix this add\r\n<add name=\"PerRequestLifestyle\" type=\"Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor\" />\r\nto the <httpModules> section on your web.config.\...

我可以在测试项目的 app.config 中注册一些东西吗?非常欢迎任何反馈。

附言:

我尝试添加:

<system.web>
<httpModules>
<add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor" />
</httpModules>
</system.web>
<system.webServer>
<modules>
<add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor" />
</modules>
</system.webServer>

到 app.config 没有成功。

PPS:

好的,如果我改变:

container.Register(AllTypes.FromThisAssembly()
.Pick().If(t => t.Name.EndsWith("Controller"))
.Configure(configurer => configurer.Named(configurer.Implementation.Name))
.LifestylePerWebRequest());

到:

container.Register(AllTypes.FromThisAssembly()
.Pick().If(t => t.Name.EndsWith("Controller"))
.Configure(configurer => configurer.Named(configurer.Implementation.Name))
.LifestyleTransient());

它有效,这是有道理的。最大的问题是,我如何动态更改它,具体取决于它是在部署/适当的 Web 环境中还是在测试环境中。

最佳答案

{"Looks like you forgot to register the http module Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule\r\nTo fix this add\r\n\r\nto the section on your web.config....

但您尝试注册:

PerRequestLifestyle

看出区别了吗? PerWebRequestLifestyle 不是 PerRequestLifeStyle。

看看Mauricio Scheffer`s HybridLifeStyles这使得用于测试的容器的设置非常非常容易。

关于c# - 是什么导致错误 "Looks like you forgot to register the http module CaSTLe.MicroKernel.Lifestyle.PerWebRequestLifestyleModule"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25442578/

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