Async WCF Integration with NServiceBus

I am new to NServiceBus and trying to do something that seems harder than it should be ... so I start to wander if I am missing something in the enlarged nsb picture.

Here is the scenario:

  • Provide the WCF endpoint to the client from which they request a long-running operation.
  • I would like to match the incoming request with the NServiceBus message.
  • Post a message to the bus for processing.
  • Send a response to the client so that he receives the request, and we will begin to process it.
  • The bus processes the message through a handler.
  • When the job is complete, call the client back to their callback endpoint (wcf) to give them the result of the long request they made.

I welcome criticisms, examples, or links that may be helpful. Thank you in advance!

+4
source share
3 answers

There is potential for you to do this through the NSB pipeline. You can configure handlers to execute in the order you specify. In your case, this will be completed with a notification. Depending on the use case, it might be better to redirect notifications to another endpoint that processes only those types of messages. What you need to consider are failure scenarios. If the handler fails and the message is repeated, what will happen?

All this is based on the idea that you do not need to maintain a state. If so, then you will want to explore Saga . This will maintain state for a long transaction and give you some additional features that may be required, such as timeouts.

+2
source

According to the NServiceBus website, you can open the NSB endpoint as a WCF service:

Interaction

You can provide NServiceBus endpoints as WCF services using both a single line of code and a standard WCF configuration. All you have to do is write an empty class that inherits from NServiceBus.WcfService defining the request types and response, and NServiceBus does the rest as follows:

public class MyService : NServiceBus.WcfService<MyCommand, MyErrorCodes> { }

I did some work integrating old MSMQ clients with NServiceBus - it works, but you need to make sure that the message is built correctly.

Messages sent to the NServiceBus endpoint must be wrapped in the <Messages/> envelope and must have a namespace. For instance:

 <Messages xmlns="http://tempuri.net/MyNservicebusMessage"> <MyNservicebusMessage body/> ...etc </Messages> 

In addition, if you want to use NServiceBus auditing, you must make sure that the MSMQ "Response Queue" message header does matter, although I don't think the value matters.

+1
source

A lengthy process can be synchronous or asynchronous. It cannot be both.

You can use NServiceBus to asynchronously process a long-term task and generate your progress information. Adam mentions sagas. You can use the saga to track progress. It will also help you split the process into more granular tasks and give things like automatic retries that handle temporary failures for free.

However, you will need to use a different mechanism to transmit progress information to the user. Periodic polling, lengthy polling, hidden iframe, websockets, whatever - take a look at the ideas put forth by SignalR. There is a nice video here that talks about sending notifications to browsers.

+1
source

Source: https://habr.com/ru/post/1432177/


All Articles