none
WCF Service Contract on concrete class or Abstract class

    Question

  • from the article i came to know that we can write service contract on concrete class instead of interface like this way

    namespace Foo
    {
        [ServiceContract(Namespace="http://kennyw.com/WCFservices/")]
        [WebService(Namespace="http://kennyw.com/sampleservices/")]
        public class MyService : System.Web.Services.WebService
        { 
            [WebMethod]
            [OperationContract]
            public string Hello(string name)
            { 
                return string.Format(“Hello {0}.”, name);
    
            }
        }
    }
    
    In this case the web config service configuration should look like
    
    <services>
        <service name="YourServiceName">
            <endpoint address="" behaviorConfiguration="httpBehavior" binding="webHttpBinding" contract="Foo.MyService"/>
        </service>
    </services>

    so my question is then why we can not write service contract on abstract class because interface is abstract like abstract class.

    if possible please discuss the problem point wise for which we can not write service contract on abstract class. i am looking for the actual problem which will occur when we host the service. thanks

    Friday, April 11, 2014 8:54 AM

Answers

  • Hi,

    Because it has used the serviceknowtype, it will work if we include the [ServiceKnownType] (on a Service Contract) or [KnownType] on a Data Contract. Basically we have to tell WCF what to serialize/deserialize.

    If we do not add the serviceknowtype, it will not work.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, April 18, 2014 9:54 AM
  • can u plzz come up with a working sample code where we write service contract on abstract class.

    thanks

    Hi,

    I am sorry for the late reply, then for the working sample code of using the abstract class, please try to check this thread.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, May 02, 2014 1:47 AM

All replies

  • yes, an interface is indeed abstract & so we "must" implement it. Using interfaces is a good design as there is a common understanding between the provider & the consumer.

    hope this helps!

    Friday, April 11, 2014 9:26 AM
  • my question is then why we can not write service contract on abstract.  do u know all the reason??
    Friday, April 11, 2014 6:58 PM
  • Hi,

    This is not possible. Because ServiceContractAttribute and OperationContractAttribute can only be used on Interface.

    And if any class marked with [ServiceContract] attribute, then the other service class cannot derive from it. Otherwise it gives an error while referencing it.

    For more information, please try to check this similar thread:
    #Can we use abstract class instead of interface with service contract in WCF:
    http://social.msdn.microsoft.com/Forums/en-US/e7f919b3-2c26-434c-bdec-0b8d2e463d39/can-we-use-abstract-class-instead-of-interface-with-service-contract-in-wcf?forum=wcf .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 14, 2014 5:25 AM
  • u said : This is not possible. Because ServiceContractAttribute and OperationContractAttribute can only be used on Interface. just see the below example.

    [AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Interface, Inherited = false, AllowMultiple = false)]
    public sealed class ServiceContractAttribute : Attribute

    it seems service contract can be written on class and interface. am i right? see the link

    http://forums.asp.net/t/1980106.aspx?WCF+Service+Contract+on+concrete+class+or+Abstract+class

    Thursday, April 17, 2014 8:40 AM
  • Hi,

    Because it has used the serviceknowtype, it will work if we include the [ServiceKnownType] (on a Service Contract) or [KnownType] on a Data Contract. Basically we have to tell WCF what to serialize/deserialize.

    If we do not add the serviceknowtype, it will not work.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, April 18, 2014 9:54 AM
  • can u plzz come up with a working sample code where we write service contract on abstract class.

    thanks

    Monday, April 21, 2014 10:06 AM
  • can u plzz come up with a working sample code where we write service contract on abstract class.

    thanks

    Hi,

    I am sorry for the late reply, then for the working sample code of using the abstract class, please try to check this thread.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, May 02, 2014 1:47 AM