What is WCF application in .NET ? How it works ? Code Example

A.NET framework called WCF (Windows Communication Foundation) is used to create service-oriented programs. It enables programmers to design distributed applications utilizing a range of transport and communication protocols.

This is an illustration of how a fundamental.NET WCF application functions:

Define the Service Contract: The service contract, which outlines the service’s functions, must first be defined. An interface that outlines the service’s methods and parameters can do this.

[ServiceContract]
public interface ICalculator
{
    [OperationContract]
    int Add(int a, int b);

    [OperationContract]
    int Subtract(int a, int b);

    [OperationContract]
    int Multiply(int a, int b);

    [OperationContract]
    int Divide(int a, int b);
}

Implement the Service Contract: The service implementation—the next step—involves putting the service contract into action in a class. The actual code for the measures outlined in the service contract will be included in this class.

public class Calculator : ICalculator
{
    public int Add(int a, int b)
    {
        return a + b;
    }

    public int Subtract(int a, int b)
    {
        return a - b;
    }

    public int Multiply(int a, int b)
    {
        return a * b;
    }

    public int Divide(int a, int b)
    {
        if (b == 0)
            throw new DivideByZeroException();

        return a / b;
    }
}

Define the Binding: The communication protocol and transport method are specified by the binding. Here is an illustration of how to create a fundamental binding that makes use of HTTP for communication:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_ICalculator" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:8000/Calculator"
                  binding="basicHttpBinding"
                  bindingConfiguration="BasicHttpBinding_ICalculator"
                  contract="ICalculator"
                  name="BasicHttpBinding_ICalculator" />
    </client>
</system.serviceModel>

Host the Service: You can host the service in several ways once the service contract, implementation, and binding have been established. An illustration of hosting the service in a console application is shown below:

using System;
using System.ServiceModel;

class Program
{
    static void Main()
    {
        using (var host = new ServiceHost(typeof(Calculator)))
        {
            host.Open();

            Console.WriteLine("Service is running. Press any key to stop.");
            Console.ReadKey();

            host.Close();
        }
    }
}

Consume the Service: To use the service, clients must create a proxy object. Here is an illustration of how to use the service in a console program:

using System;
using System.ServiceModel;

class Program
{
    static void Main()
    {
        var factory = new ChannelFactory<ICalculator>(
            new BasicHttpBinding(),
            new EndpointAddress("http://localhost:8000/Calculator"));

        var channel = factory.CreateChannel();

        Console.WriteLine("5 + 3 = {0}", channel.Add(5, 3));
        Console.WriteLine("5 - 3 = {0}", channel.Subtract(5, 3));
        Console.WriteLine("5 * 3 = {0}", channel.Multiply(5, 3));
        Console.WriteLine("5 / 3 = {0}", channel.Divide(5, 3));

        ((IClientChannel)channel).Close();
        factory

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories