【发布时间】:2016-12-04 10:01:27
【问题描述】:
使用我的自定义界面的 WCF ChannelFactory CreateChannel 方法 -
ChannelFactory<MyServiceInterface> myFactory=
new ChannelFactory<MyServiceInterface>(binding,endpoint);
MyServiceInterface clientInterface = myFactory.CreateChannel();
在网上阅读了很多,看起来我想正确关闭我的频道 -
private void ProperlyDisposeChannel(ICommunicationObject comObj)
{
bool success = false;
if (comObj == null || comObj.State == CommunicationState.Closed)
{
return;
}
try
{
if (comObj.State != CommunicationState.Faulted)
{
comObj.Close();
success = true;
}
}
catch (Exception e)
{
//optionally log exception
}
finally
{
if (!success)
{
try
{
comObj.Abort();
}
catch (Exception e)
{
//do not retry to abort, optionally log
}
}
}
}
所以我试图将我的频道投射到 IClientChannel(或者可能是 IChannel),但 VS 警告我 - 可疑演员表 - 解决方案中没有类型同时继承 MyServiceInterface 和 System.ServiceModel.IClientChannel
我的印象是工厂返回的代理自动实现了IClientChannel。我错了吗?这个警告是关于什么的?我应该将频道投射到什么位置?也许我的 ProperlyDisposeChannel 方法应该接受 IClientChannel 而不是 ICommunicationObject(我更喜欢 ICOmmuminationObject 因为它也适用于其他对象)
所以我正在尝试以下行,这给了我警告 -
ProperlyDisposeChannel((IChannel)clientInterface);
或ProperlyDisposeChannel((IClientChannel)clientInterface);
【问题讨论】:
-
msdn.microsoft.com/en-us/library/ms575250(v=vs.110).aspx - 表示返回工厂创建的 IChannel 类型的 TChannel。所以不确定什么是可疑的......
-
你在哪里投射到 IClientChannel?
-
为了关闭 - 调用 PropertyDisposeChannel((IChannel/IClientChannel)clientInterface)。也会让帖子更清晰
-
there is no type in the solution that inherits both MyServiceInterface and System.ServiceModel.IClientChannel这不是真的吗?您的解决方案中是否存在这种类型? C# 编译器对 WCF 一无所知。 WCF 只是一个与其他库一样的库。 -
@MikeTurner 你看到我的评论了吗?您在此页面上与其他人进行了互动,因此您可能错过了我的问题。
标签: c# wcf dispose channel channelfactory