What is Proper way to inject caller username on the server side of IIS hosted web service

May 29, 2014 at 9:39 PM

I have troubles injectiong the authenticated user credentials with unity and WCF, Id like to use child containers, but I have read that they are not thread safe.

See the following questions for details: http://stackoverflow.com/questions/23724481/unity-a-proper-way-to-inject-caller-username-on-the-server-side-of-iis-hosted-w

Any help would be appreciated.

Jun 2, 2014 at 12:43 AM
As far as I know child containers are thread safe. Another approach would be to use a per request lifetime manager similar to what is in Unity bootstrapper for ASP.NET MVC.

If you are resolving one object (e.g. a WCF service) and constructing the object graph for that one object per request then you might be able to just use a DependencyOverride to specify an IAuthenticateduser instance to use wherever there is a dependency of the type IAuthenticateduser for the entire object graph. See Using Dependency Overrides for some examples.

Randy Levy
Enterprise Library support engineer
Support How-to
Jun 2, 2014 at 7:22 AM

Thanks for the answer.

I can not use dependency overrides, because I am resolving multiple times. I've been explicitly registering IUnityContainer so that clients could resolve agaisnt it. However this caused StackOverflow exception on Dispose (). Googling for it I have found, that this is a well known problem. When I removed explicit registration, everything worked OK. You might want to consider throwing exception as part of Registration to avoid crasing Dipose.,

I've taken a look at Uniy.Mvc5, (https://github.com/feedbackhound/Unity.Mvc5/blob/master/Unity.Mvc5/UnityDependencyResolver.cs) and I can see that it is also using CreateChildContainer.

I also examine the source code of UnityContainer and its uses locks when modifying list of child containers.

So child containers are the way to go!.

Marked as answer by matra on 6/2/2014 at 12:22 AM