I would like to pass values into the constructor on the class that implements my service.
However ServiceHost only lets me pass in the name of the type to create, not what arguments to pass to its contrstructor.
I would like to be able to pass in a factory that creates my service object.
What I have found so far:
- WCF Dependency Injection Behavior which is more than what I am looking for and seems to be over-complex for my needs.
Mark's answer with the
IInstanceProvider
is correct.Instead of using the custom ServiceHostFactory you could also use a custom attribute (say
MyInstanceProviderBehaviorAttribute
). Derive it fromAttribute
, make it implementIServiceBehavior
and implement theIServiceBehavior.ApplyDispatchBehavior
method likeThen, apply the attribute to your service implementation class
The third option: you can also apply a service behavior using the configuration file.
I worked from Mark's answer, but (for my scenario at least), it was needlessly complex. One of the
ServiceHost
constructors accepts an instance of the service, which you can pass in directly from theServiceHostFactory
implementation.To piggyback off Mark's example, it would look like this: