Actually, ServiceContract and OperationContract
can be used on classes directly instead of just interfaces (although i do not
recommend such a thing myself).They can even be used on
abstract classes and the code will compile successfully; rightly so,
because according to the C# compiler there is nothing wrong.However while hosting the
service, the ServiceHost will throw the exception mentioned by Otomii Lu
because according to WCF rules, if a class has been marked with a
ServiceContract attribute then another class cannot inherit from it. And if you
think about it, it is pretty logical. Imagine what would happen if you declared
a service like this. When a client calls the Add method, should the Add method
implemented in the abstract class get called or the one in the Maths class.
[ServiceContract]
public abstract class MathsAbstract
{
[OperationContract]
public abstract int Subtract(int num1, int num2);
[OperationContract]
public virtual int Add(int num1, int num2)
{
return num1 + num2;
}
}
public class Maths : MathsAbstract
{
public override int Add(int num1, int num2)
{
return num1 + num2 + 10;
}
public override int Subtract(int num1, int num2)
{
return num1 - num2;
}
}
No comments:
Post a Comment