代码之家  ›  专栏  ›  技术社区  ›  Mahek

如何在WCF中使用RabbitMQ?

  •  0
  • Mahek  · 技术社区  · 7 年前

    我有一个场景,其中可执行文件是生产者,WCF服务是消费者。

    1) 服务调用可执行文件(生产者),该可执行文件是将消息生成RabbitMQ队列的另一个进程。

    2) 服务必须使用来自RabbitMQ队列的消息

    using RabbitMQ.Client;
    using RabbitMQ.Client.Events;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    
    namespace ConnectionServices
    {
    
        public class Connection : IConnection
        {
            public string ConnectSite(string provider, string server, string siteName)
            {
                InvokeProducer(provider, server, siteName);
                string activeInstance = RunRabbitMQ();
                return activeInstance;
    
            }
            public void InvokeProducer(string provider, string server, string siteName)
            {
                string siteManagerExePath = @"C:\Users\mbmercha\Documents\Visual Studio 2015\Projects\Producer\Producer\bin\Debug\Producer.exe";
                try
                {
                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    Process siteManagerProcess = new Process();
                    startInfo.FileName = siteManagerExePath;
                    startInfo.Arguments = string.Format("{0} {1} {2} {3}", "-b ", provider, server, siteName);
                    siteManagerProcess.StartInfo = startInfo;
                    siteManagerProcess.Start();
                    siteManagerProcess.WaitForExit();
    
                }
                catch (Exception e)
                {
    
                }
            }
            public string RunRabbitMQ()
            {
                var factory = new ConnectionFactory() { HostName = "localhost" };
                string activeInstance = null;
                using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare("DurableQueue", true, false, false, null);
                    channel.ExchangeDeclare("DurableExchange", ExchangeType.Topic, true);
                    channel.QueueBind("DurableQueue", "DurableExchange", "durable");
                    var consumer = new EventingBasicConsumer(channel);
    
                    consumer.Received += (model, ea) =>
                    {
                        var body = ea.Body;
                        var message = Encoding.UTF8.GetString(body);
                        activeInstance = message;
                    };
                    channel.BasicConsume(queue: "DurableQueue",
                                         autoAck: false,
                                         consumer: consumer);
    
    
                }
                return activeInstance;
            }
        }
    }
    

    到目前为止,服务能够调用可执行文件,并在队列中生成消息。

    1 回复  |  直到 7 年前
        1
  •  2
  •   yaakov Takhion    7 年前

    你从来没有设置 activeInstance 除了 null

    您似乎正在使用异步API,这意味着您在消息传递之后很长一段时间才从RabbitMQ检索消息 RunRabbitMQ

    如果要同步检索消息(在本例中,在同步方法调用中),则需要等待消息变为可用。为此,您需要使用“拉API”,即 channel.BasicGet(...)