Translate

Saturday, August 6, 2011

Introduction to WCF (Windows Communication Foundation Tutorial)







What is WCF?



In simple terms WCF can be thought of as the next generation of webservices. In true sense however, WCF is actually a replacement for both webservices and remoting.


The image below shows you a real life scenario where a WCF service is used to authorize a user access to a website within the LAN of a corporation. This WCF service has a method that takes the logged in user's LAN ID as an input parameter and returns a boolean value which is true if the user's designation is Manager or above. And it returns false if the user's designation is below that of a manager.















In this article I will show you a quick and easy way to build a WCF service and a WCF client that accesses that service.

Building a WCF Service

 Building a WCF service mainly involves 3 steps

  • Defining the service contract : This defines the signatures of all the methods provided by the service
  • Implementing the service contract: This code implements the methods defined in the service contract.
  • Hosting the service


A WCF service can be hosted in four different ways
1>As a console application
2>As a website in IIS
3>As a Windows service
4>As a Windows Activation Service

In the example below I have built the WCF service a console application. In this example,  defining and implementing the service is super simple. Hosting the service is the only part that will take slightly longer to understand.

To build a WCF service as a console application, this is what you would do

1>Create a console application project and call it SimpleService















2> Add a reference to System.ServiceModel
























3>Once you do that it will appear in your references folder















4> Add a new Class file to the project where you would define your service contract. What this means is that this code defines the signatures of the methods that this service will expose. Name the class file as DefineServiceContract.cs

























5>Paste the code below in that page.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace DotnetAnalysis
{
    [ServiceContract]
    public interface IDilbertFunctions
    {
        /* Here you define the service contract.
           In other words, this is where you specify
           what functions (web methods) your service will provide
        */

        [OperationContract]
        string HelloWorld();

        [OperationContract]
        double Add(double x, double y);
    }
}


6>Add another class file called ImplementServiceContract.cs .In this page you have to implement the methods defined in your service contract. Paste the code below do do that.

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

namespace DotnetAnalysis
{
    class DilbertFunctions : IDilbertFunctions
    {

        //This is the class where you implement the web methods defined in the service contract.

        public string HelloWorld()
        {

            return "Hello World";
        }

        public double Add(double a, double b)
        {
            double sum = (a + b);
            return sum;
        }
    }
}



7>Host the service by pasting the code below to the program.cs file


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace DotnetAnalysis
{
    class Program
    {
        static void Main(string[] args)
        {

            //This is where you host the service


            Uri baseAddress = new Uri("http://localhost:8000/DilbertService");//"DilbertService can be replaced with any name you want.

            ServiceHost selfHost = new ServiceHost(typeof(DilbertFunctions), baseAddress);//DilbertFunctions is the name of the class that implements the web methods

            selfHost.AddServiceEndpoint(typeof(IDilbertFunctions), new WSHttpBinding(), "DilbertFunctions");//IDilbertFunctions is the name of the interface that defines the service contract


            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            selfHost.Description.Behaviors.Add(smb);

            selfHost.Open();
            Console.WriteLine("The service is ready.");
            Console.WriteLine("Press <ENTER> to terminate service.");
            Console.WriteLine();
            Console.ReadLine();

            // Close the ServiceHostBase to shutdown the service.
            selfHost.Close();


        
        }
    }
}

8> At this stage this is how your application looks like (click on the image to expand it)

9>Run the service by pressing ctrl+f5 . After which you will see a screen as shown below





















10> Now if you paste this url http://localhost:8000/DilbertService  into internet explorer, you should be able to see this






































Further Reading:

How to test this service using WCF test client
http://dotnetanalysis.blogspot.com/2011/08/how-to-use-wcf-test-client.html

How to consume this service by creating a WCF client
http://dotnetanalysis.blogspot.com/2011/08/how-to-consume-wcf-service-wcf-client.html



Windows Communication Foundation .net 4.0:


No comments:

Post a Comment

Comments will appear once they have been approved by the moderator