【发布时间】: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