Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Return and display a custom class

1 view
Skip to first unread message

Jude Wong

unread,
Dec 14, 2009, 5:53:02 AM12/14/09
to
Hi,

My web method returns a custom class (business object). The issue is that
when I test the web method using a web browser, the returned webpage does not
show the properties of the class except for the following (empty tag).

<DirectorySearchResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/" />

The class DirectorySearchResult has a Count property of type int and is
readonly, i.e. only get accessor defined.

However as soon as I included the set accessor, the webpage shows the Count
property.

Does this mean that each property of the custom class must implement both
the set and get accessors if I wanted the webpage to display them? Testing
the web method using a web browser is convenient way for me to make sure
things work.

Any idea on the above? Thanks.


--
Best regards,

Jude

Steven Cheng

unread,
Dec 15, 2009, 1:12:06 AM12/15/09
to

Hi Jude,

Regarding on the serialization issue mentioned, if you're using ASP.NET
ASMX webservice, then it is an existing limitation of the XmlSerializer the
asmx webserivce uses. The XmlSerializer will not serialize non-public or
readonly members. Here is a web thread discussing on this also:

http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/3dff9c8b-083
b-4ac5-8e43-249c4071d3bf

You can try adding an empty setter on the property as workaround. Or
another approach you can consider is completely control the XML
serialization by implementing the IXmlSerializable interface:

http://msdn.microsoft.com/en-us/library/system.xml.serialization.ixmlseriali
zable.aspx

BTW, for the serialization test (on custom objects return or send in asmx
webmethod), you can using a console application instead of using browser.
Here is a simple code snippet for the serialization test(the output xml is
identical to what the ASMX runtime serialization does):
==================
private static void TestXmlSerializatin()
{
DirectorySearchResult result = new DirectorySearchResult();
result.Items.Add("item1");
result.Items.Add("item2");

XmlSerializer ser = new
XmlSerializer(typeof(DirectorySearchResult));
ser.Serialize(Console.Out, result);

}
=======================

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msd...@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.


--------------------
>From: =?Utf-8?B?SnVkZSBXb25n?= <Jude...@newsgroup.nospam>
>Subject: Return and display a custom class
>Date: Mon, 14 Dec 2009 02:53:02 -0800

0 new messages