Reputation: 526
This is a question I got asked in an interview.
When you create a WCF service, you get two files; "IService.cs" and "Service.cs". Why is it a class implementing an interface versus a class inheriting an abstract class. Don't reply saying that you cannot put a [servicecontract] attribute over the abstract class. I know you can only apply it to interfaces, but why?
Upvotes: 4
Views: 5280
Reputation: 31071
Several reasons I can think of:
[ServiceContract]
has no implementation, why waste the one inheritance slot you have on it? For example, all our service classes inherit from an abstract ServiceBase
class which provides common context state and methods, in addition to implementing the [ServiceContract]
interface. However, even if that were not the case, I would still leave the base class slot free for future use.[ServiceContract]
if appropriate.[ServiceContract]
from another, then adding service classes to the same inheritance tree will ruin it.Upvotes: 1
Reputation: 41757
WCF completely decouples the client from the service, if you specify the implementation of the service as the service you have tightly coupled your client to the service.
Upvotes: 6
Reputation: 161773
One can implement more than one interface. One can only inherit a single abstract class.
Upvotes: 6