【问题标题】:WCF service Subscribers not raising Faulted eventWCF 服务订阅者未引发故障事件
【发布时间】:2011-09-10 19:16:38
【问题描述】:

我有一个简单的 WCF 服务,用于提供与应用之间的通知。 该服务拥有一个静态订阅者列表,在尝试向其中任何一个发送消息之前,它会检查...

((ICommunicationObject)callback).State == CommunicationState.Opened

不幸的是,如果使用 WCF 服务的应用程序发生故障 - 因此没有从 WCF 服务取消订阅,该服务将挂起并最终超时。远非理想。 为了对此进行排序,我添加了

OperationContext.Current.Channel.Faulted += new EventHandler(Channel_Faulted);

在订阅方法中,但它永远不会被命中。 是否有可靠的方法来确定回调通道是否实际上是打开的? 或者也许异步发送消息,以便如果订阅者出现在列表中但实际上已经死了,服务不会像极客一样等待约会? :)

任何最高兴得到的帮助

谢谢

自然

以下服务的完整代码...

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;
        }
    }

【问题讨论】:

    标签: wcf callback


    【解决方案1】:

    最终放弃了 HttpBinding 并切换到 NetTcpBinding 立即引发错误事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-04
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      相关资源
      最近更新 更多