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;
}
}
Config:
<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. i got the answer but answer was not clear to me.
1st answer
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.
this statement is not clear then another service class cannot derive from it". This is quite logical as if it allows then which derived class method should it call.
2nd answer
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.
another guy said possible if we use [ServiceKnownType] (on a Service Contract) or [KnownType] on a Data Contract
looking for more insight that if not possible then why not possible. if possible explain with sample scenario & sample code. thanks
If You use abstract class it will compile but while run time it will throw an exception saying
You can use normal class with ServiceContractAttribute.