Business Central/NAV integration with Azure Service Bus using Azure function.
Hello Team!
Today I tell you how to send messages from Business Central (BC) to Azure Service Bus (ASB). Also you can repeat this actions with C\AL for Dynamics NAV using "Http Web Request Mgt." codeunit instead of HttpClient variable.
More about ASB you could read here:
https://azure.microsoft.com/en-us/services/service-bus/
Lets start:
1. First of all you need to create Service Bus namespace. Open Azure portal, click on Create New resource, type "Service Bus" and press Create
Input name of your Bus. Choose Pricing tier (Basic for this test), resource group and location.
After several minutes you'll get your ASB:
2. Now we need SAS policy for our ASB and at least one message queue:
Click on Shared access policy of your ASB and press Add. Type some name and select Manage. Press Create.
Select Queues tab and press Queue. Input name and press Create:
Set SAS policy for your queue (you could skip it and use SAS policy for Service bus):
As you could notice - this policies doesn't contain SAS token. Only connection strings. Obtaining of SAS token for ASB is a headache - you could read how to construct it here:
We will use much more easier method to connect to service bus - Azure function!
3. Azure functions allows you to use Connection strings to authenticate ASB. And integration of Business Central with Azure functions is very simple.
Create App service for your function:
Now we need VS Code to create Azure Function. With next extensions : Azure Functions and C#
Open VS Code, install extensions and click on Azure Functions menu:
4. Connect to your Azure account, select Subscription and Press Create Function icon. Select App service (previously created) and function type - Http Trigger, language - C#.
you'll get some kind of HelloWorld project. We need to change it a little.
All code you could find here:
https://github.com/ABaludin/ASBFunction
Add Service Bus package to your project:
Open local.settings.json and add ServiceBusConnection parameter which is primary connection string from Service bus or queue SAS policy:
And finally - function code. Get request from Business Central and send it to ASB:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace ABaludin.Asb
{
public static class ASB
{
[FunctionName("ASB")]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
ILogger log,
[ServiceBus("nav2asb", Connection = "ServiceBusConnection", EntityType = Microsoft.Azure.WebJobs.ServiceBus.EntityType.Queue)] out string busMessage)
{
string message = new StreamReader(req.Body).ReadToEnd();
busMessage = message;
return message != null
? (ActionResult) new OkObjectResult("ok")
: new BadRequestObjectResult("No message");
}
}
}
Now publish your function to App service, open App service on Azure portal and copy your function's URL:
5. Sending messages from Business Central:
Create page extension which sends employees data to ASB. Integration is very simple, especially with anonymous authentication:
Repository with code:
https://github.com/ABaludin/HappyBirthday
pageextension 50145 "AWR_EmployeeList" extends "Employee List"
{
actions
{
addlast(Reporting)
{
action("AWR_SendCongratulation")
{
ApplicationArea = All;
Image = SendMail;
Promoted = true;
PromotedCategory = Process;
Caption = 'Send Congratulation';
trigger OnAction()
var
Client: HttpClient;
Url: Text;
Content: HttpContent;
Response: HttpResponseMessage;
ResponseText: Text;
begin
Url := 'https://<>.azurewebsites.net/api/<>';
Content.WriteFrom(StrSubstNo(Message_Txt, Rec."First Name", Rec."Last Name"));
if not Client.Post(Url, Content, Response) then
Error(Text001_Err);
Response.Content().ReadAs(ResponseText);
if not Response.IsSuccessStatusCode() then
Error(Text002_Err, Response.HttpStatusCode(), ResponseText);
end;
}
}
}
var
Text001_Err: Label 'Service inaccessible';
Text002_Err: Label 'The web service returned an error message:\ Status code: %1\ Description: %2';
Message_Txt: Label 'Today is Happy Birthday of %1 %2! Do not forget to congratulate!';
}
you could check message quantity in ASB monitoring on Azure portal:
but to check message text and some other functions you need special tool to work with ASB.
I like this one:
https://github.com/paolosalvatori/ServiceBusExplorer/releases
See screenshots, it's quite good:
Hope, this was useful for you!

Like
Report
*This post is locked for comments