C# 强制转换WCFClient(或使用常规集合?)
我有多个WCFClient(根据web引用设计),它们都实现了自己的接口,而这些接口又继承了另一个接口 我想从所有web服务继承的接口调用这些方法,所以不要这样做C# 强制转换WCFClient(或使用常规集合?),c#,web-services,C#,Web Services,我有多个WCFClient(根据web引用设计),它们都实现了自己的接口,而这些接口又继承了另一个接口 我想从所有web服务继承的接口调用这些方法,所以不要这样做 case "DVSSync": DVSSync.WcfDVSSyncClient dvsSyncClient = new DVSSync.WcfDVSSyncClient("BasicHttpBinding_IWcfDVSSync1");
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();
谢谢 像这样的事情怎么样:
void Foo()
{
GenericClient client = CreateClient(service);
//do stuff with generic client
}
GenericClient CreateClient(string service)
{
switch(service)
{
case "DVSSync":
return new WcfDVSSyncClient()
//etc
}
}