Pages

Monday, November 17, 2014

Enabling Web Deploy for Azure Web Roles Without Visual Studio

We all know that how easy it is to enable remote desktop and web deploy on azure roles with the help of Visual Studio.

All you need to do it check appropriate check boxes shown in the nice deployment wizard.


























But.......
the question is, how can I do it when I am not using VS for deployment or say how can I do it when I am using TFS builds for automated deployments to azure?

Well, this is quite easy.
First, the basics. When you enable the web deploy for the role - all it does it opens up a port (8172 to be specific) on your role so that you can push those files real quick.

Now to achieve this without Visual Studio, all you need to do is, include following optional parameters to msbuild command and you are done.

msbuild /p:EnableWebDeploy=true;webDeployPorts=Your_WebRole_Name:8172 /t:publish Your_Azure_ProjectName.ccproj

I tried integrating this with TFS builds and works like a charm. You can verify it by logging in to the Azure portal and the dashboard of your cloud service shows list of input endpoints available.

Hope this helps someone. Enjoy :)

Monday, September 1, 2014

Unresponsive DataCacheFactory Constructor Initialization

Hey folks,

This was one of the most strange issue I saw today and it is with Microsoft Azure Managed Cache Service APIs.

What was the issue?

We were using Azure managed cache 2.3.1 APIs and everything was set including changes needed in the configuration files.

Here is what typical snippet to initialize the cache factory looks like


static DataCacheFactoryConfiguration cacheFactoryConfiguration = new DataCacheFactoryConfiguration("default");
static DataCacheFactory cacheFactory = new DataCacheFactory(cacheFactoryConfiguration);


The line 2 in above snippet was the issue. As soon as the compiler used to hit the line which actually initializes the DataCacheFactory object from config file - compiler never returned.

In the very first line of this post I mentioned a word strange because - the snippet above worked on each machine (windows 8 / windows 8.1) with no issues however there was a colleague of mine who was facing the problem who had exact same configuration as that of other machines.
Adding more suspense to the scenario, this only didn't work with the web application. When we tried creating a quick console application pointing to the same cache service on same machine - it worked. interesting the world of programming, isn't it?

 What is the solution?

Well, after long search on this, mostly found links about best practices of using DataCacheFactory class but nothing near the actual issue. At one point - started feeling that are we the ONLY guys in entire universe who are facing this real strange issue? but .. there is one thing you can keep doing and that is keep trying.. and we headed towards searching more on this by altering keywords here and there and then we found something

found a post of stackoverflow in which the guy described about exact same issue and solution too, but the real reason behind this error is still unknown to me ( or to the post owner too). If anyone knows - feel free to leave a comment.

 There are two components which you might find installed on your machine.i.e
1. IIS Express Application Compatibility Database for x64 and
2. IIS Express Application Compatibility Database for x86

All you need to do is, uninstall those, restart your visual studio and you are done.

[Adding further notes which were not there in the post mentioned above]
After this, if you start facing the error -
Specified argument was out of the range of valid values. parameter name site.

Don't get panic, simply install IIS on your local computer. Here is how you can do this
Open Control Panel > Turn Windows features on or off  > Check Internet Information Services and click ok.

Restart visual studio and you are good to go.

Hope this works for you and saves your efforts of searching solution for this strange issue.

Wednesday, May 21, 2014

The "DeleteCurrentDeployment" task failed unexpectedly - Error solved

Hey folks, sharing a quick solution on a strange issue which I faced today.

I was having both Azure SDK installed on my machine i..e SDK version 2.2 and 2.3, I went ahead and uninstalled all components in SDK 2.3 and tried building my one of azure project which was based on SDK 2.2

Here was the issue I was facing after cleaning and building solution using Visual Studio 2013

The "DeleteCurrentDeployment" task failed unexpectedly.
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.ServiceHosting.Tools, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
File name: 'Microsoft.ServiceHosting.Tools, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
   at Microsoft.Cct.Debugging.DevFabricService..ctor()
   at Microsoft.Cct.CctProjectNode.<get_DevFabricService>b__14()
   at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Func`1 valueFactory)
   at System.Threading.LazyInitializer.EnsureInitialized[T](T& target, Func`1 valueFactory)
   at Microsoft.Cct.CctProjectNode.get_DevFabricService()
   at Microsoft.Cct.CctBuildDeploymentTaskHost.DeleteCurrentDeployment()
   at Microsoft.CloudExtensions.MSBuildTasks.DeleteCurrentDeployment.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__20.MoveNext()


Solution:

Re-install the Azure emulator using Azure SDK 2.2 and hurray!! the error is gone.


Saturday, March 1, 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.

Sunday, January 19, 2014

Inconvenient MVC Web Optimization Framework and High CPU Utilization

While I am writing this post I am bit relaxed and listening to 'bollywood' music so don't wonder if you find anything off topic or typos :), lazy weekend!!

What was the issue?

Last week, while working with the Window azure cloud services we tried to stress test it and guess what we found? CPU spikes!!
Initially we thought that this might be something related to load on the server as it was a small VM instance but at the same time the results were unbelievable as number of concurrent requests to the server were comparatively less. This gave us the kick start and we started to drill down to the real issue.

The approach:

It all started with several meetings with few brainy people around as I was clueless, some even directly said man your application got memory licks..but then
Thanks to the Visual Studio profiling framework which helped me a-lot to drill down into real issue about this CPU spikes. You can read about how to use this feature and how it helps in my blog post here Profiling windows azure cloud services - http://passionatetechie.blogspot.in/2014/01/profiling-for-windows-azure-cloud.html

When I tried to stress test the service and tried to collect the profiled logs , found that there were these two methods which were taking almost more than 80% of CPU, wowwwwww!!
1. Scripts.Render
2. Styles.Render

 Note - I was using ASP.NET MVC web optimization nuget version 1.0

Irony is, these methods are from the web optimization framework of ASP.NET MVC which is suppose to improve the performance of your web application by bundling the java-scripts and css files. More info about bundling here - http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

All right then of course the straight way to remove these calls from application and try to stress application again and check results. we did exact same thing and this time application performed pretty well!! no CPU spikes!! man bingooo!!!!!!

But then why in my application the optimization framework was doing something like this? why ? why why and why.......? this question even appeared in my dreams!!
I started search around it but didn't find something helpful, I even started doubting my decision about removing those calls from the application unless I hit this post here
http://stackoverflow.com/questions/14210721/mvc-4-on-azure-scripts-render-bundles-jquery-slow and
http://stackoverflow.com/questions/12230246/azure-cache-preview-outputcache-high-cpu-slow

Here is why and it is purely based on what I understood

When you use the bundling (Web optimization framework) - server uses the caching.
But now as you are mentioning the setting in your web.config file that you are not using default caching of server anymore instead you want to use the Windows azure distributed cache.
Like this


<caching>
  <outputCache defaultProvider="DistributedCache">
    <providers>
      <add name="DistributedCache" 
type="Microsoft.Web.DistributedCache.DistributedCacheOutputCacheProvider, 
Microsoft.Web.DistributedCache" cacheName="default" dataCacheClientName="default" />
    </providers>
  </outputCache>
</caching>

So seems this web optimization framework doesn't understand this distributed cache and it simply ignores the caching part.
Due to this it tries to recompile your bundles every time with each request which causes higher CPU utilization.


I also had the distributed cache related settings in my application's web.config , so I decided to remove both from my application i.e. Bundling as well as distributed caching - it worked and CPU utilization never touched the boundary.


Takeaways:

In case you are facing the same issue of high CPU consumption on the web server in your ASP.NET MVC application or cloud service then
1. Run the VS Profiler locally and see whats the root cause of it
2. If you find the Optimization framework APIs are taking most of the CPU then try removing it and stress your application again to see the results.

Next step:

According to this link http://aspnetoptimization.codeplex.com/workitem/46 , its the discussion on codeplex on very same issue but If you go to the comments section then someone said that they have fixed it in the next version of nuget. (i.e. post version 1.0)
I havent tried using next version of this nuget but when I will do it I will update this post.

Hope this helps and stops wondering someone about why my CPU percentage is higher when using web optimization framework of MVC.







Saturday, January 18, 2014

Profiling Windows Azure Cloud Services

Hi again guys,

Last week I was working really close with performance related things of the web applications and this what I came across for Windows Azure Cloud services.

Profiling is one of the great feature of Visual Studio and I almost fell in love with it. why? because first of all its quite descriptive and helps to measure the memory / CPU monitoring of your application and second is it can be configured in both ways i.e. while running your application locally as well as while running your application in the cloud.

I will not go into much deeper on how to use this feature and how it helps, all you need to do is click on the link to know and you are done.
http://msdn.microsoft.com/en-us/library/2s0xxa1d.aspx

You can use this great feature to go into the quite deeper understanding in terms of how your application is consuming resources on the server and it helps in deciding to scale up or scale down your application needs. More information on it at http://msdn.microsoft.com/en-us/library/dd264994.aspx

But all right , lets come back to the Cloud arena and let see how can use this for Windows Azure cloud services.

basically you can use this feature while deploying your application using VS to cloud deployment wizard. (Publish)

A great article http://msdn.microsoft.com/en-us/library/windowsazure/hh369930.aspx#BK_ProfilingCloudService which explains the approach about how you can do it pretty easily while deploying to Azure through your VS.

I was interested more on what special this VS does when we check the check box of Enable Profiling in the publish wizard? and here is what I found

Basically VS adds few entries in your cloud service definition and cloud service configuration files.

(I am using Windows Azure SDK 2.2)

In csdef
 <InternalEndpoint name="Microsoft.WindowsAzure.Plugins.Caching.cacheArbitrationPort" protocol="tcp" />
  <InternalEndpoint name="Microsoft.WindowsAzure.Plugins.Caching.cacheClusterPort" protocol="tcp" />
  <InternalEndpoint name="Microsoft.WindowsAzure.Plugins.Caching.cacheReplicationPort" protocol="tcp" />
  <InternalEndpoint name="Microsoft.WindowsAzure.Plugins.Caching.cacheServicePortInternal" protocol="tcp" />
  <InternalEndpoint name="Microsoft.WindowsAzure.Plugins.Caching.cacheSocketPort" protocol="tcp" />


In cscfg
<Setting name="Profiling.ProfilingConnectionString" value="YourStorageAccount" />
<Setting name="CloudToolsDiagnosticAgentVersion" value="2.2" />


But then I also found that the size of the deployment packages increases when you enable this option of profiling, and that's because VS also adds the 64 bit profiler in your package.
So I am not sure about how you can use this feature when you dont want to publish using VS to your cloud service, however article http://michaelwasham.com/2011/08/10/deployment-to-windows-azure-fails-with-profiling-enabled/ explains how can you do it but as it is written for older version of SDK so I am doubtful if this works for later versions of Windows Azure SDK.

Overall profiling feature managed to impress me with with it's native integration with Windows Azure Cloud Services.

Once you enable the profiling and deploy the cloud service, VS 2012 Server explorer helps you in getting report out of your profiling build with just a single click and you are set to analyze the report.

Tuesday, January 14, 2014

Converting Lists to Collections in C# and Using Linq on it

All right guys, this one is one of the shortest blog post I am doing today and hope it helps people like me who wasted almost few minutes searching for solution on this.

Basically .NET Framework offers us too many collections however two of those are
1. List
2. Collection

We all know Linq, i.e. this nice and slick feature of .NET to query various collections however when it comes to Collections then we get this famous typecasting error
i..e Cannot convert IEnumerable to Collection etc etc..

Well, after several minutes of discussion with my brainy peers and some search on internet here is what I have done

Its a workaround which works in two steps
1. Convert your Collection to List and perform Linq on it.
2. Convert back your List to Collection. How? here we go

Collection<Entity> collection = new ObservableCollection<Entity>(collection.ToList().Distinct());

And you are done.

Hope this helps someone.