Is using RPC with Masstransit best practice if you are trying to get a response from a queue - rabbitmq

I thought using RPC is bad practice but all the resources I am finding point to using RPC in order to get a response from a queue after publishing a request. Are there any other ways of doing it? Is it the best practice?
Thanks

MassTransit has built-in support for producing requests (which can be published, or sent directly to a specific endpoint). The request client can be created manually or added to a dependency injection container, and one or more response types can be handled.
MassTransit uses the bus endpoint to receive responses by default.
To register the request client in the container, the AddRequestClient method is used as shown below.
services.AddMassTransit(x =>
{
// configure transport/host/etc.
x.AddRequestClient<CheckOrderStatus>();
});
RPC is a common pattern, and producing requests when a response is required, it a regularly used approach. Another option is combining a command with an event, and observing the event separate from the request producer. However, if a linear programmatic flow is required, using RPC via the request client is an easy solution.

Related

Message is still processed even after client request timeout in MassTransit

I am implementing standard request/response scenario with MT and RabbitMQ. Client is Asp.net core API and consumer is a windows service.
As part of testing the exception cases if I stop the consumer and submit a request from API using request client, since there is no consumer processing, API got request timeout exception which is cool. But the message is sitting in the consumer queue and when I start the consumer, it picks the message and process the stuff( sending the message to external endpoint) and moved to a _skipped queue as there is no request client listening for this message.
Do you think it is correct behavior? First place when the api got request timeout exception, he will retry anyway so what’s the point of processing first message still?
How can I ignore those message where request clients were already finished processing with any error?
Thanks
What you are describing is very common, and I'd recommend reading up on idempotence and other distributed system failure scenarios.
Sending commands (the request, in this case) and conveying outcomes via a timeout in a message-based system can be very misleading. For instance, if you look at the ForkJoint, in the event of a request timeout, the response is actually a 202/Accepted instead of communicating an error.
The message is in the queue, it will be processed, so there is no reason to fail the controller and report an error back to the caller. So an intermediate response is used instead.
The sample is part of MassTransit Season 3 where I discuss a new idiom to deal with eventual completion/failure of commands in distributed systems. There might be some useful examples in there to help you understand the failure scenarios.
As doc sayed, to discard skipped messages so they are not moved to the _skipped queue:
cfg.ReceiveEndpoint("input-queue", ec =>
{
ec.DiscardSkippedMessages();
});

Configure RabbitMQ to route to queue via HTTP endpoint, therefore not needing the normal JSON data

For my deployment I have a number of 3rd party systems that can only send HTTP POST requests with metrics (I need in the queue) and they cannot be re-configured. My goal is to have specific endpoints (or vhosts) that when POST'd to will automatically route to the correct queue, without needing the necessary routing key and other standard rabbitmq JSON data. As this modification is not possible in the 3rd party systems.
I can't find any way to do this natively as of now, but I believe it may be possible to configure a HTTP reverse proxy in the front, whereby any data sent to the specific endpoint, will be re-directed to the correct rabbitMQ HTTP endpoint, where I could then bolt in the nessary JSON data so it can be parsed by rabbitmq and placed in the realvent queue. I wanted to check if this is the only logical solution to this, or am I missing something obvious that can be done within rabbitmq's administration page or via config files.

Wrapping async MSMQ with a sync WCF service

How can I build a synchronous WCF service that wraps asynchronous MSMQ communications?
Let us have a simple scenario. I have a client which supports only synchronous web service calls. I need to send a synchronous request for "Order", but the back end system exposes this as an asynchronous request and response MSMQs. The WCF does not need to have any logic just wrap the MSMQ asych communication and pass parameters back and forth.
Grateful for your help
Let us have a simple scenario. I have a client which supports only synchronous web service calls
Synchronous call means you are using same link (end point url) or channel for your communication between client and server, so according to your assumption no.
Reason: Every time your client will send a request, it will keep waiting for response from server and will produce error.
Alternatively,You can define two services (different end point url) in your wsdl or webservice, one for request and one for response.
At client side you need to invoke these end point url saperately for sending request and receiving response,so it will appear as synchronous but ultimately it will be asynch. Thats all you can do in this scenario.
As per my understanding.

WCF service to queue all request

I have a wcf service and handle a lot of client (server document generation). This service should receive a lot of request and should be handle in queue. It also have a callback. (callback will return successfully generated document). I am still using PIA and will implement OpenXML in the future.
Is it wcf msmq is the way to implement this?
Is there any samples might be related? Previously its running in local machine but now want to change it as a so called "Server generated"
WCF MSMQ doesn't support callback directly - it supports only one-way operations. But for example this article discuss how to add this support. With default configuration you can send message back to original sender but it is not a callback. To support responses every client will have to expose queue and pass address of its queue as part of the request to be able to receive the message from the service. More about responses in MSMQ is in MSDN magazine.

Using WCF To send directly to MSMQ queues

I have a windows service that picks up messages from various MSMQs. The service picks up the messages from the queues, and then executes a specific workflow based on the queue the message came in from. Is it possible to configure a WCF service to accept a message from an endpoint and place it in a queue? I would like for the service to have multiple endpoints, and depending on which endpoint received the message, the message would be placed in a different queue. I have seen things about netMsmqBinding but so far I haven't been able to determine if this is meant for the use I am thinking about.
Any help is appreciated,
Mike
Am I getting this correctly - you want to have
a WCF service exposed to the outside world using multiple endpoints
depending on which endpoint the message comes into the WCF service, it should put those messages into separate MSMQ queues?
You can definitely have a WCF service that will expose any number of endpoints to the outside and that would drop incoming messages into an MSMQ queue. I'm just not sure whether you'll be able inside the WCF service method to know which endpoint the request came in on..... typically, the WCF service class doesn't know anything about where the request came from (and typically doesn't need to know...).
I see a few approaches to this:
have multiple services, one for each (type of) endpoint; they can share all their code and everything, but if you separate this into separate service classes, then each service class would inherently know what endpoint it serves, and thus could know what MSMQ queue to put the info into
find a way to send some header information along with the message that somehow tells you where to put the messages
in WCF 4.0, you might be able to do something with the new WCF Routing Service.... not quite sure how that would work, however - since it would also require some kind of "hint" to know which endpoint it was called on and how to route the incoming message
The way I was able to go about this was by creating a custom ServiceHost class that I could pass a configuration name to. This is an alternative to the default implementation that uses the Service Implementation type name to locate the configuration. This allowed me to run multiple instances of the same implementation on the same machine. In addition to that, inside the custom service class I added a custom MessageInspector that would attach itself to every endpoint to listen for messages. I then use the IDispatchMessageInspector.AfterReceiveRequest() method to take the incoming message and then place it into a queue that I define inside my configuration. A little complicated, but it works perfectly for our scenario.
net.msmq bindings allow a WCF client to write directly to an MSMQ queue, even if the queue-reading WCF service is not live -- it doesn't even have to exist. net.msmq bindings also allows automagic reading from queues. No use of MessageQueue to write to, or read from, a queue.