Runtime Decision to choose a specific WCF client

99 views Asked by At

I have this Kind of scenario in my current Project. Trying my level best to explain. Let me know if you have any questions.

 public bool CancelService(int serviceId, int userID)
    {
        var serviceResponse = ServiceClient.CancelService(serviceId, userID);
    }


//This Service call is throwing casting issues 
    private IContractsService ServiceClient
    {
        get
        {
            if (ContractType == CviValues.ContractTypeIDH)
            {
                return (Presentation.CommonService.IContractsService)IDHServiceClient;
            }
            else if (ContractType == CviValues.ContractTypeIpa)
            {
                return (Presentation.CommonService.IContractsService)IPAServiceClient;
            }
            else
            {
                return (Presentation.CommonService.IContractsService)CommonServiceClient;
            }
        }
    }

    private Presentation.IDHService.ContractsServiceClient _iDHServiceClient;
    private Presentation.IDHService.ContractsServiceClient IDHServiceClient
    {
        get
        {
            if (_iDHServiceClient == null)
            {
                _iDHServiceClient = new Presentation.IDHService.ContractsServiceClient(
                    new BasicHttpBinding(BasicHttpSecurityMode.None) { MaxReceivedMessageSize = Int32.MaxValue, OpenTimeout = new TimeSpan(0, 2, 0), ReceiveTimeout = new TimeSpan(0, 10, 0), SendTimeout = new TimeSpan(0, 2, 0) },
                    new EndpointAddress(ValueUtilities.IDHContractsServiceEndpointUrl));
            }
            return _iDHServiceClient;
        }
    }

    private Presentation.CommonService.ContractsServiceClient _commonServiceClient;
    private Presentation.CommonService.ContractsServiceClient CommonServiceClient
    {
        get
        {
            if (_commonServiceClient == null)
            {
                _commonServiceClient = new Presentation.CommonService.ContractsServiceClient(
                    new BasicHttpBinding(BasicHttpSecurityMode.None) { MaxReceivedMessageSize = Int32.MaxValue, OpenTimeout = new TimeSpan(0, 2, 0), ReceiveTimeout = new TimeSpan(0, 10, 0), SendTimeout = new TimeSpan(0, 2, 0) },
                    new EndpointAddress(ValueUtilities.CommonContractsServiceEndpointUrl));
            }
            return _commonServiceClient;
        }
    }

    private Presentation.IPAService.ContractsServiceClient _iPAServiceClient;
    private Presentation.IPAService.ContractsServiceClient IPAServiceClient
    {
        get
        {
            if (_iPAServiceClient == null)
            {
                _iPAServiceClient = new Presentation.IPAService.ContractsServiceClient(
                    new BasicHttpBinding(BasicHttpSecurityMode.None) { MaxReceivedMessageSize = Int32.MaxValue, OpenTimeout = new TimeSpan(0, 2, 0), ReceiveTimeout = new TimeSpan(0, 10, 0), SendTimeout = new TimeSpan(0, 2, 0) },
                    new EndpointAddress(ValueUtilities.IPAContractsServiceEndpointUrl));
            }
            return _iPAServiceClient;
        }
    }

Points to note:

  1. IDHServiceClient,CommonServiceClient & IPAServiceClient all implements IContractsService in their respective svc Files so all clients have same exposed methods. Different Namespaces though.

  2. Methods like CancelService are scattered all over the project and hence the need is to make a change to ServiceClient property to return the right client.

Problem ==> The need is to make a change to ServiceClient property to return the right client.

Benefits ==> I don't have to change the code ServiceClient.CancelService and such calls (which are all over the code )

How can I do that?

0

There are 0 answers