Hosting WCF Service on Windows Azure

Posted by: Mahesh Sabnis , on 7/6/2012, in Category Microsoft Azure
Views: 105389
Abstract: In this article, we will see how to create a WCF service using Azure cloud template provided in VS 2010 and then host it in an Azure environment using Web Role.

The Windows Azure platform provides an environment for developing scalable and highly available applications. Today we will look at a scenario where a multi-location company has a set of Web Services that are consumed by client applications in their various networks. This architecture is a good candidate for Azure deployment wherein the WCF service is hosted in the cloud and it communicates to SQL Azure for data.

In this article, we will see how to create a WCF service using cloud template provided in VS2010 and then host it in Cloud environment using the Web Role.

 

The following diagram represents the architecture of the application

azure-wcf-architecture

The Web role handles external HTTP and HTTPS communications. This provides services like IIS 7.5 for hosting environment. This article also explains the capability of Visual Studio 2010 for publishing an application over cloud using the Publish wizard. Pre-requisite for publishing is a Azure subscription for the cloud deployment and service creation.

Creating WCF Service

Step 1: Open VS2010 and create a new Cloud project, name it as ‘WCF_Cloud_HostedService’.

azure-cloud-project

Step 2: In this project, add a new WCF Service Web Role project as shown below and name it as WCF_DataService

wcf-service-role-project

Step 3: Rename IService1.cs to IService.cs and Service1.svc to Service.svc. For this application, I am using Company Database on Sql Azure and the EmployeeInfo table as shown below:

table-definition

Now add a new ADO.NET Entity Data model in the WCF project, name it as CompanyEDMX. Complete the wizard by selecting the Sql Azure Server name, Company Database and the EmployeeInfo table. The Entity generated is as shown below:

entity-employee

Step 4: Add the following OperationContract in the ‘IService.cs’

using System.ServiceModel;
 
namespace WCF_DataService
{
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        EmployeeInfo[] GetEmployees();
        [OperationContract]
        void CreateEmployee(EmployeeInfo obj);
        [OperationContract]
        void UpdateEmployee(int Id, EmployeeInfo obj);
        [OperationContract]
        void DeleteEmployee(int Id);
    }
}

Change the implementation of the Service class as below:

using System.Linq;
 
namespace WCF_DataService
{
    public class Service : IService
    {
        CompanyEntities objContext;
        public Service()
        {
            objContext = new CompanyEntities();
        }
 
        public EmployeeInfo[] GetEmployees()
        {
            var Employees = objContext.EmployeeInfoes.ToArray();
            return Employees;
        }
 
        public void CreateEmployee(EmployeeInfo obj)
        {
            objContext.AddToEmployeeInfoes(obj);
            objContext.SaveChanges();
        }
 
        public void UpdateEmployee(int Id, EmployeeInfo obj)
        {
            var Employees = objContext.EmployeeInfoes.ToList();
            var Employee = (from Emp in Employees
                            where Emp.ID == Id
                            select Emp).First();
            Employee.Salary = obj.Salary;
            Employee.DeptName = obj.DeptName;
            Employee.Designation = obj.Designation;
            objContext.SaveChanges();
        }
 
        public void DeleteEmployee(int Id)
        {
            var Employees = objContext.EmployeeInfoes.ToList();
            var Employee = (from Emp in Employees
                            where Emp.ID == Id
                            select Emp).First();
            objContext.DeleteObject(Employee);
            objContext.SaveChanges();
        }
    }
}

Step 5: The ‘WCF_DataService.Azure’ web role project is set as the start project. To test this WCF service, press F5.

test-wcf-service

Note that the url is shown on the local machine with port 81.

Publishing the WCF Service on the Cloud

The publish feature provides a flexible and handy mechanism of deploying the application over the cloud. Before using this facility, you must have a Windows Azure Subscription, which you can get by visiting www.azure.com. Your credit card information will be required.

Step 1: Build the application, right-click on the ‘WCF_DataService.Azure’ and select ‘Publish’. The Azure Publish SignIn window will be displayed as seen here, where you need to select the subscription:

azure-publish-signin

Clicking next will take you to the ‘Windows Azure Publish Settings’ window, where you need to set the following:

  • Hosted Service: This is the repository for the application deployed on the cloud under the subscription.
  • Environment: The environment for the application to be deployed, there are two values : Production and Staging.
  • Build Configuration: This allows you to select whether the application is deployed under debug or release mode.
  • Service Configuration: In any cloud Web role project, there exist Service Configuration files for ‘Cloud’ and ‘Local’. These are used to define configurations for application to deploy and use other services like Storage Service. For publishing over the cloud, you must select ‘Cloud’.

Now a window will display as shown below:

axure-publish-settings

Since we need to created a Hosted service for the application to deploy, you will get the following window using which you can create the hosted service and select the Region for deployment, which allows to select Data Center for the application.

create-azure-services

Enter the name and Location:

azure-choose-location

Click on ‘Ok’. The deploy process will start as shown below:

I9_Publish_5

The Windows Azure Publish Settings windows is as follows:

publish-windows-azure-app

Now once you click on the Publish buttonm, the publish activity starts and the progress can be seen in VS 2010 as shown below:

azure-publish-progress

Now if you switch back to the Windows Azure portal, you will see the deployment progress:

deploy-progress

After the deployment is successfully completed, VS 2010 will display the summary as shown below:

azure-deploy-summary

Now if you click on the Website URL (marked with Dark Yellow rectangle) or copy paste in the browser, the Service will be shown here: (Note: By default you will get Access Denied error, so change the URL by appending Service.svc to the URL in the Address bar.)

azure-service

Now if you want to update a service and re-publish, you will be asked the following

update-service

The deployment will be replaced.

Now you can created a Client project like ASP.NET, ASP.NET MVC or WinForm and add the Service which can be tested just like an on-premise WCF service.

Conclusion: By using VS 2010 with Windows Azure tools installed, a developer can easily take a WCF application over to the cloud.

The entire source code of this article can be downloaded over here

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Author
Mahesh Sabnis is a DotNetCurry author and a Microsoft MVP having over two decades of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions), and Front-end technologies like Angular and React. Follow him on twitter @maheshdotnet or connect with him on LinkedIn


Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by LHasson on Monday, August 13, 2012 11:05 PM
Thanks very much.  This tutorial was very helpful to me.  Excellent.
Comment posted by Prapthi on Wednesday, September 12, 2012 4:23 AM
Very good article, I found it very usefull. This is exactly what a beginner needs. Thank you so much for this.
Comment posted by anil babu on Friday, October 12, 2012 1:00 AM
Can you tell me How to consume this service in my asp.net
plz give me
Comment posted by amin on Friday, October 26, 2012 5:04 PM
CompanyEntities objContext;    is not working for me   i cant map the table  
help please
Comment posted by amin on Friday, October 26, 2012 5:11 PM
never mind now it works  thank youu great tutorial !
Comment posted by Darach on Saturday, December 22, 2012 4:57 PM
The following are what I believe to be errors due to me working with version 5.0 of the Entity Framework.

Noted what I believe is an error.
// method in error:
public void CreateEmployee(EmployeeInfo obj)
        {
            objContext.AddToEmployeeInfoes(obj);
            objContext.SaveChanges();
        }

// What I believe is correct:
public void CreateEmployee(EmployeeInfo obj)
        {
            objContext.EmployeeInfoes.Add(obj);
            objContext.SaveChanges();
        }

// method in error:
public void DeleteEmployee(int Id)
        {
            var Employees = objContext.EmployeeInfoes.ToList();
            var Employee = (from Emp in Employees
                            where Emp.ID == Id
                            select Emp).First();
            objContext.DeleteObject(Employee);
            objContext.SaveChanges();
        }
// What I believe is correct:
        public void DeleteEmployee(int Id)
        {
            var Employees = objContext.EmployeeInfoes.ToList();
            var Employee = (from Emp in Employees where Emp.ID == Id select Emp).First();
            objContext.EmployeeInfoes.Remove(Employee);
            objContext.SaveChanges();
        }

Comment posted by Wit124 on Sunday, April 27, 2014 10:09 PM
Hi, I tried to implement above example in VS2012. After, I copy/pasted code in IService.cs and Service.svc.cs, the following methods are not recognized: objCOntext.AddToEmployeeInfoes and objContext.DeleteObject.
Am I doing something wrong?
Appreciate your help.

Comment posted by Wit124 on Sunday, April 27, 2014 10:13 PM
I've just noticed that your ADO.NET Entity Data model doesn't have ".tt" files. Changing Entity Code Generation Strategy to "Default" cause that more error is generated.
Thanks
Comment posted by Wit124 on Monday, April 28, 2014 7:49 AM
I've just noticed that your ADO.NET Entity Data model doesn't have ".tt" files. Changing Entity Code Generation Strategy to "Default" cause that more error is generated.
Thanks