3
votes

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.

2

2 Answers

1
votes

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).

0
votes

WCF services default to a per call instance mode. This means that a new instance of your WCF service is instantiated for each incoming method invocation. It sounds like what you're wanting is a singleton instance mode, but you really want to avoid this if scability is an issue.

The way I've gotten around this is to use the per call instance mode, but have a static data store behind the scenes that I synchronize access to. This at least allows clients to connect, even if they have to block momentarily while the data store is in use once the connection is established.

Refer to the MSDN help on System.ServiceModel.InstanceContextMode for more details.