1

I want to use an abstract class to define it as DataContract for WCF. The following example shows how I define my abstract class UpperClass.

[DataContract(IsReference = true)]
[KnownType(typeof(SubClass1))]
[KnownType(typeof(SubClass2))]
abstract public class UpperClass
{
    abstract public void update();
}

SubClass1 and SubClass2 inherit from UpperClass. Actually, they only share 1 method named update(), which is only used internally (not a DataMember). The following code shows the implementation of SubClass1 and SubClass2:

[DataContract]
public class SubClass1 : UpperClass
{
    private int[] _val1;
    private int[] _val2;

    //internal constructor:
    public SubClass1()
    {
        _val1 = new int[2];
        _val2 = new int[2];

        //initialize values of _val1 and _val2 ...
    }

    //internal update method
    public override void update()
    {
        //here comes update formulas...
    }

    //enable simple access from WCF client:
    [DataMember]
    public int[] val1
    {
        get { return _val1; }
        set { _val1 = value; }
    }

    [DataMember]
    public int[] val2
    {
        get { return _val2; }
        set { _val2 = value; }
    }
}





[DataContract]
public class SubClass2 : UpperClass
{
    private int[] _value1;
    private int[] _value2;

    //internal constructor:
    public SubClass2()
    {
        _value1 = new int[12];
        _value2 = new int[12];
    }

    // ...

On the server side, I supply my survice using the following code, which is defined as [OperationContract(IsInitiating = true, IsTerminating = false)] in its Interface-class.

public UpperClass init(string s)
{
    if (s.Equals("SubClass1"))
        obj = new SubClass1();
    else if (s.Equals("SubClass2"))
        obj = new SubClass2();

    return obj;
}

And on the client side, I consume (or initialize) my service using:

UpperClass DatSet = proxy.init("SubClass1");

When I debug to the end of this line, the content of DatSet looks very strange:

{MyExternalLayer.IMyProcessingLayer_proxy.SubClass1} [MyExternalLayer.IMyProcessingLayer_proxy.SubClass1]: {MyExternalLayer.IMyProcessingLayer_proxy.SubClass1} ExtensionData: {System.Runtime.Serialization.ExtensionDataObject} extensionDataField: {System.Runtime.Serialization.ExtensionDataObject} PropertyChanged: null

Boozzz
  • 245
  • 1
  • 6
  • 19
  • What does the exception message say? – nvoigt Jun 17 '14 at 14:02
  • Somehow I got rid of the exception. Sorry, but I don't know why. However, my variable `DatSet` has very weird content. It seems like deserialization to `SubClass1` does not work as I expect it to. – Boozzz Jun 17 '14 at 14:06
  • It looks quite normal to me. Maybe you could explain what you expect? – nvoigt Jun 17 '14 at 14:12
  • I want `DatSet` to be an object of type `SubClass1`, which has two variables: `val1` and `val2`. Instead, it seems to be of type `UpperClass`, which does not have the two variables `val1` and `val2`. But when I write `SubClass1 DatSet = proxy.init("SubClass1");` I get a type conversion exception. How can my client consume those two variables `val1` and `val2`? – Boozzz Jun 17 '14 at 14:23
  • But that's the point of putting it into a UpperClass reference, no? I think your confusion stems from the OOP concept and has nothing to do with WCF. Try the same without the proxy just instantiating the classes directly, I think you will have the same problems. – nvoigt Jun 17 '14 at 14:27
  • that is very likely, yes ;-) – Boozzz Jun 17 '14 at 14:28
  • Possible duplicate of [Using WCF with abstract classes](https://stackoverflow.com/questions/3101756/using-wcf-with-abstract-classes) – Ashraf Sada Aug 26 '17 at 05:23

1 Answers1

0

I pasted your Data Contracts into a WCF service and they work just fine, although I do not recommend having business logic (abstract public void update();) in your data contracts and transcending service boundaries. But that's just an opinion.

Eric Scherrer
  • 3,328
  • 1
  • 19
  • 34