How to route traffic to servicebus service via api management and also retain session information for return call

Casper Rubæk 246 Reputation points
2021-01-13T18:15:54.827+00:00

I would like to get help in routing traffic to servicebus through api management.

I am able to send a message to a queue on servicebus through its REST API endpoint: POST https://

Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,135 questions
{count} vote

3 answers

Sort by: Most helpful
  1. ChaitanyaNaykodi-MSFT 22,701 Reputation points Microsoft Employee
    2021-01-20T04:45:18.107+00:00

    Hello @CasperR-1809, my sincere apologies for the delay. To expose Service Bus through API management you can follow this Microsoft Build Event demo (skip to 40:54) . The policy snippet used here is as bellow.

    <policies>  
     <inbound>  
     <base />  
     <set-header name="Content-Type" exists-action="override">  
     <value>vnd.microsoft.servicebus.yml</value>  
     </set-header>  
     <set-header name="Authorization" exists-action="override">  
     <value>{{orders-queue-key}}</value>  
     </set-header>  
     <set-header name="BrokerProperties" exists-action="override">  
     <value>@{  
                        var json = new JObject();  
                        json.Add("MessageId", context.RequestId);  
                        json.Add("Label", "New-Order");  
                        return json.ToString(Newtonsoft.Json.Formatting.None);                      
     }</value>  
     </set-header>  
     <set-backend-service base-url="{{orders-sb-namespace}}" />  
     <rewrite-uri template="{{orders-queue}}" />  
     </inbound>  
     <backend>  
     <base />  
     </backend>  
     <outbound>  
     <base />  
     <choose>  
     <when condition="@(context.Response.StatusCode == 201)">  
     <set-header name="Content-Type" exists-action="override">  
     <value>application/json</value>  
     </set-header>  
     <set-body>@{  
                            var json = new JObject() {{"Order", context.RequestId}} ;  
                            return json.ToString(Newtonsoft.Json.Formatting.None);       
     }</set-body>  
     </when>  
     </choose>  
     </outbound>  
     <on-error>  
     <base />  
     </on-error>  
    </policies>  
    

    You can follow this documentation for adding named values in API management. Please let me know if there are any additional concerns.

    2 people found this answer helpful.

  2. ChaitanyaNaykodi-MSFT 22,701 Reputation points Microsoft Employee
    2021-01-22T06:59:37.157+00:00

    (Posting this as an answer due to character limits in Comments section).
    Hello @CasperR-1809, Based on my understanding of the architecture given. The requirement is -> Upon Single request from the client, the following operations should take place within Azure API Management.

    1. Add a message to Service Bus Topic 1.
    2. This message added above will trigger a Function App which will generate some data and add it to Service Bus Topic 2.
    3. Perform a Peek-Lock Message (Non-Destructive Read) OR Receive and Delete Message (Destructive Read) to receive the message from Service Bus Topic 2.

    This message(content) from Service Bus Topic 2 should be sent back to same Client. Please correct me if my understanding is wrong.

    Based on my understanding of the architecture, I do not think this is possible just using API Management, I think you might have to use a Function App before your Azure Service Bus to maintain the same client session.
    If it helps I could think of below mentioned APIM policy which can do multiple calls in the backend.

    <policies>  
     <inbound>  
            <!-- Sending a message to Azure Service Bus -->  
     <send-request ignore-error="true" timeout="30" mode="new" response-variable-name="responseState">  
     <set-url>http{s}://{serviceNamespace}.servicebus.windows.net/{queuePath&#124;topicPath}/messages</set-url>  
     <set-method>POST</set-method>  
     <set-header name="Content-Type" exists-action="override">  
     <value>vnd.microsoft.servicebus.yml</value>  
     </set-header>  
     <set-header name="Authorization" exists-action="override">  
     <value>{<!-- -->{orders-queue-key}}</value>  
     </set-header>  
     <set-header name="BrokerProperties" exists-action="override">  
     <value>@{  
                                    var json = new JObject();  
                                    json.Add("MessageId", context.RequestId);  
                                    json.Add("Label", "New-Order22");  
                                    return json.ToString(Newtonsoft.Json.Formatting.None);                      
                }</value>  
     </set-header>  
     </send-request>  
     <choose>  
     <when condition="@(((IResponse)context.Variables["responseState"]).StatusCode != 201)">  
     <return-response>  
     <set-status code="401" reason="ASB POST message failed" />  
     </return-response>  
     </when>  
     </choose>  
            <!-- Receiving a message from Azure Service Bus -->  
     <set-method>POST</set-method>  
     <set-header name="Content-Type" exists-action="override">  
     <value>vnd.microsoft.servicebus.yml</value>  
     </set-header>  
     <set-header name="Authorization" exists-action="override">  
     <value>{<!-- -->{orders-queue-key}}</value>  
     </set-header>  
     <set-backend-service base-url="http{s}://{serviceNamespace}.servicebus.windows.net/{topicPath}/subscriptions/{subscriptionName}/messages/head" />  
     <rewrite-uri template="\" />  
     <base />  
     </inbound>  
     <backend>  
     <base />  
     </backend>  
     <outbound>  
     <base />  
     </outbound>  
     <on-error>  
     <base />  
     </on-error>  
    </policies>  
    

    Here are some some limitation about "timeout" mentioned in the policy above which should be taken under considertaion.

    Also based on my understanding, can you please let me know how you are planning to handle concurrency in your architecture? (Multiple Clients performing API request concurrently).

    Please let me know if there are any concerns, I will be glad to continue with our discussion. Thank you!


  3. Guilherme Mariano Munis 1 Reputation point
    2021-03-22T13:44:16.027+00:00

    Hello @CasperR-1809 , how are you?
    I am new to Azure development.
    I got to this topic because I have a task.
    I need to send a payload received from a webhook to two queues of the type servicebusqueuetrigger.
    Can you help me with this?