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?
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 abstractServiceBase
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.One can implement more than one interface. One can only inherit a single abstract class.
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.