Friday, February 28, 2014

Moving ACS Settings in Cloud Configuration File - Windows Azure Cloud Services

All right folks, This is weekend start and clock is ticking at 5:56AM ,huh.. I am wondering what I am doing by getting up so early this weekend, but anyways.. let me first share this post with you all while I do the thinking in the background..so lets get started..

Well, I wanted to see this issue since long but I didn't get time to investigate more on it, but few days back I had do to it.

What was the issue?

Typically when we want to configure the authentication settings of users of the web application then we use web.config and put all necessary settings there, and so similar is the thing when you want to configure Windows Azure ACS settings in your web application.
Visual Studio provides a great way to do it using nice GUI which is known as Identity and access configuration wizard, you can download it here for Visual Studio 2012 - http://visualstudiogallery.msdn.microsoft.com/e21bf653-dfe1-4d81-b3d3-795cb104066e

Once you are done with configuring all ACS settings in the web.config file and application starts to work fine, you might want to move these settings to cscfg file of your cloud service. Why??
Here is answer to why -
When you put settings in your cscfg files, you can directly go and modify those using Windows Azure management portal without re-deploying the application.
So in this case too, I wanted have more control over ACS settings which were lying in the web.config so far so I was looking for a way to do it.

What is the solution?

Well, there is this excellent post on step by step how you can do it.
Refer it here - http://www.cloudidentity.com/blog/2011/05/31/EDIT-AND-APPLY-NEW-WIF-S-CONFIG-SETTINGS-IN-YOUR-WINDOWS-AZURE-WEBROLE-WITHOUT-REDEPLOYING/

What is basic idea and approach taken here?
Well, if you take a look at the link, the smart approach is taken to override the ACS settings in the WebRole start event using Linq to XML.
Its quite easy to understand - it goes this way
  • Modify your cscfg and csdef to include settings which you want to override
  • Modify WebRole.cs on start method, find the web.config file of your application through code and then modify it using Linq to XML.
and that's it , you are set.

Few points to mention here -
  • Make sure to set the executionContext to elevated
  • Add reference of assembly Microsoft.Web.Administration to your application, you can install nuget Microsoft.Web.Administration
The code which actually modifies the web.config mentioned in the URL might not work for you as it is because you might have added certain customizations while configuring those ACS settings in your web.config file, but not a worry - as a good developer you will be able to tweak it easily.

That's it guys.. Hope you get the idea and if you are also a believer of making things configurable, then I am sure you will find this useful.


Thursday, February 27, 2014

Get MIME Type by file extension

I was searching for a way to get the MIME type of a file by file extension and .NET Framework 4.5 comes to the rescue.

You can make use of API GetMimeMapping which is found in the .NET Framework 4.5 System.Web.dll

Here is the documentation of it - http://msdn.microsoft.com/en-us/library/system.web.mimemapping.getmimemapping.aspx

Example on how you can use it

string mimeType = System.Web.MimeMapping.GetMimeMapping(fileName);

If you are running your ASP.NET web application in the integrated mode in IIS then this API actually uses the list of MIME types in the IIS and so seems to me that even if your IIS MIME types list gets upgraded, you dont need to update your code, huh..cool .. isn't it?

Hope you find this useful.

Reference - http://blogs.msdn.com/b/webdev/archive/2013/02/14/getting-a-mime-type-from-a-file-extension-in-asp-net-4-5.aspx

Monday, February 24, 2014

Windows Azure cannot perform a VIP swap between deployments that have a different number of endpoints - Error solved

I have been taking a look at Windows Azure VIP swap feature and started facing this issue.

What this issue is all about and what is VIP Swap?

I had deployed an application on the production slot of a cloud service and the test application was deployed on the staging slot of same cloud service.
When you have both deployments of a cloud service up and running, you can perform a VIP swap which is nothing but the swap between your virtual ip addresses of production and staging deployments and doing so your production deployment becomes the staging and staging deployment becomes the production deployment.

But then after trying to perform VIP swap I started facing an error which says
Windows Azure cannot perform a VIP swap between deployments that have a different number of endpoints.
Though error was too obvious and pointing to the exact issue still as a typical developer I preferred to do a quick search on web and it seemed like its a very common issue which is faced by many people and so documenting the possible solutions.

All right, what is the solution?

Solution is quite easy as error is descriptive enough - check the number of endpoints of your cloud service deployments.

  • Check the cscfg files of both deployments i.e. production and staging. The number of input endpoints on both deployments should match.
  • Check endpoint ports of both slot's deployments e.g. Https / Http. (443/80)
  • Check if one of your deployment has remote desktop enabled on any of it's role instances because it ultimately ends up opening another port internally on your cloud service. If remote desktop is enabled then it needs to be enabled on both deployment slot's instances.

Takeaways:

Ideally your both deployments should match exactly because whenever you will want to put something on the production slot by swapping it with staging then you must have to make sure that it works correctly and this is why the staging slot is used for internal testing purpose, however there might some scenarios where you might want to have different deployments on both slots e..g Site under maintenance on production slot while you upgrade your deployment on staging and test it. So make sure to have a close look on the endpoints of your both deployments and now you know how.