开发者

WCF service Subscribers not raising Faulted event

I have a simple WCF service serving up notifications to and from an app. the service holds a static list of subscribers, and before it tries to send a message to any of them, it checks ...

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

unfortunately if the app consuming the WCF service falls over - thus not unsubscribing itself from the WCF service, the service will hang around and eventually timeout. far from ideal. in an attempt to sort this I added

OperationContext.Current.Channel.Faulted += new 开发者_如何学GoEventHandler(Channel_Faulted);

in the subscription method, but it is never hit. is there a reliable way to determine whether or not the callback channel is in fact open? or maybe to asynchronously send the messages so that if a subscriber appears in the list but is actually dead, the service wont wait around like a geek being stood up on a date? :)

any help most happily received

thanks

nat

full code for service below...

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


ended up ditching the HttpBinding and switched to NetTcpBinding which raises the the fault event immediately.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜