English 中文(简体)
WCF Publish / Subscribe: How to handle client side timeout so as not to miss information?
原标题:

I have a simple WCF publish/subscribe up and running, based on this example. I am using netTcpBinding with reliableSession enabled. Everything works fine with the functionality (the subscribed clients receive the published data as expected), but at some point the connection times out if it has been idle for a while. I can set up the publisher to reconnect on timeout, but the subscribed clients will be lost. Is there a way to get them back? I would prefer not to just increase the timeouts, as that could cause other problems.

最佳回答

The solution I eventually came up with was to assign a unique identifier to every message that was published, and cache the published message in the service adapter (in the same place where I was storing the callbacks to the subscribed clients. Whenever I published the message, subscribers would receive the message and the corresponding unique id. Subscribers could then use the channel.Faulted event to reconnect and resubscribe to the service with a special method that takes the last received message id as a parameter.

Service code:

    /// <summary>
    /// Operation used by the subscriber to subscribe to events published.
    /// </summary>
    public void Resubscribe(int lastReceivedMessageId)
    {
        // Get callback contract
        IPubSubCallback callback = OperationContext.Current.GetCallbackChannel<IPubSubCallback>();
        ThreadPool.QueueUserWorkItem(delegate(object state)
        {
            adapter.Resubscribe(lastReceivedMessageId, callback);
        }); 
    }

Adapter code:

/// <summary>
    /// Operation used by the subscriber to resubscribe to events published.
    /// </summary>
    public void Resubscribe(int lastReceivedMessageId, IPubSubCallback callback)
    {
        try
        {
            // Send the subscriber any missed messages
            foreach (KeyValuePair<int, string> missedMessage in publishedMessages.Where(x => x.Key > lastReceivedMessageId))
            {
                callback.MessagePublished(missedMessage.Value, missedMessage.Key);
            }

            // Add the subscriber callback to the list of active subscribers
            if (!callbacks.Contains(callback))
            {
                callbacks.Add(callback);
            }
        }
        catch
        {
            // ignore subscription, callbacks failed again
        }
    }

The service can then work out what the client has missed, and resend those messages in the correct order.

This solution seems to be working well for me, but I have a feeling that there must be a better way to do this. Comments / additional answers are very welcome! :)

问题回答

暂无回答




相关问题
WCF DataMember Serializing questions

Ok, so I was part way through the long winded process of creating DTOs for sending my model over the wire and I don t feel like I m going down the right route. My issue is that most of the entities ...

Access WCF service on same server

I have a .NET website with a WCF service. How do I access the current operations context of my service? One possible work around is to just make a call to the service within the app...but that seems ...

WCF binding error

So I got into work early today and got the latest from source control. When I try to launch our ASP.NET application, I get this exception: "The binding at system.serviceModel/bindings/wsHttpBinding ...

The service operation requires a transaction to be flowed

I am facing strange issue with our WCF service. The same code was working fine until recently we added more OperationContracts(Web Methods). We have common 3 tier architecture. DAL (WCF) BLL Web ...

热门标签