Skip to main content

How to use Shared Access Signature with queues from Windows Azure

In the last post we talk about how we can use Shared Access Signature with blobs. In this post we will see how we can use this feature with queues. First of all we should know that this is a new feature that appeared with the February release of the new version Windows Azure.
The new version of Windows Azure added to the Sharing Access Signature new functionalities for queues. The following features are supported for queues:
  • Add, remove, update delete queue message
  • Message Count of a queue
  • Access queue metadata
  • For a give queue we can set the following restrictions:
  • Time range – define the time interval when the user has access to the queue
  • Access permissions – what type of actions a user can make on the queue (read, peak, update/add/process message)
  • Server stored access policy – to generate offline Shared Access Signatures that can be very easily revoke or re-issues without having to change the account key
In the next example we will see how we can create a Shred Access Signature for a given queue. The first step is to get a reference to our queue.
CloudQueueClient queueClient = myAccount
                    .CreateCloudQueueClient();
CloudQueue myQueue = queueClient
                    .GetQueueReference("myQueue");
myQueue.CreateIfNotExist();
Next, we need to create the access policy for our queue. In our case we will permit the consumer to process the messages from our queue for an hour. In this example the consumer have an hour from the moment when we generated this access policy, but we could set also a start time from when this policy is available.
SharedAccessQueuePolicy sharedAccessPolicy = new SharedAccessQueuePolicy()
{
    Permissions = SharedAccessQueuePermissions
                    .ProcessMessages,
    SharedAccessExpiryTime =
                    DateTime.UtcNow +  TimeSpan.FromHours(1)
};
The shared access policy that we created need to be added to the queue. Each policy is identifying by a unique id. Based on this id we can remove a policy. After we set the permissions the consumer will be able to access our queue.
string policyIdentifier = "QueuePolicy1";
QueuePermissions queuePermissions=
    new QueuePermissions();
queuePermissions.SharedAccessPolicies.Add(
    policyIdentifier,
    sharedAccessPolicy);
myQueue.SetPermissions(queuePermissions);
Now, we have set the permissions on the queue. What we need now is the shared access signature. This signature is a token that need to be shared with the consumer. Using this token any consumer will be able to access our queue and process our messages.
string accessSignature =
          myQueue.GetSharedAccessSignature(
               new SharedAccessQueuePolicy(),
               policyIdentifier);
The only think that the consumer needs to do is to use the Shared Access Signature that we provided to generate a storate credentials. Based on this credential we can get a reference to the queue.
StorageCredentials storageCredentials =
     new StorageCredentialsSharedAccessSignature(    
          accessSignature);
queue = new CloudQueueClient(
               "http://myExampleQueue.queue.core.windows.net",
                storageCredentials)
                    .GetQueueReference("myQueue");
CloudQueueMessage messageFromQueue =
          queue.GetMessage(TimeSpan.FromMinutes(2));
In this post we saw how we can create a Shared Access Signature for a queue. It is very similar with the methods that were used for blobs. The big advantage to use Shared Access Signature to a queue is that we provide a limited time and a specific access type for a consumer of messages (or for a producer). In this way it is very easy to manage the persons that have access to the queue. Also using Shared Access Signature we can provide access only to a queue and not to all the storage account.
Tutorials about Shared Access Signature:
  1. Overview
  2. How to use Shared Access Signature with tables from Windows Azure
  3. How to use Shared Access Signature with blobs from Windows Azure
  4. How to use Shared Access Signature with queues from Windows Azure
  5. How to remove or edit a Shared Access Signature from Windows Azure 
  6. Some scenarios when we can use Shared Access Signature from Windows Azure

Comments

  1. Very useful set of tutorials. One question. I am running on a clean install of VS2012 with the latest VS2012 version of SDK 1.7 (1.7.1?). With this types like 'SharedAccessQueuePolicy' are not recognised. Any ideas?

    ReplyDelete
    Replies
    1. What version of .NET are you using in your project. You should use 4.0 for now. From what I know the support of Windows Azure in .NET 4.5 is not 100%.

      Delete
  2. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment

Popular posts from this blog

How to check in AngularJS if a service was register or not

There are cases when you need to check in a service or a controller was register in AngularJS.
For example a valid use case is when you have the same implementation running on multiple application. In this case, you may want to intercept the HTTP provider and add a custom step there. This step don’t needs to run on all the application, only in the one where the service exist and register.
A solution for this case would be to have a flag in the configuration that specify this. In the core you would have an IF that would check the value of this flag.
Another solution is to check if a specific service was register in AngularJS or not. If the service was register that you would execute your own logic.
To check if a service was register or not in AngularJS container you need to call the ‘has’ method of ‘inhector’. It will return TRUE if the service was register.
if ($injector.has('httpInterceptorService')) { $httpProvider.interceptors.push('httpInterceptorService&#…

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine:
threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration:

TeamCity.NET 4.51EF 6.0.2VS2013
It seems that there …

Run native .NET application in Docker (.NET Framework 4.6.2)

Scope
The main scope of this post is to see how we can run a legacy application written in .NET Framework in Docker.

Context
First of all, let’s define what is a legacy application in our context. By a legacy application we understand an application that runs .NET Framework 3.5 or higher in a production environment where we don’t have any more the people or documentation that would help us to understand what is happening behind the scene.
In this scenarios, you might want to migrate the current solution from a standard environment to Docker. There are many advantages for such a migration, like:

Continuous DeploymentTestingIsolationSecurity at container levelVersioning ControlEnvironment Standardization
Until now, we didn’t had the possibility to run a .NET application in Docker. With .NET Core, there was support for .NET Core in Docker, but migration from a full .NET framework to .NET Core can be costly and even impossible. Not only because of lack of features, but also because once you…