How to implement a WCF Service Contract

As I described how to create a Service Contract in the previous post now I will move forward and implement the Service Contract. For this I need to define a class which implements the Interface created for the service Contract.

In the previous post I created a console application for defining a service contract and in Program.cs I defined the service contract. So I am using the same file to write a class code which implements the interface.

The Interface created for the service contract in previous post is as follows.

// Define a service contract.
  [ServiceContract()]
  public interface ICalculator
  {
    // Create the method declaration for the contract.
    [OperationContract]
    double Add(double d1, double d2);
    [OperationContract]
    double Subtract(double d1, double d2);
    [OperationContract]
    double Multiply(double d1, double d2);
    [OperationContract]
    double Divide(double d1, double d2);
  }

Now I will create a class with the name of Calculator which will implement the above mentioned interface.

// Create a class that implements the service contract.
public class Calculator : ICalculator
{
     // Implement functionality for the service operations(methods).
     public double Add(double d1, double d2)
        {
            return d1 + d2;
        }

        public double Subtract(double d1, double d2)
        {
            return d1 - d2;
        }

        public double Multiply(double n1, double n2)
        {
            return d1 * d2;
        }

        public double Divide(double n1, double n2)
        {
            return d1 / d2;
        }
}

					
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s