locked
WCF Service Contract on concrete class or Abstract class RRS feed

  • Question

  • User88744855 posted

    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 4:55 AM

Answers

All replies

  • User-417640953 posted

    Hi mou_inn,

    if possible please discuss the problem point wise for which we can not write

    service contract on abstract class

    First we should see the define of the service contract attribute.

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

    From above code we can see the AttributeTarget for Class and Interface. So it is surely we can use the service contract on abstract class.

    [DataContract]
    [ServiceKnownType(typeof(InheritedClassName))]
    public abstract class BaseClass
    {
    
    [DataMember]
    public int Id { get; set; }
    
    }

    However, we should point the child classes by using the ServiceKnownType attribute.

    Similar issues with answers, please check them.

    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://stackoverflow.com/questions/616808/exposing-wcf-subclasses-based-on-an-abstract-class

    Thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 14, 2014 4:14 AM
  • User88744855 posted

    i want a working sample code where people create service contract on anstract class like below examle and consumer can call the service also. when i search google to know that can we create service contract on abstract class then i got some write up and all write said that there will be error when u try to run the service but u are saying it is possible. so plzz come up with a working sample code. thanks

    [ServiceContract]
    public abstract class TheService
    {
       // more stuff here
    }

    config entry

    <services>
        <service name="YourServiceName">
            <endpoint address="" behaviorConfiguration="httpBehavior" binding="webHttpBinding" contract="TheService"/>
        </service>
    </services>
    Tuesday, April 15, 2014 6:05 AM