Receive a Message from Service Bus Queue via .NET DLL

PierreCPierreC Member Posts: 2
Dear all,

I would like to use a .NET DLL to process Azure Service Bus messages in NAV 2017.
I have imagined that I receive Service Bus Messages from a queue via the DLL, then fire an event in the DLL, which is caught in NAV via an event handler.
The event handler has a string as parameter, which contains the message.
Here is the code of the .NET DLL and the NAV code (see below).

My problem is that as soon as I start the Codeunit, NAV hangs completely or is blocked.
The problem will be that in the DLL _resetEvent.Wait(); is executed.
In a .NET console app this works fine and it behaves as I expect it to.
Unfortunately, however, it does not with the DLL and NAV.

If I omit the resetEvent.Wait();, the Start() function exits too quickly and the event handler cannot execute.

Do you guys have any other idea how I can solve this problem?

I need a solution where the function does not terminate too fast and at the same time does not block NAV.

NAV Code:
OnRun()
EventimWrapper.Start();

EventimWrapper::MessageReceive(msg : Text)
MESSAGE(msg);


.NET Code:
public static void Start()
{
	_resetEvent = new ManualResetEventSlim(false);

	ReceiveMessagesAsync().GetAwaiter().GetResult();

}

static async Task ReceiveMessagesAsync()
{
	_client = new ServiceBusClient(AZURE_SERVICE_BUS_CONNECTIONSTRING, new ServiceBusClientOptions
	{
		TransportType = ServiceBusTransportType.AmqpWebSockets
	});

	_processor = _client.CreateSessionProcessor(QUEUE_NAME, new ServiceBusSessionProcessorOptions
	{
		MaxConcurrentSessions = 1,
		AutoCompleteMessages = false
	});

	// add handler to process messages            
	_processor.ProcessMessageAsync += MessageHandler;

	// add handler to process any errors
	_processor.ProcessErrorAsync += ErrorHandler;

	// start processing 
	await _processor.StartProcessingAsync();
	_resetEvent.Wait();
}
	
static async Task MessageHandler(ProcessSessionMessageEventArgs arg)
{
	
	SessionMessageEventArgs = arg;
	await Task.Run(() => OnMessageReceive(arg.Message.Body.ToString()));
}

Answers

  • gycsiakgycsiak Member Posts: 19
    Hi PierreC,

    I am currently developing a solution for Azure Service Bus Communication between Microsoft Dynamics BC 14 Version.

    For your question, I am advised to use Await command and Async Communication so that the particular DLL Function would eventually wait for any Response from Azure Service Bus.

    You can find valuable examples here, should you still need it:
    <link>https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample01_HelloWorld.md</link&gt;

    Which .NET Version are you using for communicating between NAV and ASB? Did you also use Azure.Core Nuget Package as well? Did you also received any "DLL Missing Error" in NAV for Microsoft.Bcl maybe?

    Thank you for your feedback!
Sign In or Register to comment.