0

I have a N-layer solution that works correctly in my dev environment. Apparently it works also on production environment, but sometime the execution fails. I do not understand why. I just know that nothing changes on database, no usefull error is visible and no log is written. My supposition is a concurrency problem. I think that something fails when I try to do more than one select once the entity framework context has been initialized.

Here how my solution is structured

enter image description here

In the facade I inject the entity framework context. Here the configuration on my web.config of the service interface:

<containers>
  <container>
    <types>
      <register type="it.MC.IContext.IDataContext, IContext"
                mapTo="it.MC.EntityFrameworkContext.PublicAreaContext, EntityFrameworkContext">
        <lifetime type="singleton" />        
      </register>

      <register type="it.MC.IFacade.IPublicAreaFacade, IFacade"
                mapTo="it.MC.Facade.PublicAreaFacade, Facade">
        <interceptor type="TransparentProxyInterceptor" />
        <lifetime type="singleton" />
        <constructor>
          <param name="context" type="it.MC.IContext.IDataContext, IContext"/>
        </constructor>
      </register>
    </types>
  </container>
</containers>

As you can see, my context and the facade are singleton. I think both are really wrong. I think that both Facade that the entity Framewrk context should be instanciate per request. I think this will solve the problem of the concurrency too.

Can anyone help me to correct my code please?

Thank you

1 Answer 1

1

I know that your question is:

Can anyone help me to correct my code please?

I read it like this:

Can anyone help me change this code so that IContext and IFacade will be re-initialized per request.

With that said... Yes, I also doubt that you want to keep your IContext as a singleton.

Why you shouldn't use singleton DataContexts in Entity Framework

Here's how you can change the lifetimemanager to PerRequestLifetimeManager, if that's what you want. Note that you probably need the Unity.Mvc NuGet-package.

<containers>
  <container>
    <types>
      <register type="it.MC.IContext.IDataContext, IContext"
                mapTo="it.MC.EntityFrameworkContext.PublicAreaContext, EntityFrameworkContext">
        <lifetime type="Microsoft.Practices.Unity.PerRequestLifetimeManager, Microsoft.Practices.Unity.Mvc" />        
      </register>

      <register type="it.MC.IFacade.IPublicAreaFacade, IFacade"
                mapTo="it.MC.Facade.PublicAreaFacade, Facade">
        <interceptor type="TransparentProxyInterceptor" />
        <lifetime type="Microsoft.Practices.Unity.PerRequestLifetimeManager, Microsoft.Practices.Unity.Mvc" />
        <constructor>
          <param name="context" type="it.MC.IContext.IDataContext, IContext"/>
        </constructor>
      </register>
    </types>
  </container>
</containers>

Before moving to production I suggest you read this post about the PerRequestLifetimeManager.

Its purpose would be to only instantiate one instance per request, which could (for example) prevent redundant operations and lookups during the course of a single request.

The danger is if someone assumes that the object created is a good place to store state during the request. The idea of dependency injection is that a class receives a dependency (commonly an interface) and doesn't "know" anything about it at all except that it implements that interface.

Also, think about the Facade you got, and how it will work if it's re-initated every request. Does it perform any heavy operations at initialization? You might want to think about the lifetimemanager for that one.

UPDATE Since you're using WebAPI you should be able to use HierarchicalLifetimeManager instead.

http://www.asp.net/web-api/overview/advanced/dependency-injection

The dependency resolver attached to the HttpConfiguration object has global scope. When Web API creates a controller, it calls BeginScope. This method returns an IDependencyScope that represents a child scope.

Web API then calls GetService on the child scope to create the controller. When request is complete, Web API calls Dispose on the child scope. Use the Dispose method to dispose of the controller’s dependencies.

http://www.devtrends.co.uk/blog/introducing-the-unity.webapi-nuget-package

If you are registering any components that implement IDisposable such as Entity Framework's DbContext, you will want to make sure that these components get disposed of at the end of the request. This is achieved by registering these components with a HierarchicalLifetimeManager.

Sign up to request clarification or add additional context in comments.

5 Comments

Interesting.. I had seen that perRequestLifetimeManager but I was not sure was the correct solution because I need the unity.mvc package and I do not have mvc.
I do not think the facade is a problem, because all the initializations inside it are lazy. The PerRequestLifrtimeManager should not be a problem because my back end is stateless. I just have a doubt. In the link you sent me is written: Let's say someone determines that the lifestyle of that dependency should be transient, not per web request. What transient means?
Transient means that it's newed up every time. Not per request or singleton, but everytime it's resolved. I'll try to find a resource for WebApi as well.
So in my case, transient should coincide with the request because I risolve the context in the facade that is called once per request
@Ciccio - Yes, in that case Transient is correct. You can also try with PerResolveLifetimeManager, which creates an instance per resolve-cycle. Set a debugger in your dispose and constructor (or log it). Then make a few requests and you should see it beeing created and disposed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.