Monday, March 7, 2011

Learning WCF4: Default Endpoint

Learning WCF 4 : Default endpoint

Prior to WCF 4 , i.e. in WCF3 , there were three major steps when one talked about WCF Service.
  1. Creating WCF Service Contract (IService)
  2. Implementing the contract(Service1.svc)
  3. Making an endpoint for the service , which could be done using code or web.config file . Majority of times it was done using web.config file.
  4. Hosting the service
  5. Consuming that service

Most of the times developers felt that creating a basic wcf service even , they need to play with web.config file, which was a tedious task. Thus Microsoft came up with concept of DEFAULT ENDPOINT in WCF4.

So enjoy coding your first program with WCF4 , without creating endpoint.

Step 1:
Open Visual Studio IDE 2010
Fileà New Project à WCF Tab.
Choose template WCF Service Application, Name it BasicWcf
Step 2:
Delete default code generated from Contarct IService1 , so that it should look like following .


namespace BasicWcf
{
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

    }

}

Similary do for Service class : so that it looks like following
namespace BasicWcf
{
   public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

    }
}

Step3: Open web.config file and delete following line for the time being .

 
Step 4: Build the service

Step 5: Add new project to the application

 

Step 6: Add service refrence in this console app to the service created.

 
Step 7: Click on discover .
 
Step 8:  Add the following in the Program.cs
using ConsoleApplication1.ServiceReference1;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Service1Client proxy = new Service1Client();
            var res=proxy.GetData(2403);
            Console.WriteLine(res);
            Console.ReadKey(true);

        }
    }
}


Step 9: Build the console app.
Step 10 : Set it as start up project.

Step 11; Run the project (F5)

Output :

Summary : This article shows how WCF 4 works with default endpoint . Default endpoint in WCF uses basicHttpBinding.

To check this just add following code in the Program.cs file and run again.

            var bindingName = proxy.Endpoint.Binding.Name;
            Console.WriteLine(bindingName);
            Console.ReadKey(true);