【问题标题】:How can I throttle a WCF MSMQ endpoint?如何限制 WCF MSMQ 终结点?
【发布时间】:2012-12-31 22:43:35
【问题描述】:

我是 WCF 新手,刚刚学习如何使用 MSMQ 让客户端与主机(都在控制台应用程序中)对话。

我希望能够从客户端向主机发送消息并让主机立即接收它们,或者,如果主机停止,则在重新启动时从中断处继续。

我几乎可以正常工作,但我发现当我重新启动主机时队列中有十条消息时,消息没有按队列顺序处理。我认为有一些多线程正在进行,使它们出现乱序。我希望能够将 WCF 服务限制为一次处理一条消息以阻止这种情况发生(除非有更好的解决方案)。

对于我将要处理的系统而言,MSMQ 消息按顺序而不是并行处理是必不可少的。

我的服务合同的代码是:

[ServiceContract(Namespace = "http://www.heronfoods.com/DemoService")]
public interface IDemoService
{
    [OperationContract(IsOneWay = true)]
    void SendMessage(string message);
}

对于服务合同的实施,我有这个。 (控制台输出是因为这是一个供我学习的演示应用程序):

public class DemoService : IDemoService
{
    public void SendMessage(string message)
    {
        Console.WriteLine("{0} : {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), message);
    }
}

我的宿主应用程序是一个控制台应用程序,代码如下:

class Program
{
    static void Main(string[] args)
    {
        Console.Title = "WCF Host";

        using (var host = new ServiceHost(typeof(Library.DemoService)))
        {
            var endpoint = new ServiceEndpoint(
                ContractDescription.GetContract(typeof(Library.IDemoService)),
                new NetMsmqBinding(NetMsmqSecurityMode.None),
                new EndpointAddress("net.msmq://localhost/private/test"));

            host.AddServiceEndpoint(endpoint);

            host.Open();
            Console.WriteLine("Host Active");
            Console.ReadLine();
        }
    }
}

客户端同样简单:

class Program
{
    static void Main(string[] args)
    {
        Console.Title = "WCF Client";

        IDemoService proxy = ChannelFactory<IDemoService>.CreateChannel(
            new NetMsmqBinding(NetMsmqSecurityMode.None),
            new EndpointAddress("net.msmq://localhost/private/test")
            );

        do
        {
            string msg = Console.ReadLine();
            if (msg=="")
                break;
            else
                proxy.SendMessage(msg);
        } while (true);
    }
}

【问题讨论】:

    标签: wcf msmq


    【解决方案1】:

    我假设您的队列不是事务性的。

    虽然我不确定是否有办法将 netMsmqBinding 限制为单个线程,但您不需要应用此限制。

    为保证按顺序交付,您只需将队列设为事务性,然后将 exactlyOnce 属性应用于 netMsmqBinding 配置。

    参见示例here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      • 1970-01-01
      • 2011-10-17
      • 2015-08-20
      • 2021-07-24
      • 2012-01-16
      • 1970-01-01
      相关资源
      最近更新 更多