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.
You can simply create and instance of your
Service
and pass that instance to theServiceHost
object. The only thing you have to do is to add a[ServiceBehaviour]
attribute for your service and mark all returned objects with[DataContract]
attribute.Here is a mock up:
and the usage:
I hope this will make life easier for someone.
Screw it… I blended the dependency injection and service locator patterns (but mostly it's still dependency injection and it even takes place in the constructor which means you can have read-only state).
The dependencies of the service are clearly specified in the contract of it's nested
Dependencies
class. If you are using an IoC container (one that doesn't already fix the WCF mess for you), you can configure it to create theDependencies
instance instead of the service. This way you get the warm fuzzy feeling that your container gives you while also not having to jump through too many hoops imposed by WCF.I'm not going to lose any sleep over this approach. Neither should anyone else. After all, you're IoC container is a big, fat, static collection of delegates that creates stuff for you. What's adding one more?
I use static variables of my type. Not sure if this is the best way, but it works for me:
When I instantiate service host I do the following:
You'll need to implement a combination of custom
ServiceHostFactory
,ServiceHost
andIInstanceProvider
.Given a service with this constructor signature:
Here's an example that can spin up MyService:
Register MyServiceHostFactory in your MyService.svc file, or use MyServiceHost directly in code for self-hosting scenarios.
You can easily generalize this approach, and in fact some DI Containers have already done this for you (cue: Windsor's WCF Facility).
We were facing this same problem and have solved it in the following manner. It is a simple solution.
In Visual Studio just create a normal WCF service application and remove it's interface. Leave the .cs file in place (just rename it) and open that cs file and replace the name of the interface with your original class name which implements the service logic (this way the service class uses inheritance and replaces your actual implementation). Add a default constructor that calls the base class's constructors, like this:
The MyService base class is the actual implementation of the service. This base class should not have a parameterless constructor, but only constructors with parameters that accept the dependencies.
The service should use this class instead of the original MyService.
It's a simple solution and works like a charm :-D
This was a very helpful solution - especially for someone who is a novice WCF coder. I did want to post a little tip for any users who might be using this for an IIS-hosted service. MyServiceHost needs to inherit WebServiceHost, not just ServiceHost.
This will create all the necessary bindings, etc for your endpoints in IIS.