Azure Storage - 7: Azure Queue + .NET - OperationContext


In the previous post I described how QueueRequestOptions can be used and provided simple examples. In this post I will continue to work with Azure Storage Queue. I am going to describe an important parameter OperationContext.


Refer to previous posts to get started with Azure Storage Queue Service.


OperationContext represents the context for a request operation against Azure Storage Service, and provides additional runtime information about its execution. Consider a following example:

queue.AddMessage(new CloudQueueMessage("test"));
var ctx = new OperationContext();
queue.GetMessage(operationContext: ctx);
Console.WriteLine($"{ctx.ClientRequestID}, StartTime: {ctx.StartTime}, {ctx.EndTime}");

The ctx object will be filled by Azure Storage library with values. It is possible to set some of properties, such as ClientRequestID from code. The ClientRequestID appears in the Client Request ID field of every log entry relating to the request. Monitoring and Diagnostics is out of scope of this post, so I won't go any further. The other important property is UserHeaders, it is possible to pass custom headers with the request, they can be consumed by a proxy.

Also, a request result is stored in LastResult and RequestResults. The following example prints information about the last operation:

var ctx = new OperationContext();
queue.AddMessage(new CloudQueueMessage("test"), operationContext: ctx);
Console.WriteLine($"{ctx.LastResult.HttpStatusCode} {ctx.LastResult.HttpStatusMessage}");

This code gets values from LastResult property of OperationContext and prints 201 Created. There are number of other useful properties: ETag, ServiceRequestID, TargetLocation that help to better understand how request has been processed.


In this post I briefly described OperationContext and its most useful properties. Also I provided basic code samples. OperationContext can be very handy in certain situations, especially when you need to collect and analyze logging information. I will cover this topic in one of the future posts.