Home > DeveloperSection > Articles > Creating WCF Service Web Role in Windows Azure Application

Creating WCF Service Web Role in Windows Azure Application


Cloud Computing Cloud Computing 
Ratings:
0 Comment(s)
 5397  View(s)
Rate this:

Creating WCF Service Web Role in Windows Azure Application

In this article I am going to explain how to create WCF Service Web role in windows azure application and how to use the WCF Service in an outside application.

WCF Service Role enables us to create WCF service and host in Windows Azure. In this article, we will create a WCF Service Role and host on local development fabric and consume in a console application.

Follow the steps given below to create or add WCF Service web role in windows azure:

·         Open Microsoft Visual Studio 2010 as an administrator.

·         To create a new project FileNewProject.

·         Select Cloud template from the Installed Templates.

·         Select Windows Azure Project and enter the name of the project as WCFWebRole.

·         Click OK to proceed.

Creating WCF Service Web Role in Windows Azure Application

·         To add a WCF service web role to the solution, choose WCF Service Web Role and then choose the right arrow. The roles are displayed in the Windows Azure solution pane of the dialog box.

·         Click OK to proceed.

Creating WCF Service Web Role in Windows Azure Application

 

After adding WCF service, modify IService1.cs as given below:

    [ServiceContract]

    public interface IService1

    {

        [OperationContract]

        string GetSquare(double num);

    }

 

Also modify Service1.svc.cs as shown below:

    public class Service1 : IService1

    {

        public string GetSquare(double num)

        {

            return string.Format("The square of a number is: {0}", Math.Sqrt(num));

        }
    }

Modify web.config file of WCF as shown below:

<system.serviceModel>

    <behaviors>

      <serviceBehaviors>

        <behavior>

          <serviceMetadata httpGetEnabled="true"/>

          <serviceDebug includeExceptionDetailInFaults="false"/>

        </behavior>

      </serviceBehaviors>

    </behaviors>

    <bindings>

      <basicHttpBinding>

        <binding>

          <security mode="TransportCredentialOnly">

            <transport clientCredentialType="Windows" />

          </security>

        </binding>

      </basicHttpBinding>

    </bindings>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

</system.serviceModel>

 

After modifying the above files, press F5 to debug and run your application:

url of service (http://127.0.0.14:81/Service1.svc). The output should something like below:

Creating WCF Service Web Role in Windows Azure Application

Now create a Console application in order to test the WCF service. To create console application Open Visual StudioFileNewProjectWindowsConsole Application.

After the application is open, add service reference in an application as shown in the below figure:

Creating WCF Service Web Role in Windows Azure Application

Enter the WCF Service path in the Address textbox and click on the Go button to display the services. When the service is successfully displayed click on OK button to add service in the application.

Creating WCF Service Web Role in Windows Azure Application

When the service is successfully added in the application, add the service reference namespace:

using ConsoleApplication1.ServiceReference1;

 

To call GetSquare() method of the WCF Service (Service1) write the below code:

 

 

static void Main(string[] args)

        {

            Service1Client proxy = new Service1Client();

            var result = proxy.GetSquare(10);

            Console.WriteLine(result);

            Console.ReadKey(true);

        }

 

Now test the application by running (Ctrl + F5) your application:

Creating WCF Service Web Role in Windows Azure Application

After reading this article you can easily create WCF Service web role in windows azure application.


Don't want to miss updates? Please click the below button!

Follow MindStick