我试图对城堡windsor进行一些测试,在我的一个测试中我想检查windsor安装程序,所以我检查容器可以解析我的组件给定其界面.
到目前为止,这么好,问题开始时组件在其安装程序中具有PerWebRequest生活方式,起初它抱怨HttpContext.Current为null,有一个解决了在测试设置中创建假上下文我现在在nunit中有这个异常测试
System.Exception:看起来你忘记注册http模块Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule在你的web.config部分添加''.如果您在集成模式下运行IIS7,则需要将其添加到下面的部分
当我从NUnit运行它时,我如何在windsor中注册模块或类,以便它可以工作,或者如何模拟,因为在此测试中并不是真正的Web请求,只需检查容器是否解析了类型.
如果我在真正的webrequest之外使用这个组件进行任何集成测试,也有同样的事情会发生,有没有办法让这个工作或真的模拟一个Web请求,这样这个测试可以运行?
Tranks提前
费尔
我正在转换现有的ASP .Net Web API 2项目以使用OWIN.该项目使用Castle Windsor作为依赖注入框架,其中一个依赖项设置为使用PerWebRequest生活方式.
当我向服务器发出请求时,我得到一个Castle.MicroKernel.ComponentResolutionException例外.该异常建议将以下内容添加到配置文件中的system.web/httpModules和system.WebServer/modules部分:
<add name="PerRequestLifestyle"
type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor" />
Run Code Online (Sandbox Code Playgroud)
这不能解决错误.
从SimpleInjector的OWIN集成提供的示例中获取灵感,我尝试使用以下方法在OWIN启动类中设置范围(以及更新依赖关系的生活方式):
appBuilder.User(async (context, next) =>
{
using (config.DependencyResolver.BeginScope()){
{
await next();
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这也没有用.
我如何使用Castle Windsor的PerWebRequest生活方式或在OWIN中模拟它?
Castle/Windsor新手,请耐心等待.
我目前正在使用框架System.Web.Mvc.Extensibility,并在其启动代码中,它注册了HttpContextBase,如下所示:
container.Register(Component.For<HttpContextBase>().LifeStyle.Transient.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
Run Code Online (Sandbox Code Playgroud)
我想要做的是改变行为并将httpContextBase的生活方式改为PerWebRequest.
所以我将代码更改为以下内容:
container.Register(Component.For<HttpContextBase>().LifeStyle.PerWebRequest.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时,我收到以下错误:
System.Configuration.ConfigurationErrorsException: Looks like you forgot to
register the http module Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule
Add '<add name="PerRequestLifestyle"
type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.MicroKernel"
/>' to the <httpModules> section on your web.config
Run Code Online (Sandbox Code Playgroud)
这是我做下<system.web>和<system.webServer>,但是,我仍然得到同样的错误.任何提示?
提前致谢.
更新
每个请求添加了代码块
在system.web.mvc.extensibility框架中,有一个名为extendedMvcApplication的类,它继承自HttpApplication,在Application_start方法中,它调用BootStrapper.Execute().此方法的实现如下:
public void Execute()
{
bool shouldSkip = false;
foreach (IBootstrapperTask task in ServiceLocator.GetAllInstances<IBootstrapperTask>().OrderBy(task => task.Order))
{
if (shouldSkip)
{
shouldSkip = false;
continue;
}
TaskContinuation continuation = task.Execute(ServiceLocator);
if (continuation == TaskContinuation.Break) …Run Code Online (Sandbox Code Playgroud) 我有一个ServiceStack API应用程序,它使用Simple Injector作为其IoC容器.我需要某些组件才能拥有"按网络请求"的生活方式.
我在这里查看了Simple Injector文档,发现它没有一个,而是两个与'每个web请求'相对应的生活方式:
这让我很困惑,因为我一直认为所有ASP.NET应用程序都使用相同的基本管道,并且通过插入HTTP模块,每个Web请求实现了IoC容器.为什么Web API应用程序会有所不同?
任何人都可以了解哪个最适合ServiceStack API应用程序?
dependency-injection perwebrequest servicestack simple-injector
我有一个MVC 4项目,我正在尝试将PerWebRequest对象注入我的控制器.但是,似乎没有跨多个请求重新创建对象
private static IWindsorContainer InitializeWindsor()
{
var container = new WindsorContainer().Install(FromAssembly.This());
// Add Factory facility
container.AddFacility<TypedFactoryFacility>();
// Register all controllers from this assembly
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
container.Register(
AllTypes.FromAssembly(assembly).BasedOn<Controller>().Configure(c => c.LifestyleTransient())
);
}
// Register HTTP Handlers
container.Register(Component.For<HttpRequestBase>().LifeStyle.PerWebRequest.UsingFactoryMethod(() => new HttpRequestWrapper(HttpContext.Current.Request)));
container.Register(Component.For<HttpContextBase>().LifeStyle.PerWebRequest.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
// Register components
container.Register(Component.For<PerWebRequestObject>().LifeStyle.PerWebRequest);
}
Run Code Online (Sandbox Code Playgroud)
这是我的PerWebRequestObject:
public class PerWebRequestObject
{
public DateTime DateCreated { get; set; }
public PerWebRequestObject()
{
DateCreated = DateTime.UtcNow;
Debug.WriteLine("Created: " + DateCreated.ToLongTimeString());
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的TestController:
public class …Run Code Online (Sandbox Code Playgroud)