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.
One way to do this is using the GetExports method of CompositionContainer. It returns an IEnumerable<Lazy<Object, Object>>. If it contains at least one element then the "serviceType" is available.
So the check can be:
if (container.GetExports(serviceType, null, null).Any())
{
return new MyServiceHost(container, serviceType, baseAddresses);
}
Then a way to get the exported service can be:
Object seviceObj = container.GetExports(serviceType, null, null).First().Value;
The problem now is that value is of type System.Object can you will have to cast it dynamically or use the dynamic keyword (and loose all the nice help of the compiler).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With