铸造WCFClient(或使用通用集合?)

时间:2010-11-23 23:02:26

标签: c# web-services

我有多个wcfClients(由Web引用设计),它们都实现了自己的接口,而这些接口又继承了另一个接口。

我想从所有Web服务都继承的接口调用方法,所以不要这样做......

case "DVSSync":
                        DVSSync.WcfDVSSyncClient dvsSyncClient = new DVSSync.WcfDVSSyncClient("BasicHttpBinding_IWcfDVSSync1");

                        dataRow["URI"] = dvsSyncClient.Endpoint.Address.ToString();
                        dataRow["ServiceUptime"] = dvsSyncClient.ServiceUptime();
                        dataRow["Version"] = dvsSyncClient.Version();

                        dvsSyncClient.Close();
                        break;

                    case "DataInserter":
                        DataInserter.WcfDataInserterClient dataInserterClient = new DataInserter.WcfDataInserterClient("BasicHttpBinding_IWcfDataInserter1");

                        dataRow["URI"] = dataInserterClient.Endpoint.Address.ToString();
                        dataRow["ServiceUptime"] = dataInserterClient.ServiceUptime();
                        dataRow["Version"] = dataInserterClient.Version();

                        dataInserterClient.Close();
                        break;

我想做类似

的事情
 switch (service)
        {
        case "DVSSync":
                                    DVSSync.WcfDVSSyncClient dvsSyncClient = new DVSSync.WcfDVSSyncClient("BasicHttpBinding_IWcfDVSSync1");

    GenericClient wcfClient = (GenericClient)dvsSyncClient; 


                                    break;

                                case "DataInserter":
                                    DataInserter.WcfDataInserterClient dataInserterClient = new DataInserter.WcfDataInserterClient("BasicHttpBinding_IWcfDataInserter1");

   GenericClient wcfClient = (GenericClient)dataInserterClient ; 

                                    break;

        }

                                    dataRow["URI"] = wcfClient.Endpoint.Address.ToString();
                                    dataRow["ServiceUptime"] = wcfClient.ServiceUptime();
                                    dataRow["Version"] = wcfClient.Version();

                                    wcfClient.Close();

谢谢!

1 个答案:

答案 0 :(得分:1)

这样的事情:

void Foo()
{
   GenericClient client = CreateClient(service);
  //do stuff with generic client
}

GenericClient CreateClient(string service)
{
  switch(service)
  {
     case "DVSSync":
       return new WcfDVSSyncClient()
     //etc
  }
}