服务引用生成与 Web 服务不兼容的 SOAP 代理



根据我用来获取引用的WSDL,我尝试使用的操作定义如下。

出于安全考虑,我已在所有引用中将服务的名称替换为"MyService"。

<message name="MyService_fetchOperation">
<part name="user" type="xsd:string"/>
<part name="passwd" type="xsd:string"/>
<part name="package" type="xsd:string"/>
<part name="txType" type="xsd:string"/>
<part name="swref" type="xsd:string"/>
<part name="force" type="xsd:string"/>
</message>

我正在用 C# 构建一个请求:

using (var client = new MyService.MyServiceGatewayClient())
{
response = await client.fetchOperationAsync(USER, PASS, PACKAGE, "509", "", "0");
}

在这个问题的上下文中,这个请求的作用或用途并不重要。

请求(来自 Fiddler)如下所示:

POST https://myservice.gateway.thing HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: ""
Host: myservice.gateway.thing
Content-Length: 816
Expect: 100-continue
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<ActivityId CorrelationId="04a608d9-fbfd-4a4d-b26b-e57098352dff" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">
80000162-0005-fb00-b63f-84710c7967bb</ActivityId>
<VsDebuggerCausalityData xmlns="http://schemas.microsoft.com/vstudio/diagnostics/servicemodelsink">uIDPo6GoRTTueVZOiE2QS303TwoAAAAA6GBrNOg50ESdf6d7KUk2nMLdj/sn/wxCqk4Df+zV1yQACQAA</VsDebuggerCausalityData>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<fetchOperation xmlns="http://myservice.gateway.thing/v1">
<user>TEST9876</user>
<passwd>test4139</passwd><!-- plain text password ftw! -->
<package>SWITCHON</package>
<txType>509</txType>
<swref/>
<force>0</force>
</fetchOperation>
</s:Body>
</s:Envelope>

此请求在服务上返回一个错误,指出它找不到子元素"user"。

我已经向服务管理员询问了这个问题,他说我需要在fetchOperation标签中重新定义命名空间,并给出了以下示例:

<v1:fetchOperation xmlns:v1="http://myservice.gateway.thing/v1">
...
</v1:fetchOperation>

我有两个问题:

  1. 为什么有必要进行这样的改变?这将如何解决服务在请求中看不到"缺失"元素的问题?
  2. 如果需要此更改,为什么在 VS 中添加服务引用不会生成实际有效的 SOAP 代理?是服务开发人员的不一致还是我做错了什么?

为服务生成代理客户端的另一种方法是使用svutil.exe,这是一种命令行工具,用于从元数据、Web 或 wsdl 文件生成服务模型代码。

Microsoft Docs svcutil.exe

它包含在Visual Studio安装中,您可以打开开发人员命令提示符并像这样执行它:

svcutil http://url/service.svc /Language=c#

您还可以使用Windows SDK Microsoft安装svcutil:https://www.microsoft.com/en-us/download/details.aspx?id=8279

对于一个真实的例子,我使用了这个服务:http://www.chemspider.com/MassSpecAPI.asmx

svcutil http://www.chemspider.com/MassSpecAPI.asmx /Language=c#

它生成了MassSpecAPI.cs文件,位于生成的代理类的一部分下面:

//------------------------------------------------------------------------------
// <auto-generated>
/ ....
// </auto-generated>
//------------------------------------------------------------------------------
[assembly: System.Runtime.Serialization.ContractNamespaceAttribute("http://www.chemspider.com/", ClrNamespace="www.chemspider.com")]
namespace www.chemspider.com
{
using System.Runtime.Serialization;

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.CollectionDataContractAttribute(Name="ArrayOfString", Namespace="http://www.chemspider.com/", ItemName="string")]
public class ArrayOfString : System.Collections.Generic.List<string>
{
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="ECompression", Namespace="http://www.chemspider.com/")]
public enum ECompression : int
{
[System.Runtime.Serialization.EnumMemberAttribute()]
eGzip = 0,
}

最新更新