Basic WCF Implementation


This article will contain how to create publish and consume WCF service.

How to create service contract. 

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

namespace Microsoft.ServiceModel.Samples
{
    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]   
    public interface Icalculator
    {
        // Step7: Create the method declaration for the contract.
        [OperationContract]
        double Add(double n1, double n2);
        [OperationContract]
        double Subtract(double n1, double n2);
        [OperationContract]
        double Multiply(double n1, double n2);
        [OperationContract]
        double Divide(double n1, double n2);
    }
}

Creation of service.

public class CalculatorService : Icalculator
{
    decimal CallCnt = 0;
    // Step 2: Implement functionality for the service operations.
    public double Add(double n1, double n2)
    {
        CallCnt += 1;
        double result = n1 + n2;
        Console.WriteLine("Received Add({0},{1})", n1, n2);
        // Code added to write output to the console window.
        Console.WriteLine("Return: {0}", result);
        Console.WriteLine("Call Count: {0}", CallCnt);
        return result;
    } 

    public double Subtract(double n1, double n2)
    {
        CallCnt += 1;
        double result = n1 - n2;
        Console.WriteLine("Received Subtract({0},{1})", n1, n2);
        Console.WriteLine("Return: {0}", result);
        Console.WriteLine("Call Count: {0}", CallCnt);
        return result;
    }

    public double Multiply(double n1, double n2)
    {
        CallCnt += 1;
        double result = n1 * n2;
        Console.WriteLine("Received Multiply({0},{1})", n1, n2);
        Console.WriteLine("Return: {0}", result);
        Console.WriteLine("Call Count: {0}", CallCnt);
        return result;
    } 

    public double Divide(double n1, double n2)
    {
        CallCnt += 1;
        double result = n1 / n2;
        Console.WriteLine("Received Divide({0},{1})", n1, n2);
        Console.WriteLine("Return: {0}", result);
        Console.WriteLine("Call Count: {0}", CallCnt);
        return result;
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Step 1 of the address configuration procedure: Create a URI to serve as the base address.
        Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service");
        // Step 2 of the hosting procedure: Create ServiceHost
        ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);
        try
        {
            // Step 3 of the hosting procedure: Add a service endpoint.
            selfHost.AddServiceEndpoint(
                typeof(Icalculator),
                new WSHttpBinding(),
                "CalculatorService");
            // Step 4 of the hosting procedure: Enable metadata exchange.
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            selfHost.Description.Behaviors.Add(smb);
            // Step 5 of the hosting procedure: Start (and then stop) the service.
            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();
        }
        catch (CommunicationException ce)
        {
            Console.WriteLine("An exception occurred: {0}", ce.Message);
            selfHost.Abort();
        }
    }
}

Calling service from client 

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

namespace
ServiceModelSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            //Step 1: Create an endpoint address and an instance of the WCF Client.
            IcalculatorClient client = new IcalculatorClient();
            // Step 2: Call the service operations.
            // Call the Add service operation.
            double value1 = 100.00D;
            double value2 = 15.99D;
            double result = client.Add(value1, value2);
            Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
            // Call the Subtract service operation.
            value1 = 145.00D;
            value2 = 76.54D;
            result = client.Subtract(value1, value2);
            Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
            // Call the Multiply service operation.
            value1 = 9.00D;
            value2 = 81.25D;
            result = client.Multiply(value1, value2);
            Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
            // Call the Divide service operation.
            value1 = 22.00D;
            value2 = 7.00D;
            result = client.Divide(value1, value2);
            Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
            //Step 3: Closing the client gracefully closes the connection and cleans up resources.
            client.Close();
            IcalculatorClient client1 = new IcalculatorClient();
            result = client1.Add(value1, value2);
            Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
            result = client1.Divide(value1, value2);
            Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
            client1.Close();
            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();
        }
    }
}

Up Next
    Ebook Download
    View all
    Learn
    View all