运行时决定选择特定的WCF客户端

时间:2013-12-06 18:19:37

标签: c# performance asp.net-mvc-3 wcf wcf-client

我目前的项目中有这种情况。尝试我的水平最好解释。 如果您有任何问题,请告诉我。

 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;
        }
    }

注意事项:

  1. IDHServiceClient,CommonServiceClient& IPAServiceClient都在各自的svc文件中实现IContractsService,因此所有客户端都有相同的公开方法。但不同的命名空间。

  2. 像CancelService这样的方法分散在整个项目中,因此需要对ServiceClient属性进行更改以返回正确的客户端。

  3. 问题==>需要对ServiceClient属性进行更改以返回正确的客户端。

    好处==>我不必更改代码ServiceClient.CancelService和此类调用(遍及代码)

    我该怎么做?

0 个答案:

没有答案