Answered by:
Override custom object return type name in results

Question
-
Hopefully I can explain this well. I have a webservice that is accepting a complex data object (i.e. a class defined in C#) and is returning the same type of object. It returns just fine, but the name of the object type is overriden to be methodname Result. Is there a way to override this tag to be the name of the actual object type? Code and WSDL info follows. Thanks!
Object Code
public class Test_Object : ISerializable { private string myname; private int myid; public string My_Name { get { return myname; } set { myname = value; } } public int My_ID { get { return myid; } set { myid = value; } } public override int GetHashCode() { return myid.GetHashCode(); } public Test_Object() { myname = "Dude"; myid = 42; } public Test_Object(SerializationInfo info, StreamingContext ctxt) { } //Serialization function. public void GetObjectData(SerializationInfo info, StreamingContext ctxt) { } }
Web Method
[WebMethod] [TraceExtension] public Test_Object SetTestObject(Test_Object t) { if (t == null) { SoapException se = new SoapException("Messed up data", SoapException.ClientFaultCode, Context.Request.Url.AbsoluteUri); throw se; } else return t; }
WSDL
<s:element name="SetTestObject"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="t" type="tns:Test_Object" /> </s:sequence> </s:complexType> </s:element> <s:complexType name="Test_Object"> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="My_Name" type="s:string" /> <s:element minOccurs="1" maxOccurs="1" name="My_ID" type="s:int" /> </s:sequence> </s:complexType> <s:element name="SetTestObjectResponse"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="SetTestObjectResult" type="tns:Test_Object" /> </s:sequence> </s:complexType> </s:element>
B0)Tuesday, April 14, 2009 9:18 PM
Answers
-
try:
[WebMethod]
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("SetTestObject",
Use = System.Web.Services.Description.SoapBindingUse.Literal,
ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
[return: XmlElement(ElementName="Test_Object")]
public Test_Object SetTestObject(Test_Object t)
{...}- Proposed as answer by Derek Wade Tuesday, February 9, 2010 5:11 PM
- Edited by Derek Wade Tuesday, February 9, 2010 5:21 PM revised content
- Marked as answer by ManOLama Thursday, May 6, 2010 11:43 PM
Tuesday, February 9, 2010 5:08 PM
All replies
-
Please say what you're trying to accomplish. Why do you care what name is in the XML?
John Saunders
Use File->New Project to create Web Service Projects
Use WCF for All New Web Service Development, instead of old ASMX or obsolete WSEWednesday, April 15, 2009 6:54 PMModerator -
Thanks for posting back John,
I'm just being picky about wanting to have the root element of the response have the name of the object instead of the name of the method with Result appended.
I.e. I'm getting this:
<SetTestObjectResponse xmlns="http://beta.com/api/"> <SetTestObjectResult> <My_Name>string</My_Name> <My_ID>int</My_ID> </SetTestObjectResult> </SetTestObjectResponse>
When I really want this:
<SetTestObjectResponse xmlns="http://beta.com/api/"> <Test_Object> <My_Name>string</My_Name> <My_ID>int</My_ID> </Test_Object> </SetTestObjectResponse>
Wednesday, April 15, 2009 7:57 PM -
try:
[WebMethod]
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("SetTestObject",
Use = System.Web.Services.Description.SoapBindingUse.Literal,
ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
[return: XmlElement(ElementName="Test_Object")]
public Test_Object SetTestObject(Test_Object t)
{...}- Proposed as answer by Derek Wade Tuesday, February 9, 2010 5:11 PM
- Edited by Derek Wade Tuesday, February 9, 2010 5:21 PM revised content
- Marked as answer by ManOLama Thursday, May 6, 2010 11:43 PM
Tuesday, February 9, 2010 5:08 PM