web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :

Business Central/NAV integration with Azure Service Bus using Azure function.

Andrey Baludin Profile Picture Andrey Baludin 3,941

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

8814.JPG

Input name of your Bus. Choose Pricing tier (Basic for this test), resource group and location.

2742.JPG

After several minutes you'll get your ASB:

2577.JPG

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.

3022.JPG

Select Queues tab and press Queue. Input name and press Create:

7776.JPG

Set SAS policy for your queue (you could skip it and use SAS policy for Service bus):

0714.JPG

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:

https://docs.microsoft.com/en-US/azure/service-bus-messaging/service-bus-authentication-and-authorization

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:

7178.JPG

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:

2337.JPG

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:

3173.JPG

Open local.settings.json and add ServiceBusConnection parameter which is primary connection string from Service bus or queue SAS policy:

4035.JPG

And finally - function code. Get request from Business Central and send it to ASB:

2620.JPG

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:

0068.JPG

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:

6708.JPG

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:

3771.JPG

Hope, this was useful for you!

Comments

*This post is locked for comments