Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在WCF中使用RabbitMQ?_C#_Wcf_Rabbitmq_Wcf Binding - Fatal编程技术网

C# 如何在WCF中使用RabbitMQ?

C# 如何在WCF中使用RabbitMQ?,c#,wcf,rabbitmq,wcf-binding,C#,Wcf,Rabbitmq,Wcf Binding,我有一个场景,其中可执行文件是生产者,WCF服务是消费者 WCF服务工作流程如下: 1) 服务调用可执行文件(producer),该可执行文件是另一个将消息生成RabbitMQ队列的进程 2) 服务必须使用来自RabbitMQ队列的消息 3) 将数据返回到客户端 using RabbitMQ.Client; using RabbitMQ.Client.Events; using System; using System.Collections.Generic; using System.Diag

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

WCF服务工作流程如下:

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

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

3) 将数据返回到客户端

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;
        }
    }
}
到目前为止,服务能够调用可执行文件并在队列中生成消息

但服务从第2步开始失败,它将返回null而不是实际消息。 有人能告诉我这里缺少什么吗


提前感谢。

除了
null
之外,您永远不会将
activeInstance
设置为任何其他值

您似乎正在使用异步API,这意味着您在
RunRabbitMQ
方法调用完成很久之后从RabbitMQ检索消息。。。或者,如果您在返回时没有立即处理所有的消费类机器,您可能会这样做


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

您永远不会将
activeInstance
设置为除
null
之外的任何内容

您似乎正在使用异步API,这意味着您在
RunRabbitMQ
方法调用完成很久之后从RabbitMQ检索消息。。。或者,如果您在返回时没有立即处理所有的消费类机器,您可能会这样做


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

这两行代码,其中使用了
activeInstance
string activeInstance=null
返回activeInstance你从未设置过这个变量。这是我的输入错误。在实际代码中,它是正确的,但我仍然得到null@ReniuzSo请添加实际代码。现在消息必须为空,但不能为空,因为GetString()返回字符串。我编辑了代码@reniuzy。您的问题与WCF完全无关。使用
activeInstance
的这两行代码:
string activeInstance=null
返回activeInstance你从未设置过这个变量。这是我的输入错误。在实际代码中,它是正确的,但我仍然得到null@ReniuzSo请添加实际代码。现在消息必须为空,但不能为空,因为GetString()返回字符串。我编辑了代码@ReniuzYour的问题与WCF完全无关。这是我的打字错误。在实际代码中,它是正确的,但我仍然得到空值@yaakovy您仍然存在同步性问题-您正在从事件设置变量,但在方法完成之前不会触发该事件。我发现在WCF服务中,该事件没有被触发。虽然我使用console应用程序测试的代码和事件被触发,但我能够返回值。你能帮忙吗,为什么它不能在WCF中触发@yaakovIt是我的输入错误。在实际代码中它是正确的,但我仍然得到null@yaakovYou仍然存在同步性问题-您正在从事件设置变量,但在方法完成之前事件不会被触发。我发现,从WCF服务,事件未被触发。而我使用console应用程序测试的代码和事件正在被触发,并且我能够返回值。你能帮忙吗,为什么它不能在WCF中触发@卫达亚科夫