【发布时间】:2017-09-06 09:19:15
【问题描述】:
我有一个场景,其中可执行文件是生产者,WCF 服务是消费者。
WCF服务工作流程如下:
1)Service调用可执行文件(生产者),这个可执行文件是另一个进程,它产生消息到RabbitMQ队列中。
2)Service 必须消费来自 RabbitMQ Queue 的消息
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 而不是实际消息。 有人可以建议我在这里缺少什么吗?
提前致谢。
【问题讨论】:
-
这两行使用了
activeInstance:string activeInstance = null;return activeInstance;你从来没有设置这个变量。 -
这是我的拼写错误。在实际代码中它是正确的,但我仍然得到 null @Reniuz
-
所以请添加实际代码。现在消息必须为空,但不能因为 GetString() 返回字符串。
-
我已经编辑了代码@Reniuz
-
你的问题与 WCF 完全无关。
标签: c# wcf rabbitmq wcf-binding