【问题标题】:C# SOAP response is wrong formatC# SOAP 响应格式错误
【发布时间】:2018-05-23 10:18:46
【问题描述】:

过去 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。

【问题讨论】:

  • response的数据类型是什么?
  • Hej 西蒙。使用 svcutil,您可以从需要模拟的服务的 WSDL 生成 C# 接口。这样做可以确保您的服务与其他服务相匹配。 stackoverflow.com/questions/950150/…
  • @ChetanRanpariya 类型为MyActualResponseType。我已经编辑了这个问题,以便更清楚地说明这一点。 @Hans svcutil 确实用于生成服务。

标签: c# visual-studio wcf soap


【解决方案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 类型。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 2014-06-25
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    相关资源
    最近更新 更多