How to use a recurring Integration Endpoint for importing data
You are here
Send and Receive Azure Queue Messages in D365FO
In our last article, we went over how to send and receive from C# inside VS next to a unified operations. Now, let's talk about getting messages from inside X++. X++ can't directly get messages from an Azure queue, so far as I have been able to determine. It may be technically possible but we can use C# to do some of the lifting for us where X++ is a little lacking so let's do that first. In our last article, we created a console app which is great to show how things work. However, this time we will want to create a class library type class.
First, create a class library class.
In the solution explorer, right click on reference and select "Manage NuGet Packages"
Search for WindowsAzure.ServiceBus. Click Install
Next, right click on references then "Add Reference..." then click on the browse button.
Find the Microsoft.ServiceBus.dll in the following folder located in your projects root directory: packagesWindowsAzure.ServiceBus.5.0.0\lib\net46
Add it to your project.
Now, create a new class. Call it whatever you would. In this example I will be using namespace AAX_AzureQueueLibrary with classes ReceiveAzureQueue and SendAzureQueue
Next, let's fill out our new class a little. This will be our send C# class and will contain everything required to push stuff to the queue.
using Microsoft.ServiceBus.Messaging;
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Json;namespace AAX_AzureQueueLibrary
{
public class SendAzureQueue
{
QueueClient queueClient;
BrokeredMessage message;public void connect(string connectionString)
{
queueClient = QueueClient.CreateFromConnectionString(connectionString, ReceiveMode.PeekLock);
}public void sendMessage(string s)
{
message = new BrokeredMessage(s);
queueClient.Send(message);
}public void close()
{
queueClient.Close();
}
}
}
As you can see we have a method to connect, send a message and close the connection. This is the most basic method and lacks any error checking. Now, let's create another new class. This will be our receive messages class and will have everything required to get a single message from a queue.
using Microsoft.ServiceBus.Messaging;
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Json;namespace AAX_AzureQueueLibrary
{
public class ReceiveAzureQueue
{
QueueClient queueClient;
BrokeredMessage message;public void connect(string connectionString)
{
queueClient = QueueClient.CreateFromConnectionString(connectionString, ReceiveMode.PeekLock);
}public string readMessage()
{
message = queueClient.Receive();string s = message.GetBody<String>();
return (s);
}public void completeMessage()
{
queueClient.Complete(message.LockToken);
}public void close()
{
queueClient.Close();
}
}
}
Next, let's create a new Unified Operations project inside our current solution. First, we'll need to add our references. Right Click and add reference to our C# project. Next, we'll have to repeat the addition of the Microsoft.ServiceBus.dll but in our Unified Operations project. Next, let's add a new class. I called this class AAX_AzureQueueTest.
class AAX_AzureQueueTest
{
const str connectionString = "Endpoint=sb://[urlName]servicebus.windows.net/;SharedAccessKeyName=[SharedAccessName];SharedAccessKey=[SharedAccessKey]=;EntityPath=[QueueName]";public static void main(Args args)
{
AAX_AzureQueueLibrary.ReceiveAzureQueue ReceiveAzureQueue = new AAX_AzureQueueLibrary.ReceiveAzureQueue();
AAX_AzureQueueLibrary.SendAzureQueue SendAzureQueue = new AAX_AzureQueueLibrary.SendAzureQueue();//x++ get a guid, not C#
str s = "Test Message " + WinAPIServer::createGUID();SendAzureQueue.connect(connectionString);
SendAzureQueue.sendMessage(s);
SendAzureQueue.close();info(strFmt("sent: %1", s));
//Just to confirm we are getting a message back
s = "";ReceiveAzureQueue.connect(connectionString);
s = ReceiveAzureQueue.readMessage();
ReceiveAzureQueue.completeMessage();
ReceiveAzureQueue.close();
info(strFmt("Received: %1", s));
}
}
In this class we're instantiating instances of our classes from the C# project we created to simply proxy the calls to the queue. We created a message, sent it then received it back and did a variable reset in between just to confirm we are getting something back and not regurgitating what we already had. This code, the projects, solution and all settings can be found on Github.