Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Cause a Subclass of a DataContract Class to Appear in the Metadata of a WCF Service?

Tags:

c#

wcf

So let's say I have something like this:

public class Service : IService
{
    public someExposedMethod (someClass param){
    //do some stuff
    }
}

[DataContract]
public class someClass{}

[DataContract]
public class someSubClass : someClass {}

someClass is exposed and can be instantiated within the client, but someSubClass can never be instantiated and is not exposed for some reason. The only way to expose it seems to change the type of param to someSubClass. This is ridiculous. How do I do this? I don't want a method which returns an instance based on a string or something, I want the client and developer to have complete knowledge of what classes they can instantiate.

like image 533
Fugu Avatar asked Feb 26 '23 12:02

Fugu


1 Answers

You are not exposing either of those classes. Your clients never instantiate them (unless you're sharing types, which is a different story).

You are exposing metadata. When "Add Service Reference" is performed, it uses that metadata to create a client-side class that looks like your server-side class. It looks enough like it that it can serialize and deserialize the XML that represents the server-side class.

If you want metadata for the subclasses to be exposed, then you need to add the [KnownType] attribute to the operation for each subclass:

[DataContract]
[KnownType(typeof(someSubClass))]
public class someClass{}

See Known Types.

like image 83
John Saunders Avatar answered Apr 09 '23 02:04

John Saunders