开发者

How to generate proxy for a child class when my web service return a parent class?

开发者 https://www.devze.com 2022-12-24 19:13 出处:网络
I have a JAX-WS WebService like this: public class ParentClass{ public String str1; } public class ChildClass : Par开发者_开发问答entClass{

I have a JAX-WS WebService like this:

public class ParentClass{
    public String str1;
}
public class ChildClass : Par开发者_开发问答entClass{
    public String str2;
}

public class WebService{
    public ParentClass WebMethod(){
        return GetFirstChildClass();    //Return a child class
    }
}

When I generate proxy for this web service by Visual Studio, VS just generate proxy for ParentClass but I need ChildClass too. For workaround I add a dummy method to WebService that return ChildClass to generate proxy for ChildClass in client.

public class WebService{
    ...
    //This is a dummy method to generate proxy for ChildClass in client.
    public ChildClass DummyWebMethod(){
        return null;
    }
}

In addition I write web service in java (JAX-WS) and my client is a SilverLight Application. Is there a better solution for this problem?

tanx for your help ;)


After a deep search in web found @XmlSeeAlso annotation to resolve this problems. We should add this annotation above our services to generate needed references, like: http://download.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/XmlSeeAlso.html

@XmlSeeAlso({ParentClass.class})
public class WebService{
...


If you called WebService.WebMethod directly as an in-process DLL, it would return a value of type ParentClass, which you would have to manually downcast to ChildClass. That is how inheritance and polymorphism are supposed to work. Why should a web service proxy class behave any differently?

EDIT: based on comments...

In a .NET WCF service, you would solve the problem by telling the serializer about the child class, e.g.

[DataContract]
[KnownType(typeof(ChildClass))]
public class ParentClass {
    public String str1;
}

[DataContract]
public class ChildClass : ParentClass {
    public String str2;
}

The child class is then included in the generated client proxy classes, and you can cast to it. I would imagine a similar mechanism exists in JAX-WS.

0

精彩评论

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