NetMessagingBinding和并发 [英] NetMessagingBinding and Concurrency

查看:81
本文介绍了NetMessagingBinding和并发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我们有一个带有NetMessagingBinding终结点的WCF服务,该服务似乎多次处理同一条消息.我们100%确保不会将同一则消息推送两次或多次.

服务设置如下:

公共类服务:IService
{
        公共无效推(MyType x)
        {
            var传入的属性= OperationContext.Current.IncomingMessageProperties;

            ReceiveContext receiveContext;
            如果(ReceiveContext.TryGet(incomingProperties,超出receiveContext))
            {
                尝试
                {
                    AddToDb(x);

                    receiveContext.Complete(TimeSpan.FromSeconds(ReceiveContextOperationTimeout));

                }
                抓住(前例外)
                {
                    receiveContext.Abandon(例如,TimeSpan.FromSeconds(ReceiveContextOperationTimeout));
                }
            }
            别的
            {
                字符串消息= string.Format(NoReceiveContextMessage,"blah");

                _logger.ErrorFormat(message);
                抛出新的InvalidOperationException(message);
            }
        }
} 

我们正在使用默认的并发模式.在MSDN中,它们是InstanceContextMode.PerSession和ConcurrencyMode.Single.

如果我们设置ConcurrencyMode.Single和InstanceContextMode.Single,则WCF服务不会处理消息多次.

我们可以在代码中修复某些问题吗?

预先感谢!

解决方案

设置  属性  和  属性 . 这样 每当指定的队列或订阅中有新会话可用时,都会创建一个新服务实例,并且 ServiceHost   uses 一个线程从队列或订阅中按顺序接收消息.

有关更多信息,请尝试参考:
http://msdn.microsoft.com/zh-cn/library/windowsazure/hh532034.aspx .

希望它可以为您提供帮助.

最好的问候.


Currently we have a WCF Service with a NetMessagingBinding endpoint that seems to be processing the same message more than once. We are 100% sure that we are not pushing the same message twice or more times.

The service is set up as follows:

public class Service : IService
{
        public void Push(MyType x)
        {
            var incomingProperties = OperationContext.Current.IncomingMessageProperties;

            ReceiveContext receiveContext;
            if (ReceiveContext.TryGet(incomingProperties, out receiveContext))
            {
                try
                {
                    AddToDb(x);

                    receiveContext.Complete(TimeSpan.FromSeconds(ReceiveContextOperationTimeout));

                }
                catch (Exception ex)
                {
                    receiveContext.Abandon(ex, TimeSpan.FromSeconds(ReceiveContextOperationTimeout));
                }
            }
            else
            {
                string message = string.Format(NoReceiveContextMessage, "blah");

                _logger.ErrorFormat(message);
                throw new InvalidOperationException(message);
            }
        }
}

We are using the default concurrency modes. From MSDN they are InstanceContextMode.PerSession and ConcurrencyMode.Single. 

If we set ConcurrencyMode.Single and InstanceContextMode.Single then the WCF Service doesn't process messages more than once.

Is there something that we can fix in the code?

Thanks in advance!

解决方案

Hi,

When sets the ServiceBehavior.InstanceContextMode property to InstanceContextMode.PerSession and the ConcurrencyMode property toConcurrencyMode.Single. This way the ServiceHost creates a new service instance every time a new session is available in the specified queue or subscription, and the ServiceHost uses a single thread to receive messages sequentially from the queue or subscription.

[ServiceContract]
public interface IService
{
    [OperationContract(IsOneWay = true)]
    [ReceiveContextEnabled(ManualControl = true)]
    void Push(MyType x);
}



[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Single)]
public class Service : IService
{
   
     public void Push(MyType x)
        {
            var incomingProperties = OperationContext.Current.IncomingMessageProperties;

            ReceiveContext receiveContext;
            if (ReceiveContext.TryGet(incomingProperties, out receiveContext))
            {
                try
                {
                    AddToDb(x);

                    receiveContext.Complete(TimeSpan.FromSeconds(ReceiveContextOperationTimeout));

                }
                catch (Exception ex)
                {
                    receiveContext.Abandon(ex, TimeSpan.FromSeconds(ReceiveContextOperationTimeout));
                }
            }
            else
            {
                string message = string.Format(NoReceiveContextMessage, "blah");

                _logger.ErrorFormat(message);
                throw new InvalidOperationException(message);
            }
        }
}

For more information, please try to refer to:
http://msdn.microsoft.com/zh-cn/library/windowsazure/hh532034.aspx .

Hope it can help you.

Best Regards.


这篇关于NetMessagingBinding和并发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆