views:

22

answers:

1

I have a WSE 3.0 based web service, and a WinForms client application that makes use of the types defined in that service's References.cs, but subclasses them to provide some additional functionality required by the client.

However, when I pass an instance of the subclass back to the web service, even though I explicitly cast back to the original type, I get the dreaded:

Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

in reference to my derived type.

Code summary:

public class DefinedInReferenceCs
{ 
    // ... 
}

public class ClientSubclass : DefinedInReferenceCs
{
    // My extra stuff
}

public class MyClient
{
    public CallTheWebService(ClientSubclass  obj)
    {
        // obj is an instance of ClientSubclass,  cast as DefinedInReferenceCs
        svc.MyMethod((DefinedInReferenceCs)obj); 
        // Throws an exception complaining that ClientSubclass is not statically known
    }
}

I can get around this by explicitly creating a DefinedInReferenceCs instance and performing a deep copy of the relevant fields. Is there a better way?

+1  A: 

Since the XML Serializer uses Reflection to work with objects, there is no way to keep it from seeing that your base class reference references an instance of your derived class. Yes, you must use XmlInclude.

OTOH, have you considered using partial classes to add functionality to the original proxy classes? That way, you would not need a derived class at all.

John Saunders
Where exactly does XmlInclude need to be specified? That's a nice idea to use partial classes. Fields not known to the server will just be initialized with default values when instantiated client-side?
Eric J.