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