1

I got an ASP.NET MVC application which uses EF to handle DB. I've used DDD as the architecture and I got the Repository and Service patterns.

I'm trying to use StructureMap for DI but for some reason my DB got disposed after the first request.

Edit: The error I'm error I'm getting is

The operation cannot be completed because the DbContext has been disposed.

It seems that I'm getting it in the repository, for instance in:

public class AccountRepository : Repository<Account>, IAccountRepository
{
    public AccountRepository(MyDbContext context) : base(context) { }

    public Account FindAccountByEmailAddress(string emailAddress, bool loadRelatedRoles = false)
    {
        IQueryable<Account> query = (from a in Entity
                                     where a.LoweredEmailAddress == emailAddress.ToLower()
                                     select a);
        if (loadRelatedRoles)
        {
            return query.Include(a => a.Roles).FirstOrDefault();
        }

        return query.FirstOrDefault();
    }
}

In the Application_BeginRequest I'm registering the DB using

        ObjectFactory.Configure(x =>
        {
            x.For(typeof(MyDbContext))
                .HttpContextScoped();
        });

In order to reserve it as one instance per request.

In the Application_EndRequest I'm releasing the request using:

    protected void Application_EndRequest(object sender, EventArgs e)
    {
        StructureMap.ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
    }

Am I missing something? or my approch is OK and there's maybe a problem with my Repository implementation that making it happen.

OzB
  • 2,140
  • 1
  • 22
  • 38
  • 1
    Does this happen every time or only when the `loadRelatedRoles` is true? What I'm trying to get to here is check if the "Roles" are added lazily and you try to fetch them later on (after the Context has been disposed). Can you also show the Controller code where the Repository is (injected and) used? – Tallmaris Mar 28 '13 at 16:28
  • I may be wrong, but don't all requests throw a start and end request? i.e. each css file or image, could it be you are disposing objects unnecessarily? – Slicksim Mar 28 '13 at 16:28
  • About your first question - I do use Lazy, but I've removed that for testing and the error continues to appear. After further diagnosing by following your comment - I've noticed that I got this problem since I'm connecting to the DB in Application_Start and Application_OnPostAuthenticateRequest which seems to use different HttpContext. – OzB Mar 28 '13 at 18:08
  • About the .css and .js etc. You right - but I'm filtering them by applying Regex on the path and determine whether or not I should resolve this request. – OzB Mar 28 '13 at 18:12
  • Related: http://stackoverflow.com/questions/10585478/one-dbcontext-per-web-request-why – Steven Mar 28 '13 at 19:17
  • How are you creating the 'Account' repository? That is, how is Structuremap creating it? – ozczecho Mar 28 '13 at 23:25
  • I'm registering it using For().Use() - That hand-coding is the way I'm using for all of my repos and services (No DLL scanning) since I just switched from Unity and this is not organized yet. – OzB Mar 28 '13 at 23:31

1 Answers1

1

Ok I found the problem - it seems that my Application_Start code was invoked more than one time and I've used Lazy in some cases - that caused the saving of null reference to the DB context.

Thanks for your help!

OzB
  • 2,140
  • 1
  • 22
  • 38