none
Can we use Service contract on abstract class ? RRS feed

  • Question

  • can we use service contract attribute on abstract class instead of interface ?

    public class Service1 : AbstractService
        {
            public override string GetData(int value)
            {
                return string.Format("You entered: {0}", value);
            }
        }
    
        [ServiceContract]     public abstract class AbstractService
        {
            [OperationContract]
            public abstract string GetData(int value);
    
            [OperationContract]
            public CompositeType GetDataUsingDataContract(CompositeType composite)
            {
                if (composite.BoolValue)
                {
                    composite.StringValue += "Suffix";
                }
                return composite;
            }
        }
    
    
    	<system.serviceModel>
    		<services>
    			<service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior">
    				<!-- Service Endpoints -->
    				<endpoint address="" binding="wsHttpBinding" contract="WcfService1.AbstractService">
    					<!--
                  Upon deployment, the following identity element should be removed or replaced to reflect the
                  identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity
                  automatically.
              -->
    					<identity>
    						<dns value="localhost"/>
    					</identity>
    				</endpoint>
    				<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    			</service>
    		</services>
    		<behaviors>
    			<serviceBehaviors>
    				<behavior name="WcfService1.Service1Behavior">
    					<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
    					<serviceMetadata httpGetEnabled="true"/>
    					<!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
    					<serviceDebug includeExceptionDetailInFaults="false"/>
    				</behavior>
    			</serviceBehaviors>
    		</behaviors>
    	</system.serviceModel>

    if not possible then i am looking for the reason. from here http://forums.asp.net/t/1711627.aspx?Can+we+use+Service+contract+on+abstract+class+
    i got the answer but answer was not clear to me.

    Friday, April 4, 2014 10:42 AM

Answers

  • You can use service contract on Abstract class but at the time of adding a reference it will give you an error showing "If a class is marked with ServiceContractAttribute, then another service class cannot derive from it". This is quite logical as if it allows then which derived class method should it call.

    Ovais Mehboob Ahmed Khan http://ovaismehboob.wordpress.com

    Friday, April 4, 2014 10:55 AM
  • Hi,

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

    EDIT:
    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.

    For more information, please try to check:
    #Can we use abstract class instead of interface with service contract in WCF:
    http://dotnetinteviewquestions.blogspot.in/2013/08/can-we-use-abstract-class-instead-of_10.html .

    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 7, 2014 6:54 AM
    Moderator
  • Hi,

    Let me elaborate...

    according to WCF, Contract inheritance can only be used among interface types and not class types. So if any class marked with [ServiceContract] attribute the other service class cannot derive from it. Otherwise it gives an error while referencing it.


    Ovais Mehboob Ahmed Khan http://ovaismehboob.wordpress.com

    Monday, April 7, 2014 9:18 AM

All replies

  • You can use service contract on Abstract class but at the time of adding a reference it will give you an error showing "If a class is marked with ServiceContractAttribute, then another service class cannot derive from it". This is quite logical as if it allows then which derived class method should it call.

    Ovais Mehboob Ahmed Khan http://ovaismehboob.wordpress.com

    Friday, April 4, 2014 10:55 AM
  • Hi,

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

    EDIT:
    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.

    For more information, please try to check:
    #Can we use abstract class instead of interface with service contract in WCF:
    http://dotnetinteviewquestions.blogspot.in/2013/08/can-we-use-abstract-class-instead-of_10.html .

    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 7, 2014 6:54 AM
    Moderator
  • whatever u said not very clear to me. i just could not visualize it why the error come. do not under stand u said

    it will give you an error showing "If a class is marked with ServiceContractAttribute, then another service class cannot derive from it". This is quite logical as if it allows then which derived class method should it call.

    if possible please discuss in more details with easy example as a result i can understand the reason why the problem would occur. thanks

    Monday, April 7, 2014 9:08 AM
  • whatever u said not very clear to me. i just could not visualize it why the error come. do not under stand u said.

    u said : If we use an abstract class, it will compile correctly but at run time we will get an error like "Contract inheritance can only be used among interface types."  u said error will come and again u said if we use if we include the [ServiceKnownType] (on a Service Contract) not very clear to me.

    can u plzz explain in details why the error comes and the error vanish when we use ServiceKnownType . what is service known type attribute.......what it does? looking for details discuss with easy example. thanks

    Monday, April 7, 2014 9:11 AM
  • Hi,

    Let me elaborate...

    according to WCF, Contract inheritance can only be used among interface types and not class types. So if any class marked with [ServiceContract] attribute the other service class cannot derive from it. Otherwise it gives an error while referencing it.


    Ovais Mehboob Ahmed Khan http://ovaismehboob.wordpress.com

    Monday, April 7, 2014 9:18 AM