C# WCF通道工厂浇铸接口至IClientChannel悬浮浇铸

C# WCF通道工厂浇铸接口至IClientChannel悬浮浇铸,c#,wcf,dispose,channel,channelfactory,C#,Wcf,Dispose,Channel,Channelfactory,使用自定义接口的WCF ChannelFactory CreateChannel方法- ChannelFactory<MyServiceInterface> myFactory= new ChannelFactory<MyServiceInterface>(binding,endpoint); MyServiceInterface clientInterface = myFactory.CreateChannel(); 所以我试图将我的频道播送到IClientCha

使用自定义接口的WCF ChannelFactory CreateChannel方法-

ChannelFactory<MyServiceInterface> myFactory= 
new ChannelFactory<MyServiceInterface>(binding,endpoint);

MyServiceInterface clientInterface = myFactory.CreateChannel();
所以我试图将我的频道播送到IClientChannel(或者IChannel),但VS警告我- 可疑强制转换-解决方案中没有同时继承MyServiceInterface和System.ServiceModel.IClientChannel的类型

我的印象是,工厂返回的代理自动实现IClientChannel。我错了吗?这个警告是关于什么的?我该把我的频道投给什么?也许我的ProperlyDisposeChannel方法应该接受IClientChannel而不是ICommunicationObject(我更喜欢ICommuniationObject,因为它也适用于其他对象)

所以我试着用下面这句话来警告我-

ProperlyDisposeChannel((IChannel)clientInterface);

ProperlyDisposeChannel((IClientChannel)客户端界面)

尽管收到可疑警告,但您可以执行显式强制转换。实际上,CreateChannel()签名返回服务的接口类型,但也继承了wood下的IChannel接口

这是有意义的,因为该方法显然不能返回2种类型,从而允许直接使用您的服务


这是一个实现选择,它可能已经返回,比方说,
公共接口IChannel:IChannel{T Instance{get;}}

您可以使用
ICommunicationObject
执行关闭或中止-我一直都在这样做

只需将您的呼叫更改为:

ProperlyDisposeChannel((ICommunicationObject)clientInterface);

它将按预期工作。

-says返回工厂创建的IChannel类型的TChannel。因此,不确定有什么可疑之处…您将在哪里强制转换到IClientChannel?以关闭-调用PropertyDisposeChannel((IChannel/IClientChannel)clientInterface)。我也会让文章更清楚
解决方案中没有继承MyServiceInterface和System.ServiceModel.IClientChannel的类型,那么这不是真的吗?您的解决方案中是否存在这样的类型?C#编译器对WCF一无所知。WCF和其他任何库一样都是一个库。@MikeTurner你看到我的评论了吗?您在此页面上与其他人进行了互动,因此您可能错过了我的问题。谢谢。这给了我与使用相同的可疑施法警告IChannel@MikeTurner-那太奇怪了。我们有一个在层之间使用WCF的n层应用程序,我们使用完全相同的模式,我从来没有看到过这个警告。
客户端界面的类型是什么?它是MyServiceInterface吗?你能发布
MyServiceInterface
的代码吗?MyServiceInterface-[ServiceContract]公共接口MyServiceInterface{[OperationContract methods…}只是一个普通的接口,没有专门实现ICommunicationObject或IChannel…我需要吗?查看SOS评论中的链接-看起来返回的对象自动实现IChannel您不需要在合同中实现接口-WCF框架在创建通道时会为您实现。
IChannel
实现了
ICommunicationObject
,因此强制转换应该是合法的。也许您的代码中还存在一些不明显的问题?
ProperlyDisposeChannel((ICommunicationObject)clientInterface);