在WCF中工作时Publisher Subscriber模型中的异常

时间:2013-05-09 09:30:43

标签: c# .net wcf

我已经制作了一个WCF发布商订阅服务,每天处理70到80个连接 如果连接断开(由于互联网连接失败,系统关闭等) 当连接恢复时,服务本身将尝试建立连接。

有时候我注意到我的服务是抛出异常(操作超时)

This request operation sent to net.tcp://localhost:2001/sub did not receive a reply within the configured timeout (00:00:59.9799877). The time allotted to this operation may have been a portion of a longer timeout. This may be because the service is still processing the operation or because the service was unable to send a reply message. Please consider increasing the operation timeout (by casting the channel/proxy to IContextChannel and setting the OperationTimeout property) and ensure that the service is able to connect to the client

然后在其中一些例外情况之后,RM目的地超时异常即将出现,这使得与服务进一步通信的线路出现故障

RM目的地拒绝了创建可靠会话的请求。服务器 'net.tcp:// localhost:2001 / Sub'太忙,无法处理此请求。稍后再试。无法打开频道

在此期间,已订阅的客户端成功响应pub子请求

没有新频道订阅该服务

我在服务中使用net Tcp绑定

客户端代码

 public class Subscriber : IPublishing
{

    static int Counter = 0;
    ISubscription _proxy;
    string _endpoint = string.Empty;

    public Subscriber()
    {
        _endpoint = "net.tcp://localhost:2001/sub";
        MakeProxy(_endpoint, this);
    }
    void MakeProxy(string EndpoindAddress, object callbackinstance)
    {
        try
        {
            NetTcpBinding netTcpbinding = new NetTcpBinding(SecurityMode.None);
            EndpointAddress endpointAddress = new EndpointAddress(EndpoindAddress);
            InstanceContext context = new InstanceContext(callbackinstance);

            // Added for Reliable communication
            netTcpbinding.ReliableSession.Enabled = true;
            netTcpbinding.ReliableSession.Ordered = true;
            netTcpbinding.MaxBufferSize = 2147483647;
            netTcpbinding.MaxBufferPoolSize = 2147483647;
            netTcpbinding.MaxReceivedMessageSize = 2147483647;


            netTcpbinding.ReliableSession.InactivityTimeout = TimeSpan.MaxValue;
            netTcpbinding.ReceiveTimeout = TimeSpan.MaxValue;
            DuplexChannelFactory<ISubscription> channelFactory = new DuplexChannelFactory<ISubscription>(new InstanceContext(this), netTcpbinding, endpointAddress);

            _proxy = channelFactory.CreateChannel();



            Counter++;
        }
        catch (Exception ex)
        {
         //
        }
    }


    public void Subscribe()
    {
        try
        {
           _proxy.Subscribe("500");
           Counter = 0;
        }
        catch (Exception ex)
        {
            ((IContextChannel)_proxy).Abort();

        }
    }


    public void Publish(Message e, String ID)
    {

    }

    public void _Subscribe()
    {
        try
        {
            //OnUnSubscribe();
            Subscribe();
        }
        catch (Exception ex)
        {

        }
    }
}

服务器代码

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
class Subscription : ISubscription
{
    #region ISubscription Members

    public void Subscribe(string ID)
    {
        String s = DateTime.Now.ToString(
        IPublishing subscriber = OperationContext.Current.GetCallbackChannel<IPublishing>();
        Filter.AddSubscriber(KioskID, subscriber);
    }

    public void UnSubscribe(string ID)
    {
        IPublishing subscriber = OperationContext.Current.GetCallbackChannel<IPublishing>();
        Filter.RemoveSubscriber(KioskID, subscriber);
    }

    #endregion
}



class Filter
    {
        static Dictionary<string, List<IPublishing>> _subscribersList = new Dictionary<string, List<IPublishing>>();

        static public Dictionary<string, List<IPublishing>> SubscribersList
        {
            get
            {
                lock (typeof(Filter))
                {
                    return _subscribersList;
                }
            }

        }

        static public List<IPublishing> GetSubscribers(String topicName)
        {
            lock (typeof(Filter))
            {
                if (SubscribersList.ContainsKey(topicName))
                {
                    return SubscribersList[topicName];
                }
                else
                    return null;
            }
        }

        static public void AddSubscriber(String topicName, IPublishing subscriberCallbackReference)
        {
            DebugLog.WriteLog("C:\\DevPubSubServiceLogs", topicName + "came for Sub");
            lock (typeof(Filter))
            {
                DebugLog.WriteLog("C:\\DevPubSubServiceLogs",topicName +  "inside lock");
                if (SubscribersList.ContainsKey(topicName))
                {
                   // Removing any stray subscribers for  same topic name 
                   // because only 1 subscriber for 1 topic name should exist
                    SubscribersList.Remove(topicName);
                }

                List<IPublishing> newSubscribersList = new List<IPublishing>();
                newSubscribersList.Add(subscriberCallbackReference);
                SubscribersList.Add(topicName, newSubscribersList);


            }
            DebugLog.WriteLog("C:\\DevPubSubServiceLogs", topicName + "subscribed");

        }

        static public void RemoveSubscriber(String topicName, IPublishing subscriberCallbackReference)
        {
            lock (typeof(Filter))
            {
                if (SubscribersList.ContainsKey(topicName))
                {
                    //if (SubscribersList[topicName].Contains(subscriberCallbackReference))
                    //{
                    //    SubscribersList[topicName].Remove(subscriberCallbackReference);
                    //}
                    SubscribersList.Remove(topicName);
                }
            }
        }

    }

合同

[ServiceContract]
public interface IPublishing
{
    [OperationContract(IsOneWay = true)]
    void Publish(Message e, string ID);


}

[ServiceContract(CallbackContract = typeof(IPublishing))]
public interface ISubscription
{
    [OperationContract]
    void Subscribe(string topicName);

    [OperationContract]
    void UnSubscribe(string topicName);
}

请协助..

0 个答案:

没有答案