user217648
user217648

Reputation: 3466

Azure Queue to receive http requests

I want to know if it is possible to create an Azure Queue (Service bus or Storage Queue) which can be placed in front of a web application and receives http requests very fist.

updates

Thanks for the comments and answers.

I want to process the request without burding IIS. I need to make it possible to process a request in a queue before it reaches IIS.

Upvotes: 1

Views: 1356

Answers (1)

Amor
Amor

Reputation: 8491

if it is possible to create an Azure Queue (Service bus or Storage Queue) which can be placed in front of a web application and receives http requests very fist.

We can save the request message to a queue before handing the request in Azure Web App by adding some code. I wrote a C# version sample code which will record the request message to an Azure Storage queue. Steps below are for your reference.

Step 1. Add a http module to your project. In this module, I registered BeginRequest event of HttpApplication and do the message recording job.

public class RequestToQueueModeule : IHttpModule
{
    #region IHttpModule Members

    public void Dispose()
    {
        //clean-up code here.
    }

    public void Init(HttpApplication context)
    {
        // Below is an example of how you can handle LogRequest event and provide 
        // custom logging implementation for it
        context.BeginRequest += new EventHandler(OnBeginRequest);
    }

    #endregion

    public void OnBeginRequest(Object source, EventArgs e)
    {
        HttpApplication context = source as HttpApplication;
        AddMessageToQueue(context.Request);
    }


    public void AddMessageToQueue(HttpRequest request)
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine(request.HttpMethod + " " + request.RawUrl + " " + request.ServerVariables["SERVER_PROTOCOL"]);
        for (int i = 0; i < request.Headers.Count; i++)
        {
            sb.AppendLine(request.Headers.Keys[i] + ":" + request.Headers[i]);
        }
        sb.AppendLine();
        if (request.InputStream != null)
        {
            using (StreamReader sr = new StreamReader(request.InputStream))
            {
                sb.Append(sr.ReadToEnd());
            }
        }

        // Retrieve storage account from connection string.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse("connection string of your azure storage");

        // Create the queue client.
        CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

        // Retrieve a reference to a queue.
        CloudQueue queue = queueClient.GetQueueReference("queue name which is used to store the request message");

        // Create the queue if it doesn't already exist.
        queue.CreateIfNotExists();

        // Create a message and add it to the queue.
        CloudQueueMessage message = new CloudQueueMessage(sb.ToString());
        queue.AddMessage(message);
    }
}

Step 2. Register upper module in system.webServer node of web.config. Please modify the namespace name where your module placed.

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="RequestToQueueModeule" type="[your namespace name].RequestToQueueModeule" />
  </modules>
</system.webServer>

I want to process the request without burding IIS. I need to make it possible to process a request in a queue before it reaches IIS.

If you want to process the request in a queue before it reaches IIS, you need to add a proxy in front of Azure Web App. Azure Application Gateway works as a proxy and it can be put in front of Web App. If you only want to log the main information of HTT request, you could use Azure Application Gateway and turn on the Access Log. For more information, link below is for your reference.

Diagnostic logs of Application Gateway

If you want to save all the request message, I am afraid you need to build a custom proxy and log the request by yourself.

Upvotes: 2

Related Questions