C#SOAP响应格式错误

时间:2018-05-23 10:18:47

标签: c# visual-studio wcf soap

过去2-3天我一直在网上搜索,但仍无法解决这个问题 - 所以现在我来找你们,希望你们知道解决方案。

我正在尝试为客户端创建一个模拟的SOAP服务,并在visual studio中创建了一个SOAP服务。以下是需要模拟的SOAP操作的代码。

[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();
    }

我得到以下回复

<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>

但是,我的客户希望响应看起来像这样

<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>

由于我无法更改客户端的代码,因此我必须从我这边解决这个问题。有什么办法可以避免在我的回复中获取XML标签“SomeWrapperXML”吗?

设置XmlElementAttribute(“”)没有帮助,完全避免它会使它默认为其他东西。 我也查看了MSDN post来改变SOAP消息,但是这个解决方案看起来有点像hacky。

1 个答案:

答案 0 :(得分:1)

我最终将我的模拟服务的返回类型更改为原始返回类型中的字段。

在我的示例中,我需要根据svcutil生成的代码返回类型MyActualResponseType。但是MyActualResponseType只包含MyActualResponse类型的一个字段/ getter。我将SOAP方法更改为:

[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();
}

注意[return:,它使它看起来实际上正在返回类型MyActualResponseType

这个解决方案仍然感觉有点像黑客,但它似乎可以满足我的需求。