.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
SP
satyapriyanayak
Subhransu Sekhar Jena
abhays
DotnetUser

Home >> Articles >> WCF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Working with WCF Service Web Role

Posted By:Dhananjay Kumar       Posted Date: April 02, 2011    Points: 200    Category: WCF    URL: http://www.dhananjaykumar.net  

In this article, we will create a WCF Service Role and host on local development fabric and consume in a console application.
 

WCF Service Role enables us to create a WCF service and host it 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. In the second part of this article we will move the WCF Service to an Azure portal. 

To start with,

  1. Create a New Project
  2. Navigate to cloud tab
  3. Create Windows Azure Project
  4. Select WCF Service Role from given options. 

    WebRole1.gif


If you look in the Solution Explorer, you will find in the WCF Service Role project the exact same structure and files as when a normal WCF Service Application is created. It contains:

  1. IService1.cs (Service Contract )
  2. Service1.svc.cs ( Service definition file )
  3. Web.config ( configuration for EndPoints)


We can modify these files accordingly for our purpose in exactly the same way that we usually do in a WCF Service Application.

Let us modify Service Contract as below: 

IService1.svc

using System.ServiceModel;
 
namespace WCFServiceWebRole1
{   
    [ServiceContract]
    public interface IService1
    {
 
        [OperationContract]
        string GetData(int value);
      
    }  
}


And service definition would be:

Service1.svc.cs 

namespace WCFServiceWebRole1
{

    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
 
    }
}

Leave the default configuration in Web.Config.

Make sure you have set the Windows Azure project as the Startup project and run the application. In the browser you will get the error message shown below. 

WebRole2.gif

Ignore this message and append service.svc with URL, so the URL would be http://127.0.0.1:81/service1.svc. Service.svc is the name of the service definition. After appending you will get the usual WCF Service message in the browser. 

WebRole3.gif

To test this WCF Service role in a console client:

  1. Create a console application project
  2. Add Service Reference by providing URL http://127.0.0.1:81/service1.svc

    WebRole4.gif


Now we will make a normal service call:

Program.cs 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication14.ServiceReference1;

namespace ConsoleApplication14
{
    class Program
    {
        static void Main(string[] args)
        {
            Service1Client proxy = new Service1Client();
            var result = proxy.GetData(99);
            Console.WriteLine(result);
            Console.ReadKey(true);
        }
    }
}

Now when you run you may or may not get the exception shown below. 

WebRole5.gif

To solve the above exception, we have to edit App.Config file. We need to change: 

WebRole6.gif

127.0.0.1 to localhost. Because it might be that the console application is not able to resolve 127.0.0.1, we need to change it to localhost. 

WebRole7.gif

Now on running we will get the following output: 

WebRole8.gif

One behavior to be noticed here is that sometime you may get a time out exception after changing 127.0.0.1 to localhost. In my further articles, I will drill down this unwanted behavior. 

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend