In this article I am going to demonstrate the following
- Creation of a WCF service in the Web Role
- Deployment to the cloud
- Testing with a client
The attributes of a WCF service on the Web role are:
- IIS Hosted
- IIS features like pooling and caching availability
- Less Configurations Needed
The following are the steps involved.
Step 1: Create the WCF Service
Create a new Windows Azure project in Visual Studio 2010 and name it as
WCFInWebRole as shown below
In the appearing dialog add one Web Role project as shown below.
Add a new WCF Service into the project. Name it as MessengerService.
Now the Solution Explore will look like below with the newly added contracts and
service files.
Modify the DoWork() method in the interface IMessengerService as given below.
string
SendMessage(string name);
Modify the DoWork() method in the interface IMessengerService as given below.
public
string
SendMessage(string
name)
{
return
"Hello "
+ name;
}
Step 2: Test the application
Make the MessengerService.svc file as the start page as shown below.
Press F5 to execute the application and you can see the service opened in the
browser as shown below.
Now are have tested the application and it is ready to be deployed to the cloud.
Step 3: Deploy to the cloud
Right click on the Web Role project and click package menu item as shown below.
In the appearing dialog select the default option and click the Package button.
Now sign in to the Windows Azure portal and click on the New Hosted Service
button from the top ribbon.
In the appearing dialog box enter the details as shown below.
Choose the option "Deploy to production environment"
Locate the package and configuration files from your application bin folder.
(Eg: ..\WCFInWebRole\bin\Debug\app.publish)
Please note that the URL prefix should be a unique name. If the URL prefix
entered is already in use you need to change it.
After that click the Ok button of the dialog box. (You have to scroll down to
see the button)
If any warning box appears, click Yes button to continue. Wait for a few minutes
and your deployment will be ready in a few minutes as shown below.
Select the Deployment 1 row and from the properties window you can get the url
of the web role.
Click on the url and your application will get opened in the browser. Add the
suffix MessengerService.svc to get the messenger service.
Step 4: Create the Client
Now we are ready to create the client application and test the deployed online
cloud WCF service.
Create a new console application into the existing Azure project and name it as
TestClient.
Use the add service reference option of the console application and in the
appearing dialog enter the service url as shown below.
Click Ok button to continue.
Modify the main method in the Program.cs as following:
static
void
Main(string[]
args)
{
ServiceReference1.MessengerServiceClient
client =
new
ServiceReference1.MessengerServiceClient();
string
result = client.SendMessage("Ian");
Console.WriteLine(string.Format("Invoking
WCF Service Result: {0}",
result));
Console.ReadKey(false);
}
Now set the console application as the start project and execute the
application.
You can see the following results.
You can see the result Hello Ian from the WCF service.
This concludes our article on WCF service deployment as web role and the
testing.
Summary
In this article we have seen how to deploy a WCF service to cloud platform. The
WCF service inside the Web role will be hosted in IIS. The source code is
attached and the application name in the url has to be changed according to your
application name.