Timeout issue at Azure Scheduler

Found an issue on implementing service execution using Windows Azure Scheduler. The triggered call is a long running one. Scheduler times-out after 60 seconds, as waiting for response to request made by the job.

The solution I came out with, for my situation, was to modify service code (Web API), making method call asynchronous, so execution of the action method called by Scheduler job request continues and finished on time.

This is the code I used for calling the method asynchronously, at the Web API action method:

Task.Factory.StartNew(() => MyMethodClass.MyStaticMethod(parameter));

Details: http://social.msdn.microsoft.com/Forums/en-US/46f56a6f-204a-416f-b267-b558dbeeffa2/how-to-change-the-timeout-for-azure-scheduler-job?forum=azurescheduler

Cheers,

Danfer.

Starting the year – Improvement through creativity

First post for 2014. I made my New Year’s resolutions, professional field, creating great software with Zgura:

  1. Learn, get certified for Web development, exams 70-480, 70-486 and 70-487 link
  2. Apply SOLID development principles
  3. Always try to improve through creativity

On 3, I’d like to comment on the ideas expressed at Fareed Zakaria GPS program, in an interview to Elon Musk GPS link, summarizing, people working at his company must not just develop technology, they must improve on what has been developed. Just doing one work is not enough, need to improve on existent work.

As such, I’d like to share on an improvement I just implemented, developing on work already done by my colleagues at Zgura:

As we develop ASP.Net MVC apps and deploy continuously from Git into Azure Websites, one website for each stage of development and branch on source control (i.e., development, test, production) we wanted to set and use configuration environment variables for between other things, connection strings to databases, in Azure Websites and get them from Environment variables, using:

public static string ConnectionString
        {
            get
            {
                return Environment.GetEnvironmentVariable("APPSETTING_ConnectionString,"varname");
            }
        }

But after implementing that, we had an issue when trying to do local debugging as the application doesn’t have access to the environment variables repository. A solution I found was to add that variables at Application_Start, under Global.asax.cs:

#if DEBUG
            Environment.SetEnvironmentVariable("APPSETTING_ConnectionString","varname");

#endif

Cheers.

P.d.

As per a comment from my friend Salvador Aguilar, I’d like to show a screenshot of deployment configuration into Azure websites.

Continuous Deployment Azure

Upload and download file to Azure Blob storage

Windows Azure includes a storage service that allows us to store binary and text data as files. This data is stored in BLOB (binary large object). For each Azure subscription we can have up to 5 storages accounts. Each storage account can have up to 100TB of data. More information at: Azure Storage.

We can use BLOBS to store files from an application. Two operation we will need to implement will be upload and download files.

I have implemented both, upload and download methods and use in MVC 4 web applications running on Azure.

I’m showing here code for upload and download methods, the kind I use with MVC 4 web app. In this case, although we are deploying to Azure, we are doing it to a Azure website, so no need for having a cloud project in the application solution. I implemented these methods at a separated model project.

public class AzureStorage
    {
        public void UploadToBlob(string fileName, Stream stream, string containerName)
        {
            //Bellow, not working, use this if using cloud project, when deploying to services
            //CloudStorageAccount storageAcct = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);

            CloudStorageAccount storageAcct = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);

            CloudBlobClient blobClient = storageAcct.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(containerName);
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
            stream.Position = 0;
            using (var fileStream = stream)
            {
                blockBlob.UploadFromStream(fileStream);
            }
        }
public string DownloadBlob(string fileName, string containerName)
        {
            string uri = string.Empty;

            CloudStorageAccount storageAcct = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);

            CloudBlobClient blobClient = storageAcct.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(containerName);
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);

            using (var fileStream = File.OpenWrite(fileName))
            {
                blockBlob.DownloadToStream(fileStream);
            }
            uri = @"http://MyAccountName.blob.core.windows.net/" + containerName + "/" + fileName;
            return uri;
        }

As I was implementing the methods in a separated project (Model, in my case) I had to install Windows Azure Storage library, from Nuget and System Configuration library. In addition, be aware that I’m using release 2 of Azure libraries (instructions to use may change on previous versions of these libraries).

One of the practical uses for upload and download is for generating files using EP Plus and saving them at blob so to allow for later download by users. I’ll write another article to show how to use EPPlus. One of the interesting aspects of its use I’m currently implementing is mixing localization and use of localized data annotations so, instead for example writing the DisplayName of a property at a class like:

[DisplayName(“Inventory Code”)]

I use something like:

[Display(“Inventory_Code”)]

Where Inventory_Code is a resource key that points to a value, so, it could be Inventory Code or Código de Inventario. Now, imagine, you only have to set the data annotation at the property level and that will be used by views helpers to show the display values. In addition, I’m using data annotations for the headers I set for the Excel file I generate using EPPlus so localization will apply too to generated files.

More in following posts.

Cheers.

Note: in the specific case I was working on, since what I want is have the user to download files directly from Azure Blob, I just had to use the following code for downloading:

public string DownloadBlob(string fileName, string containerName)
        {
            return @"http://MyStorageAccountName.blob.core.windows.net/" + containerName + "/" + fileName;

        }

Testing sample REST service using Web API and Azure

I did the example at http://www.windowsazure.com/en-us/develop/net/tutorials/rest-service-using-web-api/

As I was discussing with Anders and Alejandro in a previous post, there is another alternative to access data models and that is using Web API and script blocks. I think this sample helps to clarify in how to do that.

The key parts concerning use of Web API, I believe, for this sample are:

public string Self
        {
            get
            {
                return string.Format(CultureInfo.CurrentCulture,
                    "api/contacts/{0}", this.ContactId);
            }
            set { }
        }

At the Contact class definition.

Notice the use of the property Self at the script block at the Index view for Home controller. Notice how Self property is used to connect with the methods at the API type controller Contacts.

You can see the site at http://contactmgr23.azurewebsites.net/. You can also try adding /api/contacts to the URL and see what happens, it will be shown differently at Firefox and Chrome than as in IE.

Note:Julio Avellaneda has a serie of posts in Spanish on Web API. http://julitogtu.com/category/asp-net-web-api/ Julio participates too at FB group Desarrolladores Nicaragua.

Free 20MB SQL Database in Azure

If you want to get started with Windows Azure, developing your first application, you can use Azure websites, you have up to 10 websites you can use for free.

Adding to that, you can have up to 1 20MB SQL database, per subscription, per the first 12 months of use of your Azure subscription.

This is great because we can use the free options, Azure websites and SQL database for learning and for testing without spending any money. To configure a website to use the free database, just select the appropriate database type, when adding the new website, through custom create:

Create Free Azure DB

More details on pricing at http://www.windowsazure.com/en-us/pricing/details/web-sites/

 

windows Azure Platform as a Service (PAAS)

Two of the main PaaS components in Windows Azure are Azure Websites and cloud services.

As a developer of applications, based on advice of friends and experience, I recomend using Websites for testing, proof of concept and applications not requiring acess to the virtual machine that support the service. In addition. I’ll use a cloud service if require a staging environment for deployments, so, once new version gets approved I will just swap virtual IP addresses and new version gets in production.

This is a running example of a website we deployed at Coding Dojo, past June 23rd: http://codingdojo2.azurewebsites.net

Costs: free the website. If you want to use a custom domain name (e.g.,myapp.mydomain.com) you have to upgrade to Shared Website, another category of service inside websites in Azure that has a monthly cost of approximately $10/month.

There is more information at: http://www.windowsazure.com/en-us/
I’ll be wrting soon on samples based on my exoerience.

Cheers.

Más sobre el Coding Dojo del 13 de junio

Aquí va el enlace al Azure Website de prueba que creamos durante la presentación de Azure:

http://codingdojo2.azurewebsites.net/

Los que estuvieron en la presentación, ¿se acuerdan de la idea que expresé? ¿como juntamos la geolocalización con aplicaciones móviles? (si recuerdan hice un demo de un Mobile Service de Azure en Android). Esto usando servicios proveidos desde la “nube”.

 

Saludos.