Total votes: 0
Category: Windows Communication Foundation
Print: Print Article
Please login to rate or to leave a comment.
Published: 21 Mar 2012
In this article we are going to see how WCF plays a major role in application development on Windows Azure Platform.
In this article we are going to see how WCF plays a major role in application development on Windows Azure Platform. For the beginners WCF is a Windows Communication Foundation is a Microsoft framework to build application in Service Oriented Architecture. We can use WCF as a service to send data across the application as a service with the endpoints predefined to communicate. The service endpoints can be of type which is hosted on to an IIS Server and available anytime, or it can also be an application oriented service to provide on demand usage. The data transfer through the messages can be of any particular format that the sender and receiver can able to understand the communication over the protocol.
Consider using WCF to communicate between the application on to the Windows Azure Framework, Microsoft has provided with the Windows Azure AppFabric specifically built for building, deploying and managing the application that uses WCF communication framework. In Windows Azure AppFabric SDK we have the different tools and managing providers with rich set of API's to host the WCF application on to the Windows Azure platform with ease. WCF Service basically can be hosted on to the Windows Azure platform as a Web Role or a Worker Role. Role in Windows Azure is basically a set of managed code that runs on top of the Azure platform to perform some operations that are supported by the framework. We need to basically understand the usage of Web Role or a Worker Role before we start developing our application which uses the WCF communication to make an effective application development.
WCF as a Web Role is specifically used if the application communication is over the HTTP and HTTPS protocols with the endpoints available to the consumers. Also we can make use of the Web Role in the application to do a straight forward communication on HTTP Protocol using the basichttpbinding over the WCF Service model. When it comes to Worker Role it's completely the background processing of the application that communicates to the Web Role to feed the data as and when required. Basically when we have application that are running background for a longer run and required to process the data then we need to go with the WCF Service on the Worker Role model. In Worker role the communication is not taken over the HTTP protocol instead we need to go with the TCP/IP protocols that can expose the service over the ports other than 80 and 443 which are used internally. We need to use the netTCPBinding as the binding to get the communication channel as a WCF service oriented one.
For simplicity we take an example of an ASP.NET application that is hosted on to a Windows Azure Environment requires the service that is to be processed internally and externally then we can have the WCF Communication channel open as a Web Role which can be consumed and used by the consumers as its ported over the HTTP Protocol. Now take the case of the Worker Role when we want to have an ASP.NET application as a Web Role and needs to communicate internally then we have the options to have the WCF Service hosted as a Worker Role that can be configured such that a Worker Role running background can host as a WCF Service. In this article we will see the example of using the WCF Service as a Web Role and Worker Role in detail.
Let us split the task into two as WCF as Web Role and WCF as Worker Role. We will see the step by step process on how to achieve using the WCF Service in both the roles effectively one by one.
WCF as Web Role
A WCF Service that is hosted on to a Web Role in Windows Azure is similar to hosting the WCF Service on to IIS on the On Premise Server. Let us see in detail on the steps to host the WCF Service as a Web Role on the Azure Environment. To start with first Open Visual Studio 2010 IDE in an Administrator mode (By right clicking on the Visual Studio IDE and selecting Run as Administrator) and select File New Project. In the list of available template point to Cloud and Select the New Windows Azure Project and provide a valid name for the project as shown in the screen below.
On successful creating the project we can see a Wizard popping up with the list of available roles to select from as per the requirement. Since in this sample we are going to see on using the WCF service as a Web Role, Microsoft have already provided with the template to start using the WCF Service as a Web Role straight forward by selecting the WCF Service Web Role from the list. Click on the button provided to select the WCF Service Web Role from the list and we can see the template is added to the right side list window. Now click on ok to proceed further also we have the option to change the name of the WCF Service Web Role by clicking on the option provided at the template selection in the list box as shown in the screen below.
Clicking on OK button will create the project completely with the list of files required to run the application over the Azure Platform. We can see 2 projects will be created as first one is for the WCF Service project where we need to put our code logic which acts per the requirement for the consumers to get the service and the second project is the Azure Cloud project which is required to package the application and provide the required configurations to host the application on the Windows Azure platform.
Now let us start with putting our code logic to the WCF Service of adding 2 numbers and provides the result to the end user who passes the numbers to get the result. Open IService1.cs interface and add the service contract which takes 2 input parameters as shown in the code below.
In the above code we have just included the interface details without implementing any of the code base to add 2 numbers and return the result as a string. Now to implement the code logic open Service1.cs and add the logic as shown in the code below. Here we need to implement the logic which adds 2 numbers and then return the result as a string to the consumer who is consuming the service.
Now we are done with our implementation which adds the 2 given numbers and provides the sum of the numbers to the consumers. This is a simple example we have taken to make the developers comfortable, we can impose complex logic as well to do the business logic tighter and make use of the service effectively. Once we are done with the above 2 code logics now we need to make some configuration changes which will make the WCF communication over a particular binding. Make the below changes to the Web.Config of the WCF project, we have added a new binding configuration (basichttpbinding) to the existing Web.Config file as shown in the code below.
Now we are done with our code logic for the WCF Service which will be used to produce the summation of 2 numbers when consumers pass the required values. To test the application for any debugging issues we will run the application first by simply pressing F5 from the keyboard or we can use the Build Solution option to build the application completely. Once the project is executed we can see the WCF Service is opened in the IE browser (Or any default browser) with the details of the Service name and the we can also see the WCF service is hosted locally in the local host of the development machine as shown in the screen below. At the background we have the Windows Azure Compute Emulators triggered and the application will be hosted locally in the emulators so that we can have the same environment which we are looking for when deploying to the Azure Cloud Platform.
Now our service is ready to be consumed locally, we need to see how to host the service as a web role to the Windows Azure Platform. In order to host the application to the Azure Management portal first we need to package the application and then follow some steps in the Windows Azure Management Portal to upload the package. Let us start with first packing the application using the Visual Studio IDE itself.
To package the application first point the Windows Azure project from the solution explorer (WCFWebRoleApplication) and right click on the project name to select the Package option from the list. Once we select the package option we can see a small configuration wizard requesting for the service configuration files to select between the local and the release version. Basically we will go with the release version to deploy to the Azure environment. So select the respective configuration settings as shown in the screen below and click on ok button to proceed further.
Now once the package is completed we can see a folder opens by default with 2 files created, one file is the configuration file and the other is the package which is used for uploading the release package to the Azure Environment with ease. In our example we will be getting the below 2 configuration files once we package the application with build success as below
- File 1 – WCFWebRoleApplication (Service Package File)
- File 2 - ServiceConfiguration.Cloud (Service Configuration File)
Now login to Windows Azure Management Portal with you valid credentials and go to manage which is the dashboard used to manage the applications and database on to the Azure Platform. On successful login we can see the management portal with the different options to manage the application online. Now let us first create a hosted service and host the application to the service by uploading the package which we have created. To create a hosted service first click on the New Hosted Service option at the top left Ribbon menu and we can see a popup requesting for the details that are to be filled to create the hosted service. We need to fill the details of the project which we developed and provide the exact details as shown in the screen below.
As indicated in the screen above we need to configure the options which are basically required to host the WCF Service as a Web Role on to the Azure Environment.
- Subscription on which we are going to host the application, basically its required for billing of the instance.
- Name of the service that should be unique with the list of already existing services.
- URL prefix which should be unique globally since it will be added as a prefix to the cloud URI.
- Data centre region on which we need to deploy our application for network traffic.
- Deployment options if we want to deploy to stage or to production environment.
- Option to start the service after successful deployment to the Azure platform.
- Name of the deployment which is used for tracking internally.
- Browse and select the package location which we got while packaging the application from the Visual Studio IDE.
- Browse and select the Service Configuration file location which we got while packaging the application from the Visual Studio IDE.
Once we are done with the above configurations we can click on OK to start deploying the application to the Azure environment. Normally it takes a bit time to deploy the package, once the deployment is completed we can see the service in the list as shown in the screen below.
We can browse the DNS Name and we can see the service in IE or any browser which can be consumed and used by any of the consumers to get the expected result in their application with ease.
So in this first part of the article we have seen what WCF is all about and how to use the WCF Service as a Web Role in deploying the application to the Windows Azure Environment
Sorry, no bio is available
This author has published 6 articles on DotNetSlackers. View other articles or the complete profile here.
Please login to rate or to leave a comment.