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:
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);
DataSet ds = newDataSet();
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:
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.
Project Solution Explorer should have the following structure:
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:
[ServiceContract]
publicinterfaceIDSCloudService
{
[OperationContract]
stringGetData(int value);
[OperationContract]
CompositeTypeGetDataUsingDataContract(CompositeType composite);
[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.
publicDataSetGetUser(stringsCommand, stringsLogonID)
{
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;");
conn.Open();
stringtableName = "user";
SqlDataAdapter adapter;
SqlCommandselectCommand = newSqlCommand(sCommand, conn);
selectCommand.Parameters.AddWithValue("@id", sLogonID);
adapter = newSqlDataAdapter(selectCommand);
newSqlCommandBuilder(adapter);
DataSet ds = newDataSet();
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.
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.
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:
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:
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.
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:
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!