开发者

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

开发者 https://www.devze.com 2023-02-01 12:40 出处:网络
So let\'s say I have something like this: public class Service : IService { public someExposedMethod (someClass param){

So let's say I have something like this:

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

[DataContract]
public class som开发者_开发百科eClass{}

[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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号