In one of my previous posts, https://ajitpatra.com/2019/12/09/d365-post-custom-message-to-azure-service-bus-queue-c/, we had gone through how to post custom message to a service bus queue. In this post, we’ll see how we can post the same message to a session enabled queue.
We have to make few lines of code changes in PostMessageToServiceBusQueue method of this post as shown below:
public static void PostMessageToServiceBusQueue(dxc_rate rate)
{
TimeSpan ts = new TimeSpan(0, 0, 90);
var ServiceBusNamespaceURL = "sb://demoazure.servicebus.windows.net";
var ServiceBusQueueURL = "https://demoazure.servicebus.windows.net/demoqueue/messages";
var SASKeyName = "IntegrationKey";
var SASKeyValue = "qkWTne2zdLQvJv6qp2+G1eqNau7XGfZZlZAEQTV9Y+A=";
string sasToken = Common.GetSASToken(ServiceBusNamespaceURL, SASKeyName, SASKeyValue, ts);
RateObject rateObject = new RateObject
{
RateID = rate.dxc_rateId.ToString(),
EndDate = rate.dxc_EndDate == null ? string.Empty : rate.dxc_EndDate.ToString(),
StartDate = rate.dxc_StartDate == null ? string.Empty : rate.dxc_StartDate.ToString(),
RateAmount = rate.dxc_Rate == null ? string.Empty : rate.dxc_Rate.Value.ToString(),
RatePercentage = rate.dxc_RatePercentage == null ? string.Empty : rate.dxc_RatePercentage.ToString(),
RateModel = rate.dxc_ratemodel == null ? string.Empty : rate.dxc_ratemodel.Value.ToString(),
RevenueCodeID = rate.dxc_RevenueCodeId == null ? string.Empty : rate.dxc_RevenueCodeId.Id.ToString()
};
var json = Common.SerializeToJsonString(rateObject);
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", sasToken);
var brokerProperties = new BrokerProperties { SessionId = Guid.NewGuid().ToString() };
client.DefaultRequestHeaders.Add("BrokerProperties", Common.SerializeToJsonString(brokerProperties));
var Content = new StringContent(json, Encoding.UTF8, "application/json");
var response = client.PostAsync(ServiceBusQueueURL, Content);
response.Wait();
}
We are using a custom class named BrokerProperties as shown below:
[DataContract]
public class BrokerProperties
{
[DataMember]
public string SessionId { get; set; }
}
Now, to send the message to a session enabled queue, we need to make sure that the queue is session enabled while creating it.

After posting the message, in Service Bus Explorer, we can see the details of the message with Session Id as shown below:


In our case, we have a Service Bus triggered Azure Function in which we read the message and perform some action. So, in that Azure Function, we need to add IsSessionsEnabled = true to ServiceBusTrigger attribute as shown below:
public static async Task Run([ServiceBusTrigger("%CalculateCSRQueue%", Connection = "AzureWebJobsServiceBus", IsSessionsEnabled = true)] Message message,
MessageReceiver messageReceiver,
string lockToken,
[DurableClient]IDurableOrchestrationClient starter, ILogger log)
{
...
}
Service Bus triggered Azure Functions without IsSessionsEnabled attribute will not be able to read messages from session enabled service bus queues.
More details on how to use Service Bus triggered Azure Durable Function with D365 CE is here: https://ajitpatra.com/2020/01/15/using-service-bus-triggered-azure-durable-function-with-d365-ce/.
Hope it helps !!