1

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

1
  • 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? Commented Nov 11, 2009 at 14:30

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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
0

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.

2 Comments

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?
Yes, the "setter" was the problem, as 'jvenema' also suggested. Thank you very much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.