开发者

Casting a WCFClient (or using a generic collection?)

I have multiple wcfClients (designed from web references) which all implement their own interfaces, which in turn all inherit another interface.

I want to call the methods from the interface in which all web services have inherited, so instead of doing this...

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

                        dataRow["URI"] = dvsSyncClient.Endpoint.Address.ToString();
                        dataRow["ServiceUptime"] = dvsSyncClient.ServiceUptime();
                        dataRow["Versio开发者_高级运维n"] = 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;

I want to do something similar to

 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();

Thanks!


What about something like this:

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜