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.


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

 public interface IParentService
 string ParentMessage(string message);


Another ServiceContract named IChildService can inherit IParentService as following,

 public interface IChildService : IParentService
 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


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.


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


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

Published by

Dhananjay Kumar

Dhananjay Kumar is Developer Evangelist for Infragistics. He is a 8 times Microsoft MVP and well respected Developer Advocate in India.He is the author of 900+ Blog Posts, and can often be found speaking around India at conferences and hosting free workshops for programmers across the country. So far, he has hosted 60 free workshops on various topics like JavaScript, Angular, WCF, ASP.NET MVC, C#, Azure etc. Follow him on twitter @debug_mode for all the updates about his blog posts and workshops. You can send him email at debugmode [at] outlook [dot] com

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

  1. 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

  2. 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 !!

  3. Yes, It is very use full and very easy to understood also. Thanks for the article.

    HI Ruchir : You are trying to implement the inheritance in a service where we expose our operation contract. In this article implement Operation contract. Please rectify the hierarchy and execute the solution.

    Hope, Upper explanation will help you to understand.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s