[英]WCF right solution for Publish/subscribe pattern implementation
I implemented my own code of Publish/subscribe pattern implementation with WCF with WSDualHttpBinding, but i've a little problem with timeouts that i explain later, for now let me show what i'm doing: 我使用WSDualHttpBinding用WCF实现了自己的发布/订阅模式实现代码,但是超时有一个小问题,我稍后会解释,现在让我展示一下我在做什么:
public interface IEventSubscriber
{
[OperationContract]
void NotifyEvent(EventNotification notification);
[OperationContract]
void NotifyServiceDisconnecting();
}
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IEventSubscriber))]
public interface IEventPublisherService
{
[OperationContract(IsOneWay = false, IsInitiating = true)]
void Subscribe(string culture);
[OperationContract(IsOneWay = false, IsInitiating = false, IsTerminating = true)]
void Unsubscribe();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
internal class EventPublisherServiceImpl : IEventPublisherService
{
ServiceHost host;
public bool StartService()
{
bool ret = false;
try
{
Uri baseAddress = new Uri(ConfigurationManager.AppSettings[GlobalConstants.CfgKeyConfigEventPublishserServiceBaseAddress].ToString());
EventHelper.AddEvent(string.Format("Event Publisher Service on: {0}", baseAddress.ToString()));
host = new ServiceHost(this, baseAddress);
// duplex session enable http binding
WSDualHttpBinding httpBinding = new WSDualHttpBinding(WSDualHttpSecurityMode.None);
httpBinding.ReceiveTimeout = TimeSpan.FromMinutes(10);
httpBinding.ReliableSession = new ReliableSession();
httpBinding.ReliableSession.Ordered = true;
httpBinding.ReliableSession.InactivityTimeout = TimeSpan.FromMinutes(10);
host.AddServiceEndpoint(typeof(IEventPublisherService), httpBinding, baseAddress);
// Enable metadata publishing.
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
// Open the ServiceHost to start listening for messages.
host.Open();
ret = true;
}
catch (Exception e)
{
EventHelper.AddException(e.Message);
}
return ret;
}
...
}
now in my implementation class i have a list of subscribers that are stored in memory, when a new notification arrived the following code is performed for each subscriber: 现在在我的实现类中,我有一个存储在内存中的订阅者列表,当收到新通知时,将为每个订阅者执行以下代码:
...
/// <summary>
/// List of active subscribers
/// </summary>
private static Dictionary<IEventSubscriber, string> subscribers = new Dictionary<IEventSubscriber, string>();
...
that i use it like this: 我这样使用它:
internal void Subscribe(string culture)
{
lock (subscribers)
{
// Get callback contract as specified on the service definition
IEventSubscriber callback = OperationContext.Current.GetCallbackChannel<IEventSubscriber>();
// Add subscriber to the list of active subscribers
if (!subscribers.ContainsKey(callback))
{
subscribers.Add(callback, culture);
}
}
}
...
private void OnNotificationEvent(NormalEvent notification)
{
lock (subscribers)
{
List<IEventSubscriber> listToRemove = new List<IEventSubscriber>();
// Method signature: Parallel.ForEach(IEnumerable<TSource> source, Action<TSource> body)
Parallel.ForEach(subscribers, kvp =>
{
try
{
kvp.Key.NotifyEvent(new EventNotification(notification, kvp.Value));
}
catch (Exception ex)
{
EventHelper.AddException(string.Format("Error notifying event notification to client: {0} - removing this one", ex.Message));
listToRemove.Add(kvp.Key);
}
} //close lambda expression
); //close method invocation
Parallel.ForEach(listToRemove, subs =>
{
try
{
subs.NotifyServiceDisconnecting();
}
catch (Exception ex) {
EventHelper.AddException(string.Format("Failed to notify client that is to be removed: {0}",
ex.Message));
}
subscribers.Remove(subs);
}
);
}
}
What is the problem with this, when timeouts are achieved (note that i set 10 minutes for ReceiveTimeout and inactive timeout) the subscribers that are in the list go to fault state, and the following exception is catched in the OnNotificationEvent 这是什么问题,当达到超时(请注意,我为ReceiveTimeout和非活动超时设置了10分钟)时,列表中的订户将进入故障状态,并且在OnNotificationEvent中捕获以下异常
* The operation 'NotifyEvent' could not be completed because the sessionful channel timed out waiting to receive a message. * 操作'NotifyEvent'无法完成,因为会话通道超时等待接收消息。 To increase the timeout, either set the receiveTimeout property on the binding in your configuration file, or set the ReceiveTimeout property on the Binding directly.
要增加超时,请在配置文件中的绑定上设置receiveTimeout属性,或者直接在绑定上设置ReceiveTimeout属性。 *
*
Ok i can increase the timeout value, but if i do this it will happen some time, eventually. 好的,我可以增加超时值,但是如果我这样做,它将最终发生。
My question are: i'm doing something wrong when trying to implement this pattern? 我的问题是:尝试实施此模式时我做错了什么? or exists any other way of implementing this pattern a better way that avoid this problem?
还是存在其他实现此模式的更好方法来避免此问题? or exist any way of reconnecting the faulted callback channel (for what i'm reading it's not possible, but due to that i can't notify the client that is connection was lost, letting the client blind not knowing that the communication ended!? or is a way to give knowledge that he lost communication with the publisher!?)
或以任何方式重新连接故障的回调通道(对于我正在读取的内容是不可能的,但是由于该原因,我无法通知客户端已断开连接,从而使客户端不知道通信已结束!!还是一种得知他与发布者失去联系的方式!?)
Of course solution like ping messages are out of date :) but ok, if nothing better appears look like i've to implement something like that... 当然像ping消息这样的解决方案已经过时了:)但是好吧,如果没有更好的显示,看起来我必须实现类似的东西...
Thanks 谢谢
For now the solution was to change the timeouts to have a infinite value: 目前,解决方案是将超时值更改为无穷大:
// duplex session enable http binding
WSDualHttpBinding httpBinding = new WSDualHttpBinding(WSDualHttpSecurityMode.None);
httpBinding.ReceiveTimeout = TimeSpan.MaxValue;
httpBinding.ReliableSession = new ReliableSession();
httpBinding.ReliableSession.Ordered = true;
httpBinding.ReliableSession.InactivityTimeout = TimeSpan.MaxValue;
You are using Parallel.ForEach but I'm not sure that this is enough. 您正在使用Parallel.ForEach,但我不确定是否足够。 AFAIR Parallel.ForEach does not execute each iteration in the separate thread.
AFAIR Parallel.ForEach不会在单独的线程中执行每次迭代。
I would like to suggest to start separate threads for each subscriber in the OnNotificationEvent and use lock to make sure that foreach will not be breaked by Collection modified exception: 我想建议为OnNotificationEvent中的每个订户启动单独的线程,并使用锁来确保foreach不会被Collection修改的异常打破:
lock (_subscribersSync)
foreach (var chatter in subscribers)
{
Logger.Log.DebugFormat("putting all users to {0}", subscribers.Key.Name);
Thread th = new Thread(PublishAllUserMessage);
th.Start(new MessageData() { Message = "", Subscriber = chatter.Key};
}
void PublishAllUserMessage(object messageData)
{
MessageData md = (MessageData)messageData;
try
{
md.Subscriber.NotifyEvent(...event parameters here ...);
}
catch (Exception ex)
{
Logger.Log.Error(string.Format("failed to publish message to '{0}'", md.Subscriber.Name), ex);
KickOff(md.Subscriber);
}
}
object _subscribersSync = new object();
void KickOff(IEventSubscriber p)
{
lock (_subscribersSync)
{
subscribers.Remove(p);
Logger.Log.WarnFormat("'{0}' kicked off", p.Name);
}
}
public class MessageData
{
public string Message;
public IEventSubscriber Subscriber;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.