gpt4 book ai didi

c# - 与 TopShelf 作为 Windows 服务一起使用时,RabbitMQ 不接收消息

转载 作者:行者123 更新时间:2023-12-01 19:11:32 25 4
gpt4 key购买 nike

我正在尝试将 RabbitMQ 微服务转换为 Windows 服务。我已经使用 TopShelf 进行转换。我的 RabbitMQ 微服务本身工作得很好,但是当我将它作为服务运行时,它不再接收消息。在我的 public static void Main(string[] args) 中,我有:

 HostFactory.Run(host =>
{
host.Service<PersonService>(s =>
{
s.ConstructUsing(name => new PersonService());
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
host.SetDescription("Windows service that provides database access totables.");
host.SetDisplayName("Service");
host.SetServiceName("Service");
});
}

然后在我的 PersonService 类中,我有

public void Start() {
ConsumeMessage();
}

最后是我的 ConsumeMessage 函数:

private static void ConsumeMessage() {
MessagingConfig.SetInstance(new MessagingConstants());
IMessageFactory pmfInst = MessageFactory.Instance;

//message worker
var factory = new ConnectionFactory() {
HostName = MessagingConfig.Instance.GetBrokerHostName(),
UserName = MessagingConfig.Instance.GetBrokerUserName(),
Password = MessagingConfig.Instance.GetBrokerPassword()
};

var connection = factory.CreateConnection();

using (var channel = connection.CreateModel()) {
channel.QueueDeclare(queue: MessagingConfig.Instance.GetServiceQueueName(),
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);

channel.BasicQos(0, 1, false);

var consumer = new EventingBasicConsumer(channel);

channel.BasicConsume(queue: MessagingConfig.Instance.GetServiceQueueName(),
noAck: false,
consumer: consumer);

Console.WriteLine("Service.");
Console.WriteLine(" [x] Awaiting RPC requests");


// Code Below Is Not Executed In Service
consumer.Received += (model, ea) => {

string response = null;

var body = ea.Body;
var props = ea.BasicProperties;
var replyProps = channel.CreateBasicProperties();
replyProps.CorrelationId = props.CorrelationId;

string receivedMessage = null;

try {
receivedMessage = Encoding.UTF8.GetString(body);
response = ProcessMessage(receivedMessage);
}
catch (Exception e) {
// Received message is not valid.
WinLogger.Log.Error(
"Errror Processing Message: " + receivedMessage + " :" + e.Message);

response = "";
}
finally {

var responseBytes = Encoding.UTF8.GetBytes(response);
channel.BasicPublish(exchange: "", routingKey: props.ReplyTo,
basicProperties: replyProps, body: responseBytes);
channel.BasicAck(deliveryTag: ea.DeliveryTag,
multiple: false);
}
};
Console.ReadLine();
}

查看 A similar SO question看起来它与 Windows 服务想要的返回有关,但我不确定如何调用 ConsumeMessage 所以 consumer.Received += (model, ea) = > {...}; 被执行。

编辑:看起来我的阻塞机制Console.ReadLine();被服务忽略,因此它只是继续并处理消息使用者。那么如何阻止那里接收消息呢?

最佳答案

您的代码使用 using 构造,这意味着当您的 OnStart 方法返回时,您的 channel 将实际被释放。 docs建议在 OnStart 上进行初始化,因此在那里创建您的 channelconsumer,但不要使用 using :

this.connection = factory.CreateConnection();

this.channel = connection.CreateModel();
this.consumer = new EventingBasicConsumer(this.channel);

那么这些对象将在OnStart方法完成后继续存在。您应该在 OnStop 方法中处理它们。

关于c# - 与 TopShelf 作为 Windows 服务一起使用时,RabbitMQ 不接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45679594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com