简体   繁体   English

如何限制WCF MSMQ端点?

[英]How can I throttle a WCF MSMQ endpoint?

I'm new to WCF and just learning how to get a client to talk to a host (both in console applications) using MSMQ. 我是WCF的新手,只是学习如何使用MSMQ使客户端与主机(在控制台应用程序中)交谈。

I want to be able to send messages from client to host and have the host pick them up immediately or, if the host is stopped, to continue where it left off when it is restarted. 我希望能够将消息从客户端发送到主机,并让主机立即将其接收,或者,如果主机已停止,则可以在重新启动时从中断处继续。

I've got this almost working but I find that when I restart the host with ten messages in the queue, the messages are not processed in the queue order. 我几乎可以正常工作了,但是我发现在队列中有十条消息的情况下重新启动主机时,消息没有按队列顺序进行处理。 I assume there's some multithreading going on that makes them appear out of order. 我假设正在进行一些多线程处理,使它们看起来混乱。 I'd like to be able to limit the WCF service to processing one message at a time to stop this happening (unless there's a better solution). 我希望能够限制WCF服务一次仅处理一条消息以阻止这种情况的发生(除非有更好的解决方案)。

It's essential to a system that I'm about to work on that the MSMQ messages are processed in order and not in parallel. 对于我要处理的系统而言,MSMQ消息必须按顺序而不是并行处理是至关重要的。

The code for my service contract is: 我的服务合同的代码是:

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

For the Service contract implementation I've got this. 对于服务合同的实现,我已经做到了。 (The console output is because this is a demo app for me to learn from): (控制台输出是因为这是供我学习的演示应用程序):

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

My host application is a console application with the following code: 我的主机应用程序是一个带有以下代码的控制台应用程序:

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();
        }
    }
}

The client is equally simple: 客户端同样简单:

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);
    }
}

I am assuming your queue is not transactional. 我假设您的队列不是事务性的。

While I'm not certain there's a way to throttle netMsmqBinding to a single thread, you shouldn't need to apply this restriction. 虽然我不确定是否有办法将netMsmqBinding限制到单个线程,但是您不需要应用此限制。

To guarantee ordered delivery you only need to make your queue transactional and then apply the exactlyOnce attribute to the netMsmqBinding configuration. 为了保证有序交付,您只需要使队列具有事务性即可,然后将exactlyOnce属性应用于netMsmqBinding配置。

See example here . 在这里查看示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM