我有一个简单的WCF服务,可以向应用程序发送通知。 该服务包含一个静态的订阅者列表,在它尝试向其中任何一个发送消息之前,它会检查...
((ICommunicationObject)callback).State == CommunicationState.Opened
不幸的是,如果使用WCF服务的应用程序崩溃 - 因此不能从WCF服务取消订阅,该服务将会挂起并最终超时。远非理想。 为了对此进行排序,我添加了
OperationContext.Current.Channel.Faulted += new EventHandler(Channel_Faulted);
在订阅方法中,但从未被击中。 是否有一种可靠的方法来确定回调通道是否实际上是开放的? 或者也许是异步发送消息,这样如果订阅者出现在列表中但实际上已经死了,那么服务就不会像一个极客站出来一样等待? :)
任何最乐意接受的帮助
感谢
NAT
以下服务的完整代码......
private static readonly List<INotificationCallback> subscribers = new List<INotificationCallback>();
public void AddMessage(string SendingUser, string message, List<string> users, MessageType messageType, Guid? CaseID)
{
subscribers.ForEach(delegate(INotificationCallback callback)
{
if (((ICommunicationObject)callback).State == CommunicationState.Opened)
{
callback.OnMessageAdded(SendingUser, message, users, messageType, DateTime.Now, CaseID);
}
else
{
RemoveSubscriber(callback);
}
});
}
public bool Subscribe()
{
try
{
INotificationCallback callback = OperationContext.Current.GetCallbackChannel<INotificationCallback>();
OperationContext.Current.Channel.Faulted += new EventHandler(Channel_Faulted);
OperationContext.Current.Channel.Closed += new EventHandler(Channel_Closed);
if (!subscribers.Contains(callback))
subscribers.Add(callback);
return true;
}
catch
{
return false;
}
}
private void RemoveSubscriber(INotificationCallback callback)
{
if (subscribers.Contains(callback))
subscribers.Remove(callback);
}
void Channel_Closed(object sender, EventArgs e)
{
INotificationCallback machine = sender as INotificationCallback;
RemoveSubscriber(machine);
}
void Channel_Faulted(object sender, EventArgs e)
{
INotificationCallback machine = sender as INotificationCallback;
RemoveSubscriber(machine);
}
public bool Unsubscribe()
{
try
{
INotificationCallback callback = OperationContext.Current.GetCallbackChannel<INotificationCallback>();
RemoveSubscriber(callback);
return true;
}
catch
{
return false;
}
}
答案 0 :(得分:0)
最终放弃了HttpBinding并切换到NetTcpBinding,立即引发了故障事件。