How to instantiate WCF host class with MEF
- Basic Request Response WCF service
- Reason behind a request – response service in WCF
- Evolving Request Response service to separate contract and business logic
I described in the last post of the series the structure behind the Request/Reponse service based on MEF, now it is time to explain how to make MEF and WCF happily live together. In the first version I hosted the service with these simple lines of code
Basically all I needed to do is to create a ServiceHost specifying in the constructor the type of the class that implements the service and let WCF to take care of every details about the creation of the concrete instances that will answer to the requests.
In this new version of the service the CoreService class cannot be anymore created with default constructor, because I need to construct the instance with MEF ; so I need to instruct WCF to create the CoreService class with MEF.
What I need is concrete implementation of the** IInstanceProvider **** , an interface used by WCF to manage the creation of concrete classes that implements my service **.
My implementation is super simple, I only need to use the** MefHelper.Create() method to let MEF create the class **and compose everything, but I need another couple of classes to instruct WCF to use my CoreServiceInstanceProvider to instantiate classes for my service. First class is a Service Behavior, represented by a class that** implements IServiceBehavior **** to make WCF use my CoreInstanceProvider **, then I create another class that** inherits from ServiceHost to automatically add this behavior to the WCF host**.
With these helper classes, hosting the service in a simple console application is a breeze.
Et voilà, two lines of code and I’m able to start the service where every instance of the service is created by MEF.