0

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!

elucid8
  • 1,412
  • 4
  • 19
  • 40
  • Is Unity created within an IInstanceProvider behavior or is it injecting dependencies in a different way? – ErnieL Apr 17 '13 at 14:49

1 Answers1

0

Have a look at UnityWcf. I have tried a couple of different approaches to aligning the lifetime of objects in Unity to the InstanceContextMode in WCF. This works very well:

http://unitywcf.codeplex.com

freshr
  • 955
  • 1
  • 9
  • 27