Monday, November 28, 2011

Installing SSL certificates to Windows Phone 7 emulator

The days are again getting tight due to new technologies like Windows Azure, Windows Phone 7 & iPhone development. It is always interesting to work in new technologies where we learn so many new things ,hacks etc…My recent project uses Windows Azure as server side technology and mobile as client side. For the time being we are using iPhone as client.

When we start with Azure we could see that we can simulate the https scenario even in the emulator which is more like the production environment. Really like it. But after some time, it became trouble. There were not enough MAC machines where we can test the iPhone application. So I moved to WP 7 as client for testing purpose. I integrated the Azure tool kit for WP 7 and when tried to access the Azure ACS, it started throwing errors .Our REST based services hosted using HTTPS in emulator are not working. ie simply says network error.

After a google we could see that it is due to lack of certificates. If it was some other .Net client I can live with ServicePointManager and its validation callback. But unfortunately there is no such ServicePointManager in Silverlight. So it is proved that WP7 never allows you to access the https url without proper certificate. But how to install certs in WP7 .When I asked my fellow developer who has Windows Phone, he told that he normally send the certificate file (.cer)through mail and the opening of attachment in phone will prompt for installation.

But where is the mail client in simulator. Tried to send to my gmail id. But no luck.Then I tried the next way. Export the certificate into .cer format, host the .cer file in IIS and access it from Phone’s browser.Yes it worked. I was able to install the certificate. But again I got error in getting data through REST service. That was due to certificate URL issue .I should have used the exact certificate which is created for the IIS. ie for the localhost/127.0.0.1.

Below are the steps to install the certificate in WP7 and get the Azure REST services working which is hosted in Azure emulator.
  • Export the certificate as .cer
    • The certificate should be for the exact web address. ie export the certificate for 127.0.0.1 from IIS.
  • Host this certificate .Even in your http://localhost/<cert Name>.cer
  • Make sure your IIS has the proper MIME type for the .cer
    • MIME type for .cer certificate file is application/x-x509-ca-cert
  • Browse to this certificate path from Windows Phone 7 emulator.
  • Install the certificate.
Now you are all set to access https service hosted Windows Azure emulator from Windows Phone 7 with debugging support.
Enjoy… production environment in development.

Monday, November 21, 2011

Azure SDK 1.6 released.But don't use till Azure Guest OS supports

I came to know that Windows Azure SDK 1.6 released and as normal I jumped into it. I did a caching framework and when I tried to host in Azure it started giving me errors .Since I am new to Azure it took me good amount of time to figure out what is going wrong. After hard googling I could see that the Azure is still  not supporting the SDK 1.6 in its production environment. Check out the link below link which explains Azure Guest OS and SDK compatibility




As of now the latest Guest OS is Windows Azure Guest OS 1.16 (Release 201109-01) which supports Azure SDK 1.5 or older.

Monday, November 14, 2011

Generic retry mechanism & catch using generic exception type

Couple of weeks back we faced one situation where a portion of our code breaks due to a IIS not available situation and we were advised to implement a retry mechanism.Hope I can talk about that server down problem in future posts. The retry mechanism needs to implement based on the exception which will occur on that particular scenario. We tried test code by putting the try catch block and could see its working in one place. But there  are a bunch of other areas we need to modify the code.Also in future too developers may need to use this retry mechanism.
The architect resting in me jumped and said that we can use a generic retry mechanism using .net generics. My colleagues were in little confusion about how to achieve a generic mechanism for retry where the size and location of code differs and spread out in 150 projects.With the help of Action delegate I was able to convince them and the subsequent google gave us ready made code to do the same.
public static void ExecuteWithRetry( Action action, UInt16 noOfAttempts )
{
    //Validate args:Throw exception on retryCount=0
    int count = 1;
    for (; ; count++)
    {
        try
        {
            action();
            break;
        }
        catch (ReTryableException ex)
        {
            Log(ex, count);
            if (count == noOfAttempts) throw;
        }
    }
}

Everybody became happy like after the first fight seen in movies. We know that the movie needs to complete its 2 hours with excitement and for that villain  should come back with more power. Here too the villain come back when the perfectionist in me put forward the suggestion of making it real generic so that we can move to the utility project and can be consumed by other fellow developers as well. So we rewrote the logic as follows.


public static void ExecuteWithRetry<TException> ( Action action, UInt16 noOfAttempts ) where TException :Exception   
{
    //Validate args:Throw exception on retryCount=0
    int count=1;
    for(;;count++)
    {
        try
        {
            action();
            break;
        }
        catch (TException ex)
        {
            Log(ex, count);
            if (count == noOfAttempts) throw;
        }
    } 
}

Oh what an idea sir ji…There was one more suggestion came to make retry after specific interval. But before that we tried to integrate modified function in our app.

Catching exception using generic type

Here starts the second part of the story. When we run from Visual studio it didn’t hit the catch block!!! Theoretically there are no issues but practically it failed. At least for some moment surrounding people even I thought that our understanding about either Exception handling or generics is wrong. Even we thought of getting rid of this concept and write redundant retry code everywhere. But when we came back to our original state , we googled it and find that it is a BUG in .Net when running inside Visual Studio debugging mode.We tried running by double clicking the application and it works perfect.We were also able to see this bug in the Microsoft connect and says its fixed.

There is on workaround available for this issue its as follows

catch (Exception ex)
{
    TException tEx = ex as TException;
    if (tEx != null) { /*Process */}
    else throw;
}

This changed my perception

“The bug is there only due to poor dev’s coding mistake never by compilers, development tools or the framework”

to

“Compilers, development tools and frameworks are also coded by developers”.

Later I happen to read a post about fixing a compiler bug which enforced my perception.

Tuesday, November 8, 2011

Yes.I was able to run Azure apps in emulator

Role instances are taking longer than expected to start. Do you want to continue waiting?”

The above error message must be very familiar to the newbies of Windows Azure when they try to run Azure application in Azure emulator .When we search for this error msg, we get so many links which says the experience of resolving this error. My situation was also not exceptional .Among my team members only my machine got this disease.

When I opened the first link I could see a list of 14 check points to be corrected to get the emulator working. I went through each and every point and verified it.But no luck. There was an interesting solution that we need to opt for retry and it will work .I tried clicking on the Wait button 10-15 times without any luck. Another forum said this was an identified issue in Azure sdk 1.3 and fixed in 1.4 .But I am getting the issue even in Azure 1.5.

There were some guys who got it working by reinstalling the Azure sdk and some went to the extend of reinstalling OS. That was not my option because we are using a OS image which has some special softwares pre installed. So I tried to dig into the inner details of emulator working.

Inner details of Azure emulator

When I started digging into emulator the first thing I could see is it will create a temporary site in IIS 7.But when I looked in IIS, I was able to realize that the issue is with creating the temporary site for Azure emulator. After that I tried opening the system tray icon of emulator and checked the emulator console view by clicking on the ‘I’ button. There was nothing. Then I decided to check where are log files of Azure emulator?

It was simple the location of Azure emulator logs is

c:\users\<username>\AppData\Local\dftmp\

Now I got a bunch of error log files and started analyzing it and could see a good error (sometimes errors are better than nothing) from the \dftmp\DevFCLogs folder which said

“There is already a listener on IP endpoint 127.0.0.1:12001”

The next google took me to a strange msdn page which asked to me to free the below ports.

12000, 12001, 12002, 808, 16001, or 15100.

After freeing the ports I was able to run the application successfully in emulator .But look at the port 808 which I an using for net.tcp binding and essential for my other project.That means I need to change that port or work on only one project at a time. When I googled again I could see that there are some more people with same issue with the 808 port

Summary

If you follow the step by step instruction to install Azure sdk and got it working in the first shot, you are the lucky person in Azure world. Otherwise you are screwed especially if you are the coder. I was lucky that I am not the developer in that project and could survive some days by drawing boxes and arrows Smile

If you are in the same situation where I was go through the Azure SDK 1.5 known issues and try the workarounds. Also look are the “Getting Started with Locally Running a Windows Azure Application” page in MSDN. Then start looking at the 14 check points mentioned above.Add the unblocking of ports as 15th check point. Finally goto the logs folder and google it. I don’t think there is any need to reinstall the OS.

My initial opinion about Windows Azure is, it’s a cloud in all means. An unclear dark cloud which may cause heavy rain at any time to which we are throwing our apps and data. Another complaint is about the Azure sdk which is not mature even in its v 1.5 to at least block the direct URL access to WCF REST service by configuration where ACS protects the service. Hope I can post more about Azure later.