Dailycode.info

Short solution for short problems

Generic WCF service factory. (In combination with generic singleton)

When you are working with WCF. You will find a need to have a factory that will initiate the services for you. To make it generic would be the best option, if your services do not differ to much from configuration implemtation. I will provide you with a class that will initiate any WCF service having its configuration in the config file or simply using the default configuration. It has a init function where you provide the endpoint configuration name.

public class ServiceFactory<T> where T : class

{

    private T _service;

    private ChannelFactory<T> fact;

    public T Service

    {

        get

        {

            if (fact.State != CommunicationState.Opened)

            {

                fact.Open();

            }

            _service = fact.CreateChannel();

            return _service;

        }

     

        set

        {

            _service = value;

        }

    }

    public void InitFactory(string endpoint)

    {

        if (_service == null)

        {

            fact = new ChannelFactory<T>(endpoint);

            _service = fact.CreateChannel();

         }

    }

} 

I used this in combination with a generic singleton, so I only initialize the service once and call the operations from the singleton:

 GenericSingleton<ServiceFactory<INurseryModuleService>>.GetInstance().InitFactory("MyEndPointConfig");

Then I just call the service operations like this:

GenericSingleton<ServiceFactory<INurseryModuleService >>.GetInstance().Service.TestNurseries(new TestNurseriesRequest());

 


MetaDataResolver not passing binding information

I was trying to get rid of the client configuration for binding and other stuff. One thing I thought of was the MetaDataResolver. This allows you to explore the endpoints from the client on the server if only you know the URL of the mex. So I started implementation of this and soon I got my endpoints. Looping over them and using them. Then suddenly when I do a call to one of the endpoints, I got a exception telling me that the maxReceivedMessageSize was not big enough.  I was positive that I set the maximum on the server configuration. So I looped over the bonding settings coming from the MetaDataResolver.Resolve. There I noticed that the binding information was not passed. After some intensive googling I found that some people said that this information is not passed because of security measurements. So these settings can differ on the server and client.

So now I find myself forced to use the client configuration.

Just passed the EndPoint configuration name to the channel factory and set all the configuration in the config file.

So now I didn’t need the metadataresolver. It can be of use when you want to explore the endpoints and take over the transferable configuration settings. The other settings will be defaulted on your client and you will eventually be forced to raiase the maxRecievedMessageSize.