wcf
  1. wcf-multiple-contracts-service

Multiple Contracts Service - (WCF)

In Windows Communication Foundation (WCF), you can define multiple service contracts in a single service implementation. This allows a single service to provide multiple functionality to clients, each through a different endpoint. This is called a multiple contracts service.

Syntax

To define a multiple contracts service in WCF, you need to define each contract separately and implement its methods in the same service class. Here's an example of how to define and implement two service contracts:

[ServiceContract]
public interface ICalculator
{
   [OperationContract]
   int Add(int x, int y);
}

[ServiceContract]
public interface IConverter
{
   [OperationContract]
   string Reverse(string s);
}

public class MyService : ICalculator, IConverter
{
   public int Add(int x, int y)
   {
      return x + y;
   }

   public string Reverse(string s)
   {
      char[] arr = s.ToCharArray();
      Array.Reverse(arr);
      return new string(arr);
   }
}

In this example, we define two service contracts: ICalculator which has an Add method that returns the sum of two integers, and IConverter which has a Reverse method that reverses a string. We then implement both of these contracts in the same MyService class.

Example

Here's an example of how to host a multiple contracts service in WCF:

using System;
using System.ServiceModel;

namespace MyNamespace
{
  class Program
  {
    static void Main(string[] args)
    {
      // Create the service host
      using (var host = new ServiceHost(typeof(MyService)))
      {
        // Add the ICalculator endpoint
        var calculatorBinding = new BasicHttpBinding();
        var calculatorEndpoint = new EndpointAddress("http://localhost:8000/calculator");
        host.AddServiceEndpoint(typeof(ICalculator), calculatorBinding, calculatorEndpoint);

        // Add the IConverter endpoint
        var converterBinding = new BasicHttpBinding();
        var converterEndpoint = new EndpointAddress("http://localhost:8000/converter");
        host.AddServiceEndpoint(typeof(IConverter), converterBinding, converterEndpoint);

        // Open the host and wait for requests
        host.Open();
        Console.WriteLine("Service ready.");
        Console.ReadLine();
      }
    }
  }
}

In this example, we create a new ServiceHost object and specify the type of MyService as the service implementation. We then add two endpoints, one for each service contract, by specifying the binding and endpoint addresses. Finally, we open the host and wait for incoming requests.

Output

When you run the code to host your multiple contracts service, you should see output that looks like this:

Service ready.

Explanation

In this example, we use the ServiceHost class to host our MyService implementation. We add two endpoints to the host, one for each service contract, using the AddServiceEndpoint method. We specify the binding and endpoint address for each endpoint. Finally, we open the service host using the Open method and wait for incoming requests.

Use

A multiple contracts service in WCF is a useful way to provide different functionality to clients through different endpoints. This can help organize your service and make it more maintainable and extensible.

Important Points

  • A multiple contracts service in WCF allows a single service to provide multiple functionality to clients through different endpoints.
  • You need to define each service contract separately and implement its methods in the same service class.
  • You can add each endpoint to the service host using the AddServiceEndpoint method.

Summary

In this page, we discussed how to create a multiple contracts service in WCF. We covered the syntax, example, output, explanation, use, important points, and summary of a multiple contracts service. Creating a multiple contracts service in WCF is a useful way to provide different functionality to clients through different endpoints, helping your service be more organized and maintainable.

Published on: