I am trying to use fluent-configured MEF to boostrap my WCF service with custom constructor.
How do I check if MEF container provides the "serviceType". E.g.:
public class MyServiceHostFactory : ServiceHostFactory
{
private readonly CompositionContainer container;
public MyServiceHostFactory()
{
this.container = MyCompositionRoot.Instance.Container;
}
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
if (serviceType == ????)
{
return new MyServiceHost(container, serviceType, baseAddresses);
}
return base.CreateServiceHost(serviceType, baseAddresses);
}
}
and then I need to add an instance provider to my behaviors:
public MyServiceHost(CompositionContainer container, Type serviceType,
params Uri[] baseAddresses) : base(serviceType, baseAddresses)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
var contracts = this.ImplementedContracts.Values;
foreach (var c in contracts)
{
// Should I get the service obj here?
var serviceObj = container.GetExports(serviceType, null, null).First().Value;
var instanceProvider = new MyInstanceProvider(serviceObj); // ????
c.Behaviors.Add(instanceProvider);
}
}
but I am not sure how this instance provider should look (should it take a serviceType as a parameter or the serviceObj?
public partial class MyInstanceProvider : IInstanceProvider, IContractBehavior
Because in the book (DI Injection in .NET) the example uses a strongly-coupled instance provider, i.e. MyServiceType1InstanceProvider, MyServiceType2InstanceProvider - but that gets tedious if I have many services hooked up with fluent mef.