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
        public class MyService : System.Web.Services.WebService
            public string Hello(string name)
                return string.Format(“Hello {0}.”, name);

    In this case the web config service configuration should look like

        <service name="YourServiceName">
            <endpoint address="" behaviorConfiguration="httpBehavior" binding="webHttpBinding" contract="Foo.MyService"/>

    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


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.

    public abstract class BaseClass
    public int Id { get; set; }

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

    Similar issues with answers, please check them.




    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

    public abstract class TheService
       // more stuff here

    config entry

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