我正在尝试编写一个使用第三方供应商提供的Java Web服务的C#客户端应用程序。
直接从URL创建服务引用因XML格式不正确而出现问题。使用良好的旧剪切和粘贴我创建了一个包含WSDL数据的文本文件并修复了格式问题,然后使用文件路径创建了服务引用。
现在我有了服务引用,当我在服务客户端上调用任何方法时,我收到了XML验证错误。我也在客户端调用Open()方法时得到错误。
System.InvalidOperationException: The top XML element 'request' from namespace '' references distinct types TestApp1.TheService.CreateBulkOffers and TestApp1.TheService.WSCreateBulkOffersResponse. Use XML attributes to specify another XML name or namespace for the element or types.
at System.Xml.Serialization.XmlReflectionImporter.ReconcileAccessor(Accessor accessor, NameTable accessors)
at System.Xml.Serialization.XmlReflectionImporter.ReconcileLocalAccessor(ElementAccessor accessor, String ns)
at System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, String ns, Type choiceIdentifierType, Boolean rpc, Boolean openModel, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportMemberMapping(XmlReflectionMember xmlReflectionMember, String ns, XmlReflectionMember[] xmlReflectionMembers, Boolean rpc, Boolean openModel, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc, Boolean openModel, RecursionLimiter limiter)
与TestApp1.TheService.CreateBulkOffers和TestApp1.TheService.WSCreateBulkOffersResponse相关的WSDL片段如下:
<xsd:complexType name="CreateBulkOffers">
<xsd:complexContent mixed="false">
<xsd:extension base="ns93:WSCommonInfo">
<xsd:sequence>
<xsd:element name="securityPolicyName" nillable="true" type="xsd:string">
</xsd:element>
<xsd:element name="folderID" nillable="true" type="xsd:long">
</xsd:element>
<xsd:element name="templateName" type="xsd:string">
</xsd:element>
<xsd:element minOccurs="0" maxOccurs="unbounded" name="offers" type="ns93:WSBulkOfferInfo">
</xsd:element>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="WSCreateBulkOffersResponse">
<xsd:sequence>
<xsd:element name="status" type="ns93:WSRequestStatus" />
<xsd:element minOccurs="1" maxOccurs="unbounded" name="offerInfo" nillable="true" type="ns93:WSOfferInfoStatus" />
</xsd:sequence>
</xsd:complexType>
从WSDL生成的类在这里:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.18408")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://webservices.CustomerServices/3.0")]
public partial class CreateBulkOffers : WSCommonInfo {
private string securityPolicyNameField;
private System.Nullable<long> folderIDField;
private string templateNameField;
private WSBulkOfferInfo[] offersField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true, Order=0)]
public string securityPolicyName {
get {
return this.securityPolicyNameField;
}
set {
this.securityPolicyNameField = value;
this.RaisePropertyChanged("securityPolicyName");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true, Order=1)]
public System.Nullable<long> folderID {
get {
return this.folderIDField;
}
set {
this.folderIDField = value;
this.RaisePropertyChanged("folderID");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=2)]
public string templateName {
get {
return this.templateNameField;
}
set {
this.templateNameField = value;
this.RaisePropertyChanged("templateName");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("offers", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=3)]
public WSBulkOfferInfo[] offers {
get {
return this.offersField;
}
set {
this.offersField = value;
this.RaisePropertyChanged("offers");
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.18408")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://webservices.CustomerServices/3.0")]
public partial class WSCreateBulkOffersResponse : object, System.ComponentModel.INotifyPropertyChanged {
private WSRequestStatus statusField;
private WSOfferInfoStatus[] offerInfoField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)]
public WSRequestStatus status {
get {
return this.statusField;
}
set {
this.statusField = value;
this.RaisePropertyChanged("status");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("offerInfo", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true, Order=1)]
public WSOfferInfoStatus[] offerInfo {
get {
return this.offerInfoField;
}
set {
this.offerInfoField = value;
this.RaisePropertyChanged("offerInfo");
}
}
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName) {
System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if ((propertyChanged != null)) {
propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
}
有关如何解决此错误的任何建议都会很棒。
修改 - 添加完整例外
System.InvalidOperationException: There was an error reflecting 'request'. ---> System.InvalidOperationException: The top XML element 'request' from namespace '' references distinct types TestApp1.TheService.CreateBulkOffers and TestApp1.TheService.WSCreateBulkOffersResponse. Use XML attributes to specify another XML name or namespace for the element or types.
at System.Xml.Serialization.XmlReflectionImporter.ReconcileAccessor(Accessor accessor, NameTable accessors)
at System.Xml.Serialization.XmlReflectionImporter.ReconcileLocalAccessor(ElementAccessor accessor, String ns)
at System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, String ns, Type choiceIdentifierType, Boolean rpc, Boolean openModel, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportMemberMapping(XmlReflectionMember xmlReflectionMember, String ns, XmlReflectionMember[] xmlReflectionMembers, Boolean rpc, Boolean openModel, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc, Boolean openModel, RecursionLimiter limiter)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc, Boolean openModel, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(String elementName, String ns, XmlReflectionMember[] members, Boolean hasWrapperElement, Boolean rpc, Boolean openModel, XmlMappingAccess access)
at System.ServiceModel.Description.XmlSerializerOperationBehavior.Reflector.XmlSerializerImporter.ImportMembersMapping(XmlName elementName, String ns, XmlReflectionMember[] members, Boolean hasWrapperElement, Boolean rpc, Boolean isEncoded, String mappingKey)
at System.ServiceModel.Description.XmlSerializerOperationBehavior.Reflector.OperationReflector.ImportMembersMapping(String elementName, String ns, XmlReflectionMember[] members, Boolean hasWrapperElement, Boolean rpc, String mappingKey)
at System.ServiceModel.Description.XmlSerializerOperationBehavior.Reflector.OperationReflector.LoadBodyMapping(MessageDescription message, String mappingKey, MessagePartDescriptionCollection& rpcEncodedTypedMessageBodyParts)
at System.ServiceModel.Description.XmlSerializerOperationBehavior.Reflector.OperationReflector.CreateMessageInfo(MessageDescription message, String key)
at System.ServiceModel.Description.XmlSerializerOperationBehavior.Reflector.OperationReflector.EnsureMessageInfos()
at System.ServiceModel.Description.XmlSerializerOperationBehavior.Reflector.EnsureMessageInfos()
at System.ServiceModel.Description.XmlSerializerOperationBehavior.CreateFormatter()
at System.ServiceModel.Description.XmlSerializerOperationBehavior.System.ServiceModel.Description.IOperationBehavior.ApplyClientBehavior(OperationDescription description, ClientOperation proxy)
at System.ServiceModel.Description.DispatcherBuilder.BindOperations(ContractDescription contract, ClientRuntime proxy, DispatchRuntime dispatch)
at System.ServiceModel.Description.DispatcherBuilder.ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime)
at System.ServiceModel.Description.DispatcherBuilder.BuildProxyBehavior(ServiceEndpoint serviceEndpoint, BindingParameterCollection& parameters)
at System.ServiceModel.Channels.ServiceChannelFactory.BuildChannelFactory(ServiceEndpoint serviceEndpoint, Boolean useActiveAutoClose)
at System.ServiceModel.ChannelFactory.CreateFactory()
at System.ServiceModel.ChannelFactory.OnOpening()
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.ClientBase`1.System.ServiceModel.ICommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.ClientBase`1.Open()
at TestApp1.Program.LaunchService() in c:\Work\TestApp1\TestApp1\Program.cs:line 36