locked
Interface VS Abstract Class in WCF RRS feed

  • Question

  • User832284694 posted

    Hi,

    Why interface are use in WCF in service contract ? We can achive the same functionality by using abstract class but why interface are prefered in WCF over abstract class ?

    Thanks.

    Friday, June 21, 2013 5:08 AM

Answers

  • User-837620913 posted

    If you use an abstract class, it will compile correctly but at runtime you will get an error like "Contract inheritance can only be used among interface types."

    EDIT:

    It will work if you include the [ServiceKnownType] (on a Service Contract) or [KnownType] on a Data Contract. Basically you have to tell WCF what to serialize/deserialize.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 21, 2013 5:28 AM
  • User-488622176 posted

    And additionally : objects in .NET can only inherit from one other object. An interface is a way to enforce object behavior according to the scheme described in the interface. An object can hence implement many interfaces.

    In many SOA logic implementations, the notion of use case is translated into a Service Contract (interface). A service component can implement many service interfaces, for example when grouping functionality that belongs together and to allow reuse of specific code (private members, ...)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 21, 2013 9:42 AM
  • User-1678381114 posted

    Actually, ServiceContract and OperationContract can be used on classes directly instead of just interfaces


    You can check the URL for more

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/e7f919b3-2c26-434c-bdec-0b8d2e463d39/can-we-use-abstract-class-instead-of-interface-with-service-contract-in-wcf

    http://msdn.microsoft.com/en-us/library/vstudio/hh273127(v=vs.100).aspx


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 21, 2013 4:43 PM

All replies

  • User-837620913 posted

    If you use an abstract class, it will compile correctly but at runtime you will get an error like "Contract inheritance can only be used among interface types."

    EDIT:

    It will work if you include the [ServiceKnownType] (on a Service Contract) or [KnownType] on a Data Contract. Basically you have to tell WCF what to serialize/deserialize.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 21, 2013 5:28 AM
  • User-488622176 posted

    And additionally : objects in .NET can only inherit from one other object. An interface is a way to enforce object behavior according to the scheme described in the interface. An object can hence implement many interfaces.

    In many SOA logic implementations, the notion of use case is translated into a Service Contract (interface). A service component can implement many service interfaces, for example when grouping functionality that belongs together and to allow reuse of specific code (private members, ...)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 21, 2013 9:42 AM
  • User-1678381114 posted

    Actually, ServiceContract and OperationContract can be used on classes directly instead of just interfaces


    You can check the URL for more

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/e7f919b3-2c26-434c-bdec-0b8d2e463d39/can-we-use-abstract-class-instead-of-interface-with-service-contract-in-wcf

    http://msdn.microsoft.com/en-us/library/vstudio/hh273127(v=vs.100).aspx


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 21, 2013 4:43 PM