I’m using a UserNamePasswordValidator in WCF along with Unity for my dependency injection, but since WCF creates the instance of the UserNamePasswordValidator, I cannot inject my container into the class. So how would one go about this?
The simplest solution I can think of is to create a static proxy/wrapper class around a static instance of a UnityContainer, which exposes all the same methods… This way, any class can access the container, and I don’t need to inject it everywhere.
So I could just do UnityContainerWrapper.Resolve() anywhere in code. So basically this solution solves 2 problems for me, I can use it in classes that I’m not creating an instance of, and I can use it anywhere without having to inject the container into a bunch of classes.
The only downside I can think of is that I’m now potentially exposing my container to a bunch of classes that wouldn’t of had access to the container before. Not really sure if this is even a problem though?
Yes, this downsite is realy bad, and you should avoid it. In your case you can do something like this
It will be created only once for service when service host is created, so you should write following code
This is just inverted idea with static wrapper of the unity container 🙂