Simon Larsen
Simon Larsen

Reputation: 21

C# SOAP response is wrong format

I have been searching the web for the last 2-3 days and remain unable to figure this one out - so now I come to you guys in the hopes that you know a solution.

I am trying to make a mocked SOAP service for a client and have made a SOAP service in visual studio. The following is the code for the SOAP action that needs to be mocked.

[WebService(Namespace = "http://mynamespace.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class MyServiceStub1 : System.Web.Services.WebService, MyServiceBindingSoap
{        
    [WebMethod]
    [SoapDocumentMethod(Action = "http://mynamespace.com/MySoapAction", 
        ParameterStyle = SoapParameterStyle.Bare)]
    [return: System.Xml.Serialization.XmlElementAttribute("SomeWrapperXML")]
    public MyActualResponseType MyServiceRequest(
        [XmlElement(ElementName = "MyRequestName")] MyServiceRequestType myServiceRequest)
    {
        return new MyActualResponseType();
    }

And I get the following response

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <SomeWrapperXML">
         <MyActualResponseType>
         </MyActualResponseType>
      </SomeWrapperXML>
   </soap:Body>
</soap:Envelope>

However, my clients expect the response to look like this

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
     <MyActualResponseType>
     </MyActualResponseType>
   </soap:Body>
</soap:Envelope>

And since I cannot change the code on my clients side, I have to fix this from my side. Is there any way I can avoid getting the XML tag "SomeWrapperXML" in my response?

Setting XmlElementAttribute("") did not help and avoiding it altogether will just make it default to something else. I have also looked at the MSDN post for altering the SOAP message, but that solution seems a bit hacky.

Upvotes: 1

Views: 660

Answers (1)

Simon Larsen
Simon Larsen

Reputation: 21

I ended up changing the return type of my mocked service to a field in the original return type.

In my example I needed to return the type MyActualResponseType according to the code generated by svcutil. However MyActualResponseType only contained one field/getter for a type MyActualResponse. I changed my SOAP method to this:

[WebMethod]
[SoapDocumentMethod(Action = "http://mynamespace.com/MySoapAction", 
    ParameterStyle = SoapParameterStyle.Bare)]
[return: System.Xml.Serialization.XmlElementAttribute("MyActualResponseType")]
public MyActualResponse MyServiceRequest(
[XmlElement(ElementName = "MyRequestName")] MyServiceRequestType myServiceRequest)
{
    return new MyActualResponse();
}

Notice the [return: which makes it look like it is actually returning the type MyActualResponseType.

This solution still feels like a bit of hack, but it seems to work fine for my needs.

Upvotes: 1

Related Questions