温莎城堡PerWebRequest生活方式没有被兑现 [英] Castle Windsor PerWebRequest Lifestyle is not being honored

查看:119
本文介绍了温莎城堡PerWebRequest生活方式没有被兑现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个3层的MVC应用程序。数据层包含EF4 code-第一的DbContext:

I'm working on a 3-tier MVC application. The data layer contains an EF4 code-first DbContext:

public class MyDataContext : DbContext
{
    // DbSet<>s...
}

另外还有一个接口和DI实现:

There's also an interface and an implementation for DI:

public interface IContextFactory
{
    MyDataContext GetContext();
}

public class ContextFactory : IContextFactory
{
    readonly MyDataContext context;
    public ContextFactory(MyDataContext context)
    {
        this.context = context;
    }

    public MyDataContext GetContext()
    {
        return this.context;
    }
}

和一个存储库模式:

public interface IRepository<T>
{
    T Create();
    void Insert(T entity);
    void Delete(T entity);
    ...
    void Save();
}

public class Repository<TEntity> : IRepository<TEntity>
  where TEntity: class, new()
{
    public Repository(IContextFactory factory)
    {
        this.context = factory.GetContext();
        this.set = factory.Set<TEntity>();
    }
    ...
}

上部层通过访问实体 IRepository&LT;&GT; 与温莎城堡注入。自定义供应商/模块明确 .Resolve&LT;方式&gt;()并根据需要

The upper tiers access the entities by having IRepository<> injected with castle windsor. Custom providers/modules explicitly .Resolve<>() them as needed.

数据层是被注册在城堡 IWindsorInstaller

The data layer is being registered in a castle IWindsorInstaller:

container.Register(
    Component.For<MyDataContext>()
    .DependsOn(new Hashtable() { {"connectionStringName", "DefaultConnection"} })
    .LifestylePerWebRequest());

container.Register(
    Component.For<IContextFactory>()
    .ImplementedBy<ContextFactory>()
    .LifestylePerWebRequest());

container.Register(
     Component.For(typeof(IRepository<>))
    .ImplementedBy(typeof(Repository<>))
    .LifestylePerWebRequest());

我不知道什么是错的 - 我的测试不包括数据上下文 - 但在调试模式下我的数据上下文的构造是越来越每个Web请求近12次叫

I wouldn't know anything was wrong - my tests don't cover the data context - but in debug mode my data context's constructor is getting called nearly a dozen times per web request.

修改:虽然它并不能解释为什么 MyDataContext 不越来越作用域为Web请求,在构造函数中我的破发点揭示了一个相同的调用堆栈都打有或让倍,它的构造: MembershipProvider.GetUser - &GT;新的存储库(IContextFactory)。我没有显式调用的getUser - 在世界上什么会导致FormsAuthentication调用的getUser这么多次

edit: Whilst it does not explain why Repository and MyDataContext aren't getting scoped to web requests, my break point inside the constructor reveals an identical call stack all dozen-or-so times that its constructed: MembershipProvider.GetUser -> new Repository(IContextFactory). I have no explicit calls to GetUser - what in the world would cause FormsAuthentication to call GetUser so many times?

推荐答案

添加这样的事情在你的Global.asax.cs文件:

Add something like this in your Global.asax.cs file:

    protected void Application_BeginRequest(object sender, EventArgs args)
    {
        System.Diagnostics.Debug.WriteLine(this.Request.RequestType + " " + this.Request.RawUrl);
    }

使用调试器挂钩,并验证是否有真的仅仅是一个每个要求的要求。也许你有平行运行Ajax请求。或mayby​​你有你的内容元素(js文件,图像)保护,其获取正在执行C#code。

Hook up with debugger and verify that there realy is just one request per "request". Maybe you have ajax requests running parallelly. Or mayby you have your content elements (js files, images) protected and their gets are executing C# code.

这篇关于温莎城堡PerWebRequest生活方式没有被兑现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆