Creating a WCF service from scratch without using the Visual Studio Template

We can create a WCF service without using the template provided with visual studio. It is easy and very effective to learn and understand the concept of WCF.

I will be using the Visual Web Developer 2012 Express edition for this exercise. you can also use the Visual Web Developer 2010 Express.

We will implement a Simple Math service that will expose methods to perform mathematical operations like addition, subtraction etc.

To start with first add a empty ASP.NET Web Application project as shown below.


Now, add a new c sharp class file as below. and name it 

IWCFMathService.cs

The "I"  will be explained in a while



Next we change this class into an interface, This becomes our service contract. 

But before we move into that we may have to add a reference to the System.ServiceModel library as shown below.





Add the below code in this new file. and change the class to interface.

Please note that you have to add the [ServiceContract] attribute above the interface. This is to make the interface as a service contract and this will be exposed to the client which is using your service.

Also every method you want to expose in your service , add the attribute [OperationContract] 

------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;

namespace WCFMathService
{
    [ServiceContract]
    public interface IWCFMathService
    {
        [OperationContract]
        double Add(double op1, double op2);

        [OperationContract]
        double Subtract(double op1, double op2);

        [OperationContract]
        double Multiply(double op1, double op2);

        [OperationContract]
        double Divide(double op1, double op2);

    }

}

-----------------------------------------------------------



Next we add another file into the project , WCFMathService.cs , this will implement the above interface and this is the place which holds the actual business logic for your service.

----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WCFMathService
{
    public class WCFMathService : IWCFMathService
    {
        public double Add(double op1, double op2)
        {
            return op1 + op2;
        }

        public double Subtract(double op1, double op2)
        {
            return op1 - op2;
        }


        public double Multiply(double op1, double op2)
        {
            return op1 * op2;
        }


        public double Divide(double op1, double op2)
        {
            if (op2 > 0)
            {
                return op1 / op2;
            }
            else
            {
                return 0;
            }
        }
    }

}
-----------------------------------------------------------------

Now that we have implemented our interface, we will now create our .svc file.

For this first add a new text file as shown below and change the name and its extension to .svc for ex. WCFMathService.svc



After adding this edit this file and add the below line 

<%@ServiceHost Service="WCFMathService.WCFMathService"%>

Next we will edit the web.config file as shown below.

<system.serviceModel>
    
    <services>
      <service
      name="WCFMathService.WCFMathService"
      behaviorConfiguration="MyServiceTypeBehaviors">
        <endpoint
        address=""
        binding="wsHttpBinding"
        contract="WCFMathService.IWCFMathService"/>
        <endpoint
        contract="IMetadataExchange"
        binding="mexHttpBinding"
        address="mex" />
      </service>
    </services>
    
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceTypeBehaviors" >
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
    
  </system.serviceModel>

it will look something like this.


Now we are Done!

The WCF service is ready. 

You can now build the project and view the WCFMathService.svc file in the browser as shown below


The browser window will look something like this.

If you get this screen , your service is up and running.


For now our project structure will look something like this




Now to test our service we will create a small test application.
This will be a console App that will add a reference to this service and make suitable calls to the available operations.

To proceed with this. Create a console application and Add the service reference to our service. you can get the URL from the browser when we browse the svc file.


Right click on the project and add a service reference as shown below




The below screen will open , paste the URL in the available text field and click on Go , the service will be fetched and when you click on it it will expose the available operations. as shown below. 

Now provide a namespace for this service and click on OK button.



Your project screen will look something like this.


Now type in the below code in your main method.

MyMathService.WCFMathServiceClient client = new MyMathService.WCFMathServiceClient();

//Calling the service method Add here and passing 1 , 2 --> Add(1,2)
double AddResult = client.Add(1, 2);
Console.WriteLine("1 + 2 = {0}",AddResult);

//Calling the service method Subtract here and passing 4 , 2 --> Subtract(4,2)
double SubResult = client.Subtract(4, 2);
Console.WriteLine("4 - 2 = {0}", SubResult);

//Calling the service method Multiply here and passing 2 , 2 --> Multiply(2,2)
double MulResult = client.Multiply(2, 2);
Console.WriteLine("2 * 2 = {0}", MulResult);

//Calling the service method Divide here and passing 10 , 5 --> Divide(10,5)
double DivResult = client.Divide(10, 5);
Console.WriteLine("10 / 5 = {0}", DivResult);

Console.ReadKey();


it may look something like this.


On running the code you will get the responses from the service.


This output shows that our service is up and running!


3 comments:

  1. FINALLY! An article straight to the point. Took me forever googling how to add a simple endpoint to a service. You sir have done a fine job here... The from scratch design approach clears up a lot of confusions I had about wcf. thankyoy

    ReplyDelete
  2. Yes this helped me too. Thanks breh.

    ReplyDelete