什么是我Ninject的范围ObjectContext的注入在我的自定义的MembershipProvider(使用Request范围)? [英] What is the scope of my Ninject injected ObjectContext in my custom MembershipProvider (using Request scope)?

查看:123
本文介绍了什么是我Ninject的范围ObjectContext的注入在我的自定义的MembershipProvider(使用Request范围)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架4和ASP.NET MVC 3,我做了一个自定义的成员资格提供程序,并使用Ninject注入的EFAccountRepository进去(绑定IAccountRepository到EFAccountRepository)。

I use Entity Framework 4 and ASP.NET MVC 3. I made a custom membership provider and use Ninject to inject an EFAccountRepository into it (Bound IAccountRepository to EFAccountRepository).

此帐户存储库注入到它的ObjectContext的。我也用在我的控制器这个仓库(及其他)。出于这个原因,当我必将IContext我ObjectContext的,我设置为每个请求,所以只ObjectContext的住在一个请求,并在仓库之间共享的范围。

This account repository has an ObjectContext injected into it. I also use this repository (and others) in my controllers. For this reason when I bound IContext to my ObjectContext, I set the scope to "per request" so the ObjectContext only lives in one request and is shared between the repositories.

我有时会当试图登录到以下错误:该ObjectContext的实例已经被处置,不能再用于需要连接的操作

I am sometimes get the following error when trying to log in:"The ObjectContext instance has been disposed and can no longer be used for operations that require a connection."

不知成员资格提供获取多久实例化。我通过在标记与库属性[注入] 并调用 Kernel.Inject 注射进库成员资格提供的Application_Start 在Global.asax文件的功能。

I wonder how often the membership provider gets instantiated. I injected the repository into the membership provider by marking the repository property with [Inject] and calling Kernel.Inject in the Application_Start function in the global.asax file.

如果供应商被实例化不止一次我将不得不再次我想注入。但是,我没有得到一个空指针异常,所以我不认为这是它。

If the provider gets instantiated more than once I would have to inject again I suppose. However, I don't get a null pointer exception, so I don't think that's it.

下面是一些code:

MyNinjectModule.cs

    public override void Load()
    {
        Bind<IMyContext>().To<MyObjectContext>().InRequestScope();
        // put bindings here
        Bind<IAccountRepository>().To<EFAccountRepository>
    }

的Global.asax

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        var kernel = new StandardKernel(new MyNinjectModule());
        ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory(kernel));
        kernel.Inject(Membership.Provider);
    }

MyMembershipProvider.cs

    [Inject]
    public IAccountRepository accountRepository { get; set; }

    public override bool ValidateUser(string username, string password)
    {
        // I get the exception here.
        return (from a in accountRepository.Accounts
                where a.UserName == username 
                 && a.Password == password
                select true).SingleOrDefault();
    }

EFAccountRepository.cs

    private readonly IMyContext context;

    public EFAccountRepository(IMyContext context)
    {
        this.context = context;
    }

    public IQueryable<Account> Accounts
    {
        get { return context.Accounts; }
    }

MyObjectContext.cs

public class MyObjectContext : ObjectContext, IMyContext
{
    public IObjectSet<Account> Accounts { get; private set; }

    public FlorenceObjectContext()
        : this("name=DomainModelContainer")
    {
    }

    public FlorenceObjectContext(string connectionString)
        : base(connectionString, "DomainModelContainer")
    {
        Accounts = CreateObjectSet<Account>();
    }
}

PS:我总是愿意就一般我的code评论;)

PS: I'm always open to comments on my code in general ;).

推荐答案

例外说,你没有正确处理上下文处置。某处你叫 context.Dispose (或者使用背景),但之后要再次是使用环境上下文已经部署不可能的,因为。如果您在每次请求上下文中使用,您必须在请求处理的最后处置情况下只有一次(当你确定没有code将使用context)。

The exception says that you are incorrectly handling disposing of your context. Somewhere you call context.Dispose (or have context in using) but after that you want to use context again which is not possible because context is already disposed. If you are using per request context you must dispose context only once at the end of request processing (when you are sure that no code will use the context).

这篇关于什么是我Ninject的范围ObjectContext的注入在我的自定义的MembershipProvider(使用Request范围)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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