Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Hosted-services / Azure

Converting WCF Services to Azure Cloud Services Step by Step

4.85/5 (4 votes)
20 Jan 2017CPOL4 min read 15.3K  
In this post, we will learn how to perform converting your WCF or Web Services to Azure Cloud services step by step.

Introduction

In this post, we will learn how to perform converting your MCF or Web Services to Azure Cloud services step by step.

Background

About Cloud Services

Microsoft's Platform as a Service (PaaS) environment can be used to create scalable applications and services. It supports multi-tier architectures and automated deployments. Previously named "Hosted Services", the Cloud Services for Windows Azure comprise one aspect of the PaaS offerings from the Windows Azure Platform. The Cloud Services are containers of hosted applications. These applications can be Internet-facing public web applications (such as web sites and e-commerce solutions), or they can be private processing engines for other work, such as processing orders or analyzing data.

About the Post

Some days earlier, I had a requirement to convert web services to Azure Cloud Services. After completing it perfectly, I decided to share it with you all.

NOTE: In this article, I will show how to convert a Web Service method DataSetGetUser(stringsCommand, stringsLogonID) as a sample, but you can use any of your services.

Tutorial Walkthrough

Introduction & Prerequisites

In this tutorial, you should have your own Web Services project and I will use one method just as a sample to show you how you can convert all of your methods. Azure Cloud Services support all interfaces you are using in Web Services, all methods that work in Web Services will work in Azure Cloud.

I will use the following method that works fine in MCF environment (you can use any of your methods), it returns a dataset that contains user login information:

C#
publicDataSetGetUser(stringsCommand, stringsLogonID)
{
    SqlConnection conn = newSqlConnection(@"Persist Security Info=False;Data 
    Source=localhost;Initial Catalog=TESTDB;User
    ID=sa;Password=test;MultipleActiveResultSets=True;Pooling=True;Min Pool Size=5;Max
    Pool Size=100;");
	    conn.Open();
	    stringtableName = "user";
	    SqlDataAdapter adapter;
	    SqlCommandselectCommand = newSqlCommand(sCommand, conn);

        selectCommand.Parameters.AddWithValue("@id", sLogonID);

        adapter = newSqlDataAdapter(selectCommand);

        newSqlCommandBuilder(adapter);

        // Create a New DataSet
        DataSet ds = newDataSet();
        // Fill The DataSet With the Contents of the "selecteItem" Table
        adapter.Fill(ds, tableName);

        conn.Close();
        return (ds);
}

Steps 1 - Create Cloud Service Project

In Visual Studio, open an Existing project where you have all MCF methods declared. Create the new project using the Azure Cloud Service Template:

Image 1

Name the new project "TestCloudService" and save it somewhere on the hard drive.

Steps 2 - Create MCF Web Role

Next, the Visual Studio Wizard will ask you to create a Web Role for the Cloud Service Project. It is a good idea to select the WFC Web Role because we are converting the method from WCF project.

Image 2

Project Solution Explorer should have the following structure:

Image 3

Please note that under the new MCF Role, we have the following files that need to be updated:

  • IService1.sc file that contains all interface declarations
  • Servoice1.svc.cs file that contains all interface implementations

Steps 3 - Update the Web Role Files

In IService1.cs, create [OperationContract] (it already has GetData and GetDataUsingDataContractOperationContract) and paste the method definition like below:

C#
[ServiceContract]
publicinterfaceIDSCloudService
    {

        [OperationContract]
stringGetData(int value);

        [OperationContract]
CompositeTypeGetDataUsingDataContract(CompositeType composite);

// TODO: Add your service operations here
        [OperationContract]
DataSetGetUser(stringsCommand, stringsLogonID);

Open Service1.svc.cs and paste your method from Web Services like below. Later, we will update connection string from Cloud database.

C#
publicDataSetGetUser(stringsCommand, stringsLogonID)
        {
// Cloud connection string
SqlConnection conn = newSqlConnection("Server=tcp:v973exnqd4.database.windows.net,1433;
                     Database=testdb;User ID=draze@v973exnqd4;Password=Ph158158one;
                     Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;");

// Old Web service connection string
//SqlConnection conn = new SqlConnection(@"Persist Security Info=False;Data Source=localhost;
//Initial Catalog=DSDB;User ID=sa;Password=draze;MultipleActiveResultSets=True;Pooling=True;
//Min Pool Size=5;Max Pool Size=100;");
conn.Open();
stringtableName = "user";
SqlDataAdapter adapter;
SqlCommandselectCommand = newSqlCommand(sCommand, conn);

selectCommand.Parameters.AddWithValue("@id", sLogonID);

            adapter = newSqlDataAdapter(selectCommand);

newSqlCommandBuilder(adapter);

// Create a New DataSet
DataSet ds = newDataSet();
// Fill The DataSet With the Contents of the "selecteItem" Table
adapter.Fill(ds, tableName);

conn.Close();
return (ds);
        }

Steps 4 - Refactor the New Interfaces

In Solution Explorer, select and open IService1.cs, select IService1 interface, right click and select Refactor – Rename.

Image 4

Name it something that will represent the Web Role business, in my case, I will call it TestCloudService.

Repeat the same steps with Service1 implementation to refactor Service1.svc.

Image 5

Image 6

Now that you are done with Refactoring, it will rename all interface declarations automatically. You need to just rename two files in Solution Explorer:

  • IService1.cs to ITestCloud.cs
  • Service1.svc to TestCloud.svc

After renaming in Solution Explorer, you will have the following structure:

Image 7

Steps 5 - Publish the Cloud Services

Firstly, you should have an Azure Cloud account, if you don’t have an account, you can get a free trial from here:

Then login to your Azure Cloud account and create a new Cloud service:

Image 8

In Solution Explorer, select the Cloud Service project and select Publish.

  • Cloud Service – Select one you have just created
  • Environment - Production
  • Build Configuration – Release
  • Service Configuration – Cloud
  • You can leave Enable Remote Desktop… Unchecked

Select Next, deployment will start and will take a few minutes.

Image 9

Steps 6 - Add Service Reference in Your Application

In the next few steps, you will add Service References to your Project which you will use to call all your Cloud Service method added in the steps above.

In Visual Studio, select the project to which you like to add Service References and Select "Add Service Reference…" in context menu:

Image 10

In the Add Service Reference dialog box, paste the Cloud Service link from the Azure portal in the Address bar and press "Go":

After it finds the Service reference, enter a name in the Namespace field and press OK.

If you have SQL server database in your Web Services project, you can add the database using Azure Cloud Portal. Copy database connection string from your Azure Cloud Portal and paste in your Service Method or in configuration file.

Now you can call any of your Web Services from Azure Cloud. Good luck!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)