【发布时间】:2021-04-06 22:51:51
【问题描述】:
更新: 我有来自 SAP Web 服务的 WSDL 文件,该文件在 Visual Studio 代码中作为连接服务导入,如下所示:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace SAPWebService
{
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.2")]
[System.ServiceModel.ServiceContractAttribute(Namespace="urn:sap-com:document:sap:rfc:functions", ConfigurationName="ServiceReference1.ZMM_SIF_KARAKTERISTIKE_MAT")]
public interface ZMM_SIF_KARAKTERISTIKE_MAT
{
[System.ServiceModel.OperationContractAttribute(Action="urn:sap-com:document:sap:rfc:functions:ZMM_SIF_KARAKTERISTIKE_MAT:ZMM_SIF_KARAKTE" +
"RISTIKE_MATRequest", ReplyAction="urn:sap-com:document:sap:rfc:functions:ZMM_SIF_KARAKTERISTIKE_MAT:ZMM_SIF_KARAKTE" +
"RISTIKE_MATResponse")]
[System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults=true)]
System.Threading.Tasks.Task<SAPWebService.ZMM_SIF_KARAKTERISTIKE_MATResponse1> ZMM_SIF_KARAKTERISTIKE_MATAsync(SAPWebService.ZMM_SIF_KARAKTERISTIKE_MATRequest request);
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.2")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="urn:sap-com:document:sap:rfc:functions")]
public partial class ZMM_SIF_KARAKTERISTIKE_MAT1
{
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.2")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:sap-com:document:sap:rfc:functions")]
public partial class ZMM_KARAKTERISTIKE_MAT_LINE
{
private string kLASAField;
private string kARAKTERISTIKAField;
private string vREDNOSTField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)]
public string KLASA
{
get
{
return this.kLASAField;
}
set
{
this.kLASAField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=1)]
public string KARAKTERISTIKA
{
get
{
return this.kARAKTERISTIKAField;
}
set
{
this.kARAKTERISTIKAField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=2)]
public string VREDNOST
{
get
{
return this.vREDNOSTField;
}
set
{
this.vREDNOSTField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.2")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="urn:sap-com:document:sap:rfc:functions")]
public partial class ZMM_SIF_KARAKTERISTIKE_MATResponse
{
private ZMM_KARAKTERISTIKE_MAT_LINE[] kARAKTERISTIKAField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)]
[System.Xml.Serialization.XmlArrayItemAttribute("item", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public ZMM_KARAKTERISTIKE_MAT_LINE[] KARAKTERISTIKA
{
get
{
return this.kARAKTERISTIKAField;
}
set
{
this.kARAKTERISTIKAField = value;
}
}
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.2")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(IsWrapped=false)]
public partial class ZMM_SIF_KARAKTERISTIKE_MATRequest
{
[System.ServiceModel.MessageBodyMemberAttribute(Namespace="urn:sap-com:document:sap:rfc:functions", Order=0)]
public SAPWebService.ZMM_SIF_KARAKTERISTIKE_MAT ZMM_SIF_KARAKTERISTIKE_MAT;
public ZMM_SIF_KARAKTERISTIKE_MATRequest()
{
}
public ZMM_SIF_KARAKTERISTIKE_MATRequest(SAPWebService.ZMM_SIF_KARAKTERISTIKE_MAT ZMM_SIF_KARAKTERISTIKE_MAT)
{
this.ZMM_SIF_KARAKTERISTIKE_MAT = ZMM_SIF_KARAKTERISTIKE_MAT;
}
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.2")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(IsWrapped=false)]
public partial class ZMM_SIF_KARAKTERISTIKE_MATResponse1
{
[System.ServiceModel.MessageBodyMemberAttribute(Namespace="urn:sap-com:document:sap:rfc:functions", Order=0)]
public SAPWebService.ZMM_SIF_KARAKTERISTIKE_MATResponse ZMM_SIF_KARAKTERISTIKE_MATResponse;
public ZMM_SIF_KARAKTERISTIKE_MATResponse1()
{
}
public ZMM_SIF_KARAKTERISTIKE_MATResponse1(SAPWebService.ZMM_SIF_KARAKTERISTIKE_MATResponse ZMM_SIF_KARAKTERISTIKE_MATResponse)
{
this.ZMM_SIF_KARAKTERISTIKE_MATResponse = ZMM_SIF_KARAKTERISTIKE_MATResponse;
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.2")]
public interface ZMM_SIF_KARAKTERISTIKE_MATChannel : SAPWebService.ZMM_SIF_KARAKTERISTIKE_MAT, System.ServiceModel.IClientChannel
{
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.2")]
public partial class ZMM_SIF_KARAKTERISTIKE_MATClient : System.ServiceModel.ClientBase<SAPWebService.ZMM_SIF_KARAKTERISTIKE_MAT>, SAPWebService.ZMM_SIF_KARAKTERISTIKE_MAT
{
public ZMM_SIF_KARAKTERISTIKE_MATClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
System.Threading.Tasks.Task<SAPWebService.ZMM_SIF_KARAKTERISTIKE_MATResponse1> SAPWebService.ZMM_SIF_KARAKTERISTIKE_MAT.ZMM_SIF_KARAKTERISTIKE_MATAsync(SAPWebService.ZMM_SIF_KARAKTERISTIKE_MATRequest request)
{
return base.Channel.ZMM_SIF_KARAKTERISTIKE_MATAsync(request);
}
public System.Threading.Tasks.Task<SAPWebService.ZMM_SIF_KARAKTERISTIKE_MATResponse1> ZMM_SIF_KARAKTERISTIKE_MATAsync(SAPWebService.ZMM_SIF_KARAKTERISTIKE_MAT ZMM_SIF_KARAKTERISTIKE_MAT)
{
SAPWebService.ZMM_SIF_KARAKTERISTIKE_MATRequest inValue = new SAPWebService.ZMM_SIF_KARAKTERISTIKE_MATRequest();
inValue.ZMM_SIF_KARAKTERISTIKE_MAT = ZMM_SIF_KARAKTERISTIKE_MAT;
return ((SAPWebService.ZMM_SIF_KARAKTERISTIKE_MAT)(this)).ZMM_SIF_KARAKTERISTIKE_MATAsync(inValue);
}
public virtual System.Threading.Tasks.Task OpenAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
}
}
重要更新:
当我将 wsdl 添加为连接服务时,我收到此错误:
Cannot import wsdl:port
Detail: There was an error importing a wsdl:binding that the wsdl:port is dependent on.
XPath to wsdl:binding: //wsdl:definitions[@targetNamespace='urn:sap-com:document:sap:rfc:functions']/wsdl:binding[@name='250_soap12']
XPath to Error Source: //wsdl:definitions[@targetNamespace='urn:sap-com:document:sap:rfc:functions']/wsdl:service[@name='ZMM_SIF_KARAKTERISTIKE_MAT']/wsdl:port[@name='250_soap12']
Cannot import wsdl:binding
Detail: Name cannot begin with the '2' character, hexadecimal value 0x32.
Parameter name: name
XPath to Error Source: //wsdl:definitions[@targetNamespace='urn:sap-com:document:sap:rfc:functions']/wsdl:binding[@name='250_soap12']
Cannot import wsdl:port
Detail: There was an error importing a wsdl:binding that the wsdl:port is dependent on.
XPath to wsdl:binding: //wsdl:definitions[@targetNamespace='urn:sap-com:document:sap:rfc:functions']/wsdl:binding[@name='250']
XPath to Error Source: //wsdl:definitions[@targetNamespace='urn:sap-com:document:sap:rfc:functions']/wsdl:service[@name='ZMM_SIF_KARAKTERISTIKE_MAT']/wsdl:port[@name='250']
Cannot import wsdl:binding
Detail: Name cannot begin with the '2' character, hexadecimal value 0x32.
Parameter name: name
XPath to Error Source: //wsdl:definitions[@targetNamespace='urn:sap-com:document:sap:rfc:functions']/wsdl:binding[@name='250']
The optional WSDL extension element 'Policy' from namespace 'http://schemas.xmlsoap.org/ws/2004/09/policy' was not handled.
XPath: //wsdl:definitions[@targetNamespace='urn:sap-com:document:sap:rfc:functions']/wsdl:portType[@name='ZMM_SIF_KARAKTERISTIKE_MAT']/wsdl:operation[@name='ZMM_SIF_KARAKTERISTIKE_MAT']
The optional WSDL extension element 'Policy' from namespace 'http://schemas.xmlsoap.org/ws/2004/09/policy' was not handled.
XPath: //wsdl:definitions[@targetNamespace='urn:sap-com:document:sap:rfc:functions']/wsdl:portType[@name='ZMM_SIF_KARAKTERISTIKE_MAT']
我尝试通过以下方法从 C# 中使用它:
public class SAPDemoApi : ISAPDemoApi
{
public readonly string serviceUrl = "http://GIRKV06S04.gir.local:8000/sap/bc/srt/rfc/sap/zmm_sif_karakteristike_mat/250/zmm_sif_karakteristike_mat/250";
public readonly EndpointAddress endpointAddress;
public readonly BasicHttpBinding basicHttpBinding;
public SAPDemoApi()
{
endpointAddress = new EndpointAddress(serviceUrl);
basicHttpBinding =
new BasicHttpBinding(endpointAddress.Uri.Scheme.ToLower() == "http" ?
BasicHttpSecurityMode.None : BasicHttpSecurityMode.Transport);
//Please set the time accordingly, this is only for demo
basicHttpBinding.OpenTimeout = TimeSpan.MaxValue;
basicHttpBinding.CloseTimeout = TimeSpan.MaxValue;
basicHttpBinding.ReceiveTimeout = TimeSpan.MaxValue;
basicHttpBinding.SendTimeout = TimeSpan.MaxValue;
}
public async Task<ZMM_SIF_KARAKTERISTIKE_MATResponse> GetData()
{
var client = await GetInstanceAsync();
var response = await client.ZMM_SIF_KARAKTERISTIKE_MATAsync(new ZMM_SIF_KARAKTERISTIKE_MATClient(basicHttpBinding, endpointAddress));
return response.ZMM_SIF_KARAKTERISTIKE_MATResponse;
}
public async Task<ZMM_SIF_KARAKTERISTIKE_MATClient> GetInstanceAsync()
{
return await Task.Run(() => new ZMM_SIF_KARAKTERISTIKE_MATClient(basicHttpBinding, endpointAddress));
}
}
注意:我使用了http://GIRKV06S04.gir.local:8000/sap/bc/srt/rfc/sap/zmm_sif_karakteristike_mat/250/zmm_sif_karakteristike_mat/250,因为我在向 Web 服务发送请求时从 SoapUI 获得了它(之前我已将 WSDL 文件导入到 SoapUI)。
当我尝试将其命名为 var output = await sapDemoApi.GetData(); 时,出现错误:
System.InvalidOperationException: There was an error reflecting 'ZMM_SIF_KARAKTERISTIKE_MAT'. ---> System.NotSupportedException: Cannot serialize interface SAPWebService.ZMM_SIF_KARAKTERISTIKE_MAT. at System.Xml.Serialization.TypeDesc.CheckSupported() at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
更新:
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header/>
<soap-env:Body>
<n0:ZMM_SIF_KARAKTERISTIKE_MATResponse xmlns:n0="urn:sap-com:document:sap:rfc:functions">
<KARAKTERISTIKA>
<item>
<KLASA>Z_TRUPCI_MAT</KLASA>
<KARAKTERISTIKA>VRSTADRVETA</KARAKTERISTIKA>
<VREDNOST>0</VREDNOST>
</item>
<item>
<KLASA>Z_TRUPCI_MAT</KLASA>
<KARAKTERISTIKA>VRSTADRVETA</KARAKTERISTIKA>
<VREDNOST>1</VREDNOST>
</item>
<item>
<KLASA>Z_TRUPCI_MAT</KLASA>
<KARAKTERISTIKA>VRSTADRVETA</KARAKTERISTIKA>
<VREDNOST>2</VREDNOST>
</item>
<item>
<KLASA>Z_TRUPCI_MAT</KLASA>
<KARAKTERISTIKA>VRSTADRVETA</KARAKTERISTIKA>
<VREDNOST>26</VREDNOST>
</item>
<item>
<KLASA>Z_TRUPCI_MAT</KLASA>
<KARAKTERISTIKA>VRSTADRVETA</KARAKTERISTIKA>
<VREDNOST>27</VREDNOST>
</item>
<item>
<KLASA>Z_TRUPCI_MAT</KLASA>
<KARAKTERISTIKA>VRSTADRVETA</KARAKTERISTIKA>
<VREDNOST>28</VREDNOST>
</item>
<item>
<KLASA>Z_TRUPCI_MAT</KLASA>
<KARAKTERISTIKA>VRSTADRVETA</KARAKTERISTIKA>
<VREDNOST>29</VREDNOST>
</item>
<item>
<KLASA>Z_TRUPCI_MAT</KLASA>
<KARAKTERISTIKA>VRSTADRVETA</KARAKTERISTIKA>
<VREDNOST>30</VREDNOST>
</item>
<item>
<KLASA>Z_TRUPCI_MAT</KLASA>
<KARAKTERISTIKA>VRSTADRVETA</KARAKTERISTIKA>
<VREDNOST>31</VREDNOST>
</item>
<item>
<KLASA>Z_TRUPCI_MAT</KLASA>
<KARAKTERISTIKA>VRSTADRVETA</KARAKTERISTIKA>
<VREDNOST>32</VREDNOST>
</item>
<item>
<KLASA>Z_GRADJA_MAT</KLASA>
<KARAKTERISTIKA>TIPDRVETA</KARAKTERISTIKA>
<VREDNOST>O</VREDNOST>
</item>
<item>
<KLASA>Z_GRADJA_MAT</KLASA>
<KARAKTERISTIKA>TIPDRVETA</KARAKTERISTIKA>
<VREDNOST>N</VREDNOST>
</item>
</KARAKTERISTIKA>
</n0:ZMM_SIF_KARAKTERISTIKE_MATResponse>
</soap-env:Body>
</soap-env:Envelope>
【问题讨论】:
-
xml 与架构不匹配。没有看到 xml 响应就无法判断。架构是由生成 c# 类的工具生成的。 WSDL 有 xml 中没有的额外标签。所以通常你必须移动获取 xml 的第一个标签和 WSDL 中的类是相同的。
-
我已经更新了问题和回复。那么,我需要更换什么?
-
您的响应 xml 以信封开头,但 c# 以标签 ZMM_SIF_KARAKTERISTIKE_MATResponse 开头。请参阅:docs.microsoft.com/en-us/previous-versions/dotnet/…
-
抱歉,我不明白我应该做什么以及在哪里解决。在您发送给我的示例中,获取 ZMM_SIF_KARAKTERISTIKE_MATResponse 是合乎逻辑的,因为我在 Body 中发送 ZMM_SIF_KARAKTERISTIKE_MAT。我到底需要更换什么?
-
在本例中使用 ResonseElementName:docs.microsoft.com/en-us/dotnet/api/…。另一种选择是为 Envelope 和 Body 添加您自己的两个 c# 类。
标签: c# web-services wsdl saprfc