Can we implement Inheritance in WCF ServiceContract : WCF Interview Series #2

Can we implement Inheritance in WCF ServiceContract?

YES we can have Contract Inheritance in WCF. In other words in WCF one ServiceContract can inherit other ServiceContract.

image

Let us take an example that you have a ServiceContract named IParentService as given below,


[ServiceContract]
 public interface IParentService
 {
 [OperationContract]
 string ParentMessage(string message);

}

Another ServiceContract named IChildService can inherit IParentService as following,


[ServiceContract]
 public interface IChildService : IParentService
 {
 [OperationContract]
 string ChildMessage(string message);
 }

Next you need to decide on implantation of Service. Single Service class can implement both contract by implementing bottom most ServiceContract in hierarchy. In this case bottom most ServiceContract is IChildService

image

Service can be implemented as following in a single service class.


public class Service1 : IChildService
 {
 public string ChildMessage(string message)
 {
 return "Hello " + message + "from Child Service";
 }

public string ParentMessage(string message)
 {
 return "Hello " + message + "from Parent Service";
 }
 }

Now you have choice that either you can expose whole hierarchy as single EndPoint or different EndPoints for different Service Contract. To expose contract hierarchy create EndPoint with bottom most ServiceContract. In this case we are creating EndPoint with Service Contract IChildService . At the client side operations from whole hierarchy could be invoked.

image

So at client side Service will be exposed as single class as given below,

image

In this way you can work with Inheritance in Service Contract. I hope you find this post useful. Thanks for reading.

About these ads

3 thoughts on “Can we implement Inheritance in WCF ServiceContract : WCF Interview Series #2

  1. Pingback: Dew Drop – May 17, 2013 (#1,549) | Alvin Ashcraft's Morning Dew

  2. Nice article I want to know how can we hide/show the methods in a wcf service based on the user logged on i.e if admin logs in to the site he must be able to access all the methods and if normal user logs on only one or two methods

  3. sir my service is giving following error :

    The contract name ‘WCF_Inheritace.IService2′ could not be found in the list of contracts implemented by the service ‘Service1′.

    in this Iservice2 is drived interface and IService1 is base interface and in endpoint i have mentioned IService2 as my contact.:

    pls help me out sir !!

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