Reputation: 156524
In our ASP.NET project we have a session manager service interface that we use to find out information about the current user. For example:
var actorId = _sessionManager.CurrentActivePerson.PersonId;
This session manager is provided to classes via constructor-based dependency injection.
Now, we have a WCF service that we would like to instantiate with different DI bindings depending on the situation:
In order for this to work properly, we need to allow the consumer to provide a domain ID as an argument, although it is not specified as an argument of the service itself. We then need to be able to access this argument from a DI binding method while the HostFactory is creating the service instance.
Does anybody have any suggestions on how we might do this?
Notes:
Upvotes: 1
Views: 408
Reputation: 233150
This sounds to me to be an authentication/authorization issue more than a DI issue. If that is the case, the most correct low-level solution would actually be to implement a custom token.
That's a pretty complex task, so you should seriously consider switching to claims-based authorization. The Windows Identity Foundation provides a lot of tools to do this.
This would mean that instead of having to manage two fundamentally different security contexts (user name vs. domain id) you externalize the identity management so that you don't have to deal with such a cross-cutting concern inside the application code. This would give you a better separation of concerns.
However, if you truly need to defer the resolution of a dependency until the actual method call, the universal solution is to inject an abstract factory.
Just in case you need information about how to enable Constructor Injection for WCF, here's how: Injecting data to a WCF service
Upvotes: 2