开发者

C# web service ... return nested custom objects

开发者 https://www.devze.com 2022-12-11 17:05 出处:网络
I\'ve started developing a new 开发者_JS百科web service in VS2005. There is only one method: [WebMethod]

I've started developing a new 开发者_JS百科web service in VS2005. There is only one method:

[WebMethod]  
[XmlInclude(typeof(Person))]  
public PersonAction GetAction()  
{  
   PersonAction action = new PersonAction();  
   return action;  
}  

where PersonAction class contains a field with a reference to a Person class

[Serializable]  
public class PersonAction    
{  
    private string actionName = "XAction";  
    private Person person1;  
    private Person person2;  

    public PersonAction() 
    {
        this.person = new Person();
        this.person.Name = "P1";
    }

    public string Name
    {
        get
        {
            return this.actionName;
        }
    }
    [XmlElement(Type = typeof(Person))]
    public Person Person1
    {
        get
        {
            return this.person1;
        }
    }
}  

I've built it, run it... but wsdl it always contains an empty tag for PersonAction ... no definition for the embedded types is available, so I get always null on the client side.

XmlElement , XmlInclude , [Serializable] apparently have no effect...

I am sure I miss something.

For sure somebody faced this problem in the past and knows the solution. I would really appreciate any piece of code for VS2005 (.NET 2.0) that would help.

Thank you


Your change that makes the variables public seems to fix it, but doesn't really. The service is now serializing the public variable, rather than the properties.

Try changing the variables back to private, and adding a "setter" function as well. I believe that's required for serialization.


You may be missing XmlRoot attribute on your Person Action class. XmlInclude may also be unnecessary.

Edited to add:

  • I use (in my ASMX/.NET 2.0 Web services) XmlRoot and don't use XmlInclude.
  • I noticed one strange thing: your properties are get-only. I believe the convention is to make data-holding properties in serializable classes get-set.
  • one more thing you can try is to take your service's WSDL, run it through WSDL utility, see how wsdl.exe generates your serializable classes and see the differences - this is a .NET 2.0/ASMX-specific advice, of course.
0

精彩评论

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

关注公众号