We've created several WCF services that process asynchronous requests. We're using basicHttpBinding
, consequently our InstanceContextMode
is PerCall
and this is what's causing a little confusion. We're seeing unusual behavior from those parts of the application being injected using Microsoft's Unity
container.
We're resolving the reference below to create a singleton of Foo
that is used throughout the application. However, when the service is hit in quick succession, Foo
will occasionally throw exceptions that indicate that it is being accessed by multiple threads and having its state changed in unexpected ways as a result.
Container.RegisterType<IFoo, Foo>(new ContainerControlledLifetimeManager());
Now, if we change the lifetime manager to TransientLifetimeManager
- essentially telling the container to inject a new instance of the class every time it's resolved, the issue is corrected.
Container.RegisterType<IFoo, Foo>(new TransientLifetimeManager());
From my understanding, the WCF doesn't control the lifetime of the AppDomain, the host does. In our case, that is IIS. So, given this information is it possible that our PerCall
WCF requests are working correctly, but due to how the AppDomain is being managed, could we be accessing the same injected object due to its singleton implementation?
Thanks for your time!