Interface in WCF Services

Introduction: What is interface in Dot Net ? How it is used in WCF Services?

An interface looks like a class, but it has no implementation. It contains:-

  1. Definitions of events, indexers, methods and properties.

  2. An interface inherited by classes.

  3. An interface defined with keyword "interface".

  4. In WCF, we use interface inside the [ServiceContract] attribute.

  5. Interfaces don't have their own objects.

What are the benefits of using interface in our WCF application?

  • Interfaces can extend/inherit other interfaces .

  • A single class can implement multiple interfaces .

  • You can modify the implementation of a service without breaking the contract .
     

Code:

[ServiceContract]
Public Interface IHello
{
    [OperationContract]
   
string HelloWorld();

    [OperationContract]
   
string HelloComputer();
}
public class Hello : IHello
{
   
string IHello.HelloWorld()
    {
       
return "Hello world";
    }
   
string IHello.HelloComputer()
    {
       
return "Hello computer";
    }
}