Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass object with complex property with web service

I have 2 classes:

    public class testClass1
    {
        public string Name { get; set; }
        public testClass2 testClass2Object { get; set; }
    }

    public class testClass2
    {
        public testClass2() { }

        public testClass2(int i) { TestProperty = i; }

        public int TestProperty { get; set; }
    }

and I want to return first class's object with webMethod :

    [WebMethod]
    public testClass1 testMethod()
    {
        testClass1 test = new testClass1();
        test.Name = "stackoverflow";
        test.testClass2Object = new testClass2(2);
        return test;
    }

but I don't get the values of testClass2 property from testClass1 object.

I tried [Serializable] [XmlInclude(typeof(testClass2))] annotations but nothing changed. Any suggestions?

like image 917
pepela Avatar asked Nov 12 '22 08:11

pepela


1 Answers

If I run the code "as is" and invoke testMethod(), I get...

<testClass1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
     <Name>stackoverflow</Name>
     <testClass2Object>
          <TestProperty>2</TestProperty>
     </testClass2Object>
</testClass1>

Are you expecting something different? Maybe I'm missing something.

If this is part of a bigger project, maybe try putting just this code into a new project and seeing if it could be a setting or other configuration type of problem.

like image 62
Robert Bolton Avatar answered Nov 14 '22 21:11

Robert Bolton