c# - RabbitMQ not receiving messages when used with TopShelf as a Windows Service -
i'm trying convert rabbitmq micro-service windows service. have used topshelf conversion. rabbitmq micro-service works fine on own when run service no longer receives messages. in public static void main(string[] args)
have:
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 provides database access totables."); host.setdisplayname("service"); host.setservicename("service"); }); }
then in personservice
class have
public void start() { consumemessage(); }
and consumemessage
function:
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 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 not valid. winlogger.log.error( "errror processing message: " + receivedmessage + " :" + e.message); response = ""; } { 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(); }
looking @ a similar question looks has return windows service wanting, i'm not sure of how call consumemessage
consumer.received += (model, ea) => {...};
executed.
edit: looks blocking mechanism console.readline();
ignored service continues on , disposes of message consumer. how block there messages received?
you code uses using
construct, means when onstart
method returns, channel
disposed. docs suggest initialization on onstart
, create channel
, consumer
there, don't use using
:
this.connection = factory.createconnection(); this.channel = connection.createmodel(); this.consumer = new eventingbasicconsumer(this.channel);
then objects continue exist after onstart
method finished. should dispose of them in onstop
method.
Comments
Post a Comment