Soo*_*nts 16 c# wcf .net-4.0 net.tcp
我们正在构建一个WCF服务器(.NET 4.0).它只会使用net.tcp传输.
当客户端关闭TCP连接时,服务器将获取未处理的CommunicationException,并终止.
Q1.如何处理CommunicationException以便服务器不会终止并继续为其他客户端提供服务?
Q2.在处理程序中,如何获取已中止的会话的SessionId?我需要这个来清理一些特定于会话的数据.
提前致谢!
PS连接是通过Internet连接的,因此无论客户端是否正常断开连接,都可以随时关闭套接字.
Eil*_*aee 23
任何WCF通道都实现ICommunicationObject,它为通道生命周期提供事件.
你应该听听Faulted事件
可以从OperationContext.Current属性一直访问sessionId .
当您的客户打开频道时(在第一次操作时),注册适当的事件:
OperationContext.Current.Channel.Faulted += new EventHandler(Channel_Faulted);
OperationContext.Current.Channel.Closed += new EventHandler(Channel_Faulted);
Run Code Online (Sandbox Code Playgroud)
和:
void Channel_Faulted(object sender, EventArgs e)
{
Logout((IContextChannel)sender);
}
protected void Logout(IContextChannel channel)
{
string sessionId = null;
if (channel != null)
{
sessionId = channel.SessionId;
}
[...]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14512 次 |
| 最近记录: |