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.

Method Overloading in WCF: WCF Interview Series #1

How to do Method Overloading in WCF?

Method or Operation overloading is one of the most important feature of OOPS. In programming languages like C, C++ and C#, this feature is heavily used by developers. While writing Service definition you may come across when you need to overload operations.

Let us go ahead and write Service with overloaded function as following. In below ServiceContract I am creating two methods and overloading them with different parameters.


[ServiceContract]
 public interface IService1
 {

[OperationContract]
 int Add(int number1, int number2);
 [OperationContract]
 string Add(string text1, string text2);
 }

I implemented service as below,


public class Service1 : IService1
 {

public int Add(int number1, int number2)
 {
 return number1 + number2;
 }

public string Add(string text1, string text2)
 {
 return text1 + text2;
 }
 }

I left default EndPoint configuration and hosting. When I ran service I encountered following exception,

image

So clearly by default WCF does not allow you to overload methods in service. However there is one attribute in OperationContract which can allow you to overload method at service end. You need to set Name parameter of OperationContract to manually enable overloading at service side.

image

To achieve overloading at Service side I set Name parameter of both function as follows,


[ServiceContract]
 public interface IService1
 {

[OperationContract(Name="AddNumber")]
 int Add(int number1, int number2);
 [OperationContract(Name="AddString")]
 string Add(string text1, string text2);
 }

Even though we have achieved overloading at Service side at client side method exposed with different name as set in Name parameter. At client side you will get method exposed as following,

image

So on asking you can say that by setting Name parameter value of OperationContract we can achieve Operation or Method overloading in WCF. I hope you find this post useful. Thanks for reading.