Tuesday, July 8, 2014

Microsoft Azure Web/WCF Role Consolidation with Relative Publish Folder Path

These days everybody is following SOA architecture so that there application logic can be access from various platforms. So in order to achieve this architecture everybody has minimum one WCF/Web Api service project and one Web Role in their application. Also is order to achieve Microsoft Azure SLA each role should have minimum two instance and should be of medium size. As per July 08,2014 Microsoft Azure Medium instance configuration is as

Name
Virtual Cores
RAM
Price Per Hour
Medium (A2)
2
3.5 GB
$0.16
(~$120/month)

So Medium machine has very good configuration for hosting a single web application. I developed multi-user heavy load application and we always recommend medium instance with variable instances. So if you have small application with 1000 users and two role (WCF and Web). then you need to pay for 4 instances which will be like 120 * 4 = $480/m for only hosting the applications and other data transfer extra. So in these cases we recommend role consolidation having multiple application hosted as website in single role. This is easy task but before that we need to know few thing. 
  • Whenever we host WCF/Web Role in Windows Azure it host the application in IIS. and in IIS we can create multiple application and Virtual Directory. So here we need to do something that we host our Web Role at port 80 (default port) and WCF role at other http port both as separate website. 
Lets do the real stuff. (I never wrote this much theory but it is important to know why we are doing this)
So suppose you have two roles in you application so you just need to Create two separate site nodes in you ServiceDefinition file and also need to create two end points for those sites as follow.

<WebRole name="WebRole1" vmsize="Medium">
<Sites>
      <Site name="Web">
             <Bindings>
                    <Binding name="Endpoint1" endpointName="Endpoint1" />
             </Bindings>
      </Site>
      <Site name="WCFServiceWebRole1" physicalDirectory="..\..\..\PublishDirectory\WCFServiceWebRole1">
              <Bindings>
                     <Binding name="Endpoint2" endpointName="Endpoint2" />
              </Bindings>
      </Site>  
 </Sites>
  <Endpoints>
      <InputEndpoint name="Endpoint1" protocol="http" port="80" />
      <InputEndpoint name="Endpoint2" protocol="http" port="8080" />
  </Endpoints>
</WebRole>

Now you can see i have created two Sites in Single Web Role and both of them have separate endpoint. I assign Webrole the port 80 so that i am able to open website with direct url like "myurl.cloudapp.net" and wcf at 8080 and it will hosted to myurl.cloudapp.net:8080

Also i want to point out one more thing which will justify this posts Header i.e Relative Publish Folder Path. 
If you see carefully for WCFServiceWebRole1 I have set the phsicalDirectory of that website as relative address. This is the beauty of this post and let me explain how i did that. What i did i just add the some publish script in MSBuild Which as as follows.

<PropertyGroup>
    <PublishPath Condition="'$(PublishPath)' == ''">$(ProjectDir)..\Publish\$(ProjectName)</PublishPath>
    <PublishConfigurationIgnore Condition="'$(PublishConfigurationIgnore)' == ''">Debug</PublishConfigurationIgnore>
  </PropertyGroup>
  <Target Name="PublishToDirectory" AfterTargets="Build" Condition="'$(PublishPath)' != '' AND '$(PublishConfigurationIgnore)' != '' AND '$(Configuration)' != '$(PublishConfigurationIgnore)'">
    <Delete Files="$(PublishPath)\**\*.*" Condition="Exists('$(PublishPath)')" ContinueOnError="true" />
    <!--<Delete Files="$(PublishPath)-Compiled\**\*.*" Condition="Exists('$(PublishPath)-Compiled')" ContinueOnError="true"/>-->
    <MSBuild Projects="$(ProjectPath)" Targets="PipelinePreDeployCopyAllFilesToOneFolder" Properties="Configuration=$(ConfigurationName);_PackageTempDir=$(PublishPath);PackageAsSingleFile=false;DeployOnBuild=True;DeployTarget=Package" />
  </Target>

So what above script will do during build it will publish the WCF Role application to "PublishToDirectory" folder and it also take care of the transformation or the web.config to. NOTE: It will not publish the site in case when Debug Mode is selected. So in Azure configuration i just give the relative address of that Publish folder relative to Project i am working with. So during this approach any developer who is working on this project so everytime when somebody map the TFS then need not to change the project publish folder as per their folder structure everything will taken care automatically.

SO BELOW IS THE IMAGE OF THE MICROSOFT AZURE IIS OF FOUR ROLE THAT WE CONSOLIDATED IN SINGLE ROLE (IN PRODUCTION RIGHT NOW)


ALSO YOU CAN FOUND THE COMPLETE CODE EXAMPLE OF MY ABOVE EXPLANATION AT HERE