views:

600

answers:

2

I've started developing a new 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

A: 

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.
azheglov
Thank you very much for your fast response... I could see that after I changed all the private fields into public, then it worked... But why the public properties are not serialized if they reference private fields?
Cristi
btw... XMLRoot didn't help
Cristi
Yes, the "setter" was the problem, as 'jvenema' also suggested. Thank you very much
Cristi
A: 

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.

jvenema
Thank you very much for your valuable suggestion. Something is telling me you are right about the "setter" :). Let's see (5 mins). Thank you
Cristi
You're "THE MAN" ! Thank you very much. PROBLEM SOLVED!!!PS: This web site is very useful when you're in trouble!
Cristi