Click here to Skip to main content
15,348,004 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I cam up with a small doubt while working on WCF. I am looking for this answer for few days over internet but couldn't find any well-explained article on this topic.

Question 1: Why we use interface in WCF?
We could have written the operation contracts, data contracts, etc., without using interface. We could have defined a class which would simply implement the operation contracts.

Question 2: Why not Abstract class instead of interface?
We could also implement the same using an Abstract Class. I could have declared and defined the contract methods in the Abstract Class and could have simply inherit the abstract class.

Agnib Pyne

What I have tried:

I have searched over internet to get a perfect explanation for this doubt.
Updated 31-Oct-20 8:27am

By definition you have not to use no interfaces and no abstract classes to use WCF...
Interfaces are considered to be the best practice because it enables a wise combination of reuses of the same contract (a class can implement multiple interfaces but can inherit only one class)...
Member 11072126 16-Oct-17 6:38am
Thanks. I completely agree with you that I can reuse the same interface for many classes.
Now, I have one more doubt that is:
When to use Abstract Class and when to use Interface?

As far as I am concerned, Abstract Class is used for IS-A relationships and interface for HAS-A relationships. But, for HAS-A relationships also, I can use Abstract Class instead of using interface.

Secondly, if my class needs to declare variables then I can go for abstract class instead of interface. But, I can implement the same using interface as I can declare properties.

So, I cannot be able to differentiate exactly when to use abstract class and interface
Kornfeld Eliyahu Peter 16-Oct-17 6:52am
Google for 'C# Abstract Class vs Interface' and start reading...
Member 11072126 16-Oct-17 7:06am
Yes, I have read various articles. But, the only valid point am getting is that I for interfaces, I can be implement multiple interfaces but not possible with abstract class. The other points, when analyzed, can be seen that the same thing is possible for both abstract class and interface.
Turn the question round: what happens if you don't use an Interface?

Well, there are then two ways to do it:
1) Define an abstract class.
2) Define a concrete class.

What is the "advantage" of an abstract class over an interface here? Well, you can add predefined behaviour to the class to add default processing - but WCF isn't about processing, it's about data transfer: all the processing that can be defaulted has been done at the remote end already. So what exactly can you add to the abstract class that is of any use to all users of the data? In practice, nothing - because you have no idea what the data will be used for.

What about a concrete class? Same problems as the abstract class, with no really benefits.

And both of them cause a major problem, because C# does not support multiple inheritance. So if you use an abstract or concrete class you cannot derive from any other class. Which means you can't create a TextBox that "knows" about your WCF source because you can't derive from Control, TextBox, or any other UI component and from the WCF class at the same time.

Interfaces have none of those restrictions, so it's a much, much better choice. It forces you to implement the data transfer contract requirements, without restricting what you can subsequently do with the data in your implementation.
Member 11072126 16-Oct-17 7:01am
Thanks. But, I didn't get the advantage of using abstract class over interface.
Suppose, I have an interface named as "ITransact", and it contains a method named as "respModel Show(reqModel)", its taking the requestObject as argument and sending back the responseObject.
Now, the same method can be written as abstract method in an abstract class and I can use inherit the abstract class instead of implementing the interface.

Yes, of course I can't use inherit more than one class but can implement multiple interfaces. Except this point, is there any other major advantages of using interface?
Kornfeld Eliyahu Peter 16-Oct-17 7:10am
It is not about advantages - as there is no absolute advantages fro one or the other... It is all about balance possibilities against requirements.
Do not try and find a one-for-all solution as you will fail with it... Start over with every problem and use only your knowledge and experience...
OriginalGriff 16-Oct-17 7:14am
Stop looking for "an advantage of using an interface in WCF" and look at the disadvantages of not using one: the biggie is that you can't inherit from two classes. And that restricts what you could potentially do enormously: You would have to use encapsulation and increase the number of classes in order to use WCF.

That is the advantage of an interface in all circumstances: it has no default implementation at all, so it can be derived from together with a suitable base class without damaging the contract that the interface implies. That isn't limited to WCF!
Member 11072126 16-Oct-17 9:49am
Still having some doubts. But, anyways thanks to you and Kornfeld for your responses and explanations. It was definitely useful.
OriginalGriff 16-Oct-17 10:15am
You're welcome!
As per my understating lets consider you have hosted one WCF service and you want to publish two method one is for your organization which will contains your companies details and other is for your client , so if you use abstract class you can either use that service for your organization or for your client.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900