Note that there are some explanatory texts on larger screens.

plurals
  1. POUnity not honoring TransientLifetimeManager?
    primarykey
    data
    text
    <p>I have the following scenario:</p> <p>A <code>WCF</code> service solution that contains a service implementation layer, business layer and data layer. The <code>WCF</code> implementation layer depends on the business layer and the business layer on the data layer. Each layer receives it's dependencies via constructor injection. We've wired this all together using Unity. We have a custom <code>WCF</code> service host/factor/behavior/instance provider that calls into our Unity container to resolve the <code>WCF</code> service to use and it's various dependencies. </p> <p>It's working great...almost.</p> <p>We're using <code>EntityFramework</code> code first in our data layer. What we're seeing is that a new DbContext class is created on the initial call into <code>WCF</code>, but that class is reused on subsequent calls into the service. This becomes a problem of course when our service is serving multiple calls that are accessing the database concurrently. I was able to prove out that the <code>DbContext</code> class is in fact being reused by placing some trace statements in the DbContext class' constructor. I went further up the chain and places similar trace statements in the business layer implementation constructor and found that this is also only being called once. In fact, it appears that only the <code>WCF</code> service implementation is being constructed on each service call and that the data and business layers are behaving as singletons.</p> <p>From what I've read, it sounds like the default behavior when using <code>RegisterType</code> with Unity is to use the <code>TransientLifetimeManager</code> and that this implies a new instance will be created on every call to <code>Resolve</code>. Since it didn't appear that this default behavior was working correctly, we tried explicitly setting the lifetime manager as <code>TransientLifetimeManager</code>, however we're still seeing our business and data layers behaving as singletons. </p> <p>Any ideas on what I need to do to get our <code>WCF</code> dependencies to behave as singletons?</p> <p><strong>Update:</strong> Still no success. I tried using the PerResolveLifetimeManager, however that did not solve the problem. As a temporary work around, I refactored my code so that it's injecting a factory into my data layer. The factory provides DbContext instances, so I'm able to ensure that each call into my data layer is using a new DbContext. This is working ok for now, but I'd like to resolve whatever issue is causing Unity to hold onto and re-use instances after they're created.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload