2012-08-03 168 views
6

我們試圖使用SOAP請求與使用Visual Studio C#的預訂API進行交互。.NET中的SOAP請求

我們已經與另一個API的Web服務進行了接口而沒有問題,但是這個問題已證明存在問題。

我們已經將它作爲服務參考添加了,並且Visual Studio已經爲它和web.config項目生成了所有類,以便一切都很順利。

然而,當我們嘗試做一個簡單的請求,客戶端SOAP服務,我們得到以下錯誤:

Namespace='com.hrs.soap.hrs' is not supported with rpc\literal SOAP. The wrapper element has to be unqualified.

的代碼是:

PingRequest pingrequest = new PingRequest();  
SoapServiceClient service = new SoapServiceClient(); 
service.ping(pingrequest); 

這什麼在網站被添加。配置文件:

<system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="HRSSoapServiceBinding" closeTimeout="00:01:00" 
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
      allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
      maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
      messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
      useDefaultWebProxy="true"> 
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
       maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
      <security mode="None"> 
      <transport clientCredentialType="None" proxyCredentialType="None" 
       realm="" /> 
      <message clientCredentialType="UserName" algorithmSuite="Default" /> 
      </security> 
     </binding> 
     </basicHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="..." 
      binding="basicHttpBinding" bindingConfiguration="HRSSoapServiceBinding" 
      contract="HRSService.HRSSoapService" name="HRSSoapServicePort" /> 
    </client> 
    </system.serviceModel> 

根據文檔請求應該看起來像fo下面

<complexType name="PingRequest"> 
    <complexContent> 
     <extension base="tns:Request"> 
      <sequence> 
       <element name="echoData" type="xsd:string"/> 
      </sequence> 
     </extension> 
    </complexContent> 
</complexType> 

完整堆棧跟蹤:llowing

[InvalidOperationException: Namespace='com.hrs.soap.hrs' is not supported with rpc\literal SOAP. The wrapper element has to be unqualified.] 
    System.Xml.Serialization.XmlReflectionImporter.CheckTopLevelAttributes(XmlAttributes a, String accessorName) +842469 
    System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, String ns, Type choiceIdentifierType, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +4072651 
    System.Xml.Serialization.XmlReflectionImporter.ImportMemberMapping(XmlReflectionMember xmlReflectionMember, String ns, XmlReflectionMember[] xmlReflectionMembers, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +699 
    System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +298 

[InvalidOperationException: There was an error reflecting 'HRSException'.] 
    System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(XmlReflectionMember[] xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +1051 
    System.Xml.Serialization.XmlReflectionImporter.ImportMembersMapping(String elementName, String ns, XmlReflectionMember[] members, Boolean hasWrapperElement, Boolean rpc, Boolean openModel, XmlMappingAccess access) +137 
    System.ServiceModel.Description.XmlSerializerImporter.ImportMembersMapping(XmlName elementName, String ns, XmlReflectionMember[] members, Boolean hasWrapperElement, Boolean rpc, Boolean isEncoded, String mappingKey) +237 
    System.ServiceModel.Description.OperationReflector.ImportMembersMapping(String elementName, String ns, XmlReflectionMember[] members, Boolean hasWrapperElement, Boolean rpc, String mappingKey) +134 
    System.ServiceModel.Description.OperationReflector.ImportFaultElement(FaultDescription fault, XmlQualifiedName& elementName) +458 
    System.ServiceModel.Description.OperationReflector.GenerateXmlSerializerFaultContractInfos() +153 
    System.ServiceModel.Description.OperationReflector.EnsureMessageInfos() +929 
    System.ServiceModel.Description.Reflector.EnsureMessageInfos() +98 
    System.ServiceModel.Description.XmlSerializerOperationBehavior.CreateFormatter() +32 
    System.ServiceModel.Description.XmlSerializerOperationBehavior.System.ServiceModel.Description.IOperationBehavior.ApplyClientBehavior(OperationDescription description, ClientOperation proxy) +42 
    System.ServiceModel.Description.DispatcherBuilder.BindOperations(ContractDescription contract, ClientRuntime proxy, DispatchRuntime dispatch) +94 
    System.ServiceModel.Description.DispatcherBuilder.ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) +247 
    System.ServiceModel.Description.DispatcherBuilder.BuildProxyBehavior(ServiceEndpoint serviceEndpoint, BindingParameterCollection& parameters) +342 
    System.ServiceModel.Channels.ServiceChannelFactory.BuildChannelFactory(ServiceEndpoint serviceEndpoint, Boolean useActiveAutoClose) +85 
    System.ServiceModel.ChannelFactory.CreateFactory() +43 
    System.ServiceModel.ChannelFactory.OnOpening() +23 
    System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +274 
    System.ServiceModel.ChannelFactory.EnsureOpened() +107 
    System.ServiceModel.ChannelFactory`1.CreateChannel(EndpointAddress address, Uri via) +134 
    System.ServiceModel.ChannelFactory`1.CreateChannel() +29 
    System.ServiceModel.ClientBase`1.CreateChannel() +91 
    System.ServiceModel.ClientBase`1.CreateChannelInternal() +26 
    System.ServiceModel.ClientBase`1.get_Channel() +261 
    BLL.HRSService.HRSSoapServiceClient.ping(HRSPingRequest pingRequest) in C:\Code\-------\BLL\Service References\HRSService\Reference.cs:15850 
    -------.Test.Ping() in C:\Code\-------\Test.aspx.cs:48 
    -------.Test.Page_Load(Object sender, EventArgs e) in C:\Code\-------\Test.aspx.cs:24 
    System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 
    System.Web.UI.Control.OnLoad(EventArgs e) +91 
    System.Web.UI.Control.LoadRecursive() +74 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207 

我們安裝小提琴手,也沒有XML請求通過,我們可以看到,大概是因爲它發出任何事情之前拋出一個異常去。

我們需要在我們的設置中進行更改以完成此項工作?

UPDATE

WSDL:可用[這裏] [1]

UPDATE

繼我已經複製到本地的WSDL和所作的修訂davidfmatheson出色的意見,建議。我仍然得到在不同的地方同樣的錯誤,雖然可能:

[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)] 
     BLL.HRSService.pingResponse BLL.HRSService.HRSSoapService.ping(BLL.HRSService.pingRequest request) { 
      return base.Channel.ping(request); 
     } 

     public BLL.HRSService.HRSPingResponse ping(BLL.HRSService.HRSPingRequestWrapper ping1) { 
      BLL.HRSService.pingRequest inValue = new BLL.HRSService.pingRequest(); 
      inValue.ping = ping1; 
      BLL.HRSService.pingResponse retVal = ((BLL.HRSService.HRSSoapService)(this)).ping(inValue); 
      return retVal.pingResponse1; 
     } 

失敗是回base.Channel.ping(請求)線;,錯誤命名空間='com.hrs.soap.hrs'不受rpc \ literal SOAP支持。包裝元素必須是不合格的。

我也試圖明確設置SOAPBinding:

Exception Details: System.InvalidOperationException: Namespace='com.hrs.soap.hrs' is not supported with rpc\literal SOAP. The wrapper element has to be unqualified. 

Source Error: 

Line 15917:  [System.Web.Services.Protocols.SoapDocumentMethod(Use = System.Web.Services.Description.SoapBindingUse.Literal)] 
Line 15918:  BLL.HRSService.pingResponse BLL.HRSService.HRSSoapService.ping(BLL.HRSService.pingRequest request) { 
Line 15919:   return base.Channel.ping(request); 
Line 15920:  } 
Line 15921: 

OK,現在我已經修改了SOAP綁定風格在我的本地WSDL記錄和重建的類和我有一個不同的錯誤:

Unable to generate a temporary class (result=1). 
error CS0030: Cannot convert type 'BLL.HRSService.HRSHotelTaxDetail[]' to 'BLL.HRSService.HRSHotelTaxDetail' 
error CS0030: Cannot convert type 'BLL.HRSService.HRSHotelAmenityCriterion[]' to 'BLL.HRSService.HRSHotelAmenityCriterion' 
error CS0029: Cannot implicitly convert type 'BLL.HRSService.HRSHotelTaxDetail' to 'BLL.HRSService.HRSHotelTaxDetail[]' 
error CS0029: Cannot implicitly convert type 'BLL.HRSService.HRSHotelAmenityCriterion' to 'BLL.HRSService.HRSHotelAmenityCriterion[]' 

堆棧跟蹤:

[InvalidOperationException: Unable to generate a temporary class (result=1). 
error CS0030: Cannot convert type 'BLL.HRSService.HRSHotelTaxDetail[]' to 'BLL.HRSService.HRSHotelTaxDetail' 
error CS0030: Cannot convert type 'BLL.HRSService.HRSHotelAmenityCriterion[]' to 'BLL.HRSService.HRSHotelAmenityCriterion' 
error CS0029: Cannot implicitly convert type 'BLL.HRSService.HRSHotelTaxDetail' to 'BLL.HRSService.HRSHotelTaxDetail[]' 
error CS0029: Cannot implicitly convert type 'BLL.HRSService.HRSHotelAmenityCriterion' to 'BLL.HRSService.HRSHotelAmenityCriterion[]' 
] 
    System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, XmlSerializerCompilerParameters xmlParameters, Evidence evidence) +1024 
    System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters parameters, Assembly assembly, Hashtable assemblies) +1997 
    System.Xml.Serialization.XmlSerializer.GetSerializersFromCache(XmlMapping[] mappings, Type type) +772 
    System.Xml.Serialization.XmlSerializer.FromMappings(XmlMapping[] mappings, Type type) +4066268 
    System.ServiceModel.Description.SerializerGenerationContext.GenerateSerializers() +185 
    System.ServiceModel.Description.SerializerGenerationContext.GetSerializer(Int32 handle) +102 
    System.ServiceModel.Description.MessageInfo.get_BodySerializer() +17 
    System.ServiceModel.Dispatcher.XmlSerializerOperationFormatter.SerializeBody(XmlDictionaryWriter writer, MessageVersion version, String action, MessageDescription messageDescription, Object returnValue, Object[] parameters, Boolean isRequest) +112 

[CommunicationException: There was an error in serializing body of message pingRequest: 'Unable to generate a temporary class (result=1). 
error CS0030: Cannot convert type 'BLL.HRSService.HRSHotelTaxDetail[]' to 'BLL.HRSService.HRSHotelTaxDetail' 
error CS0030: Cannot convert type 'BLL.HRSService.HRSHotelAmenityCriterion[]' to 'BLL.HRSService.HRSHotelAmenityCriterion' 
error CS0029: Cannot implicitly convert type 'BLL.HRSService.HRSHotelTaxDetail' to 'BLL.HRSService.HRSHotelTaxDetail[]' 
error CS0029: Cannot implicitly convert type 'BLL.HRSService.HRSHotelAmenityCriterion' to 'BLL.HRSService.HRSHotelAmenityCriterion[]' 
'. Please see InnerException for more details.] 
    System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +9440287 
    System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +345 
    BLL.HRSService.HRSSoapService.ping(pingRequest request) +0 
    BLL.HRSService.HRSSoapServiceClient.BLL.HRSService.HRSSoapService.ping(pingRequest request) in C:\Code\...\BLL\Service References\HRSService\Reference.cs:17311 
    BLL.HRSService.HRSSoapServiceClient.ping(HRSPingRequestWrapper ping1) in C:\Code\...\BLL\Service References\HRSService\Reference.cs:17317 
    BLL.HRS_Integration.Ping() in C:\Code\...\BLL\General\HRSIntegration.cs:55 
    VenueOptions.Test.Page_Load(Object sender, EventArgs e) in C:\Code\...\Test.aspx.cs:24 
    System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 
    System.Web.UI.Control.OnLoad(EventArgs e) +91 
    System.Web.UI.Control.LoadRecursive() +74 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207 

終於要求了!

提琴手顯示PING如下:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><ping xmlns="com.hrs.soap.hrs"><pingRequest xmlns=""><credentials><clientType>...</clientType><clientKey>...</clientKey><clientPassword>...</clientPassword><customerKey xsi:nil="true"/></credentials><myHRSCredentials xsi:nil="true"/><echoData>Hello HRS</echoData></pingRequest></ping></s:Body></s:Envelope> 
+0

檢查堆棧跟蹤並查看您的代碼正在破解的位置。它是在請求構建失敗還是響應反序列化失敗? – linkerro 2012-08-03 12:55:43

+0

我不確定是否誠實,Stack Trace added – cusimar9 2012-08-03 13:02:12

+0

出於好奇,wsdl.exe是否能夠生成工作代理? – 2012-08-03 13:14:02

回答

6

您正在嘗試使用來自.NET的RPC字面服務,所以你可能需要的服務轉化爲文檔文字。請參閱MSDN Blog

編輯:更完整的答案

我們的目標是能夠使用文檔文字,但仍發送相同的消息。我們來關注一下ping的操作。現在,您的WSDL已經類型定義爲:

<complexType name="HRSPingRequest"> 
    <complexContent> 
     <extension base="tns:HRSRequest"> 
      <sequence> 
       <element name="echoData" type="xsd:string"/> 
      </sequence> 
     </extension> 
    </complexContent> 
</complexType> 

和您的郵件定義爲:

<message name="HRSSoapService_pingRequest"> 
    <part name="pingRequest" type="tns:HRSPingRequest"/> 
</message> 

而且你的綁定定義爲:

<binding name="HRSSoapServiceBinding" type="tns:HRSSoapService"> 
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> 
    <operation name="ping"> 
     <soap:operation soapAction=""/> 
     <input> 
      <soap:body namespace="com.hrs.soap.hrs" use="literal"/> 
     </input> 
     <output> 
      <soap:body namespace="com.hrs.soap.hrs" use="literal"/> 
     </output> 
     <fault name="HRSException"> 
      <soap:fault name="HRSException" use="literal"/> 
     </fault> 
    </operation> 
</binding> 

所以在本地,我們改變「RPC 「to」document「,然後將WSDL修復爲實際文檔/文字:

<element name="ping" type="tns:HRSPingRequestWrapper" /> 
<complexType name="HRSPingRequestWrapper"> 
    <sequence> 
     <element name="pingRequest" type="tns:HRSPingRequest" /> 
    </sequence> 
</complexType> 
<complexType name="HRSPingRequest"> 
    <complexContent> 
     <extension base="tns:HRSRequest"> 
      <sequence> 
       <element name="echoData" type="xsd:string"/> 
      </sequence> 
     </extension> 
    </complexContent> 
</complexType> 

... 

<message name="HRSSoapService_pingRequest"> 
    <part name="pingRequest" element="tns:ping"/> 
</message> 

現在生成此WSDL並查看它是否發送ping請求。如果你沒有修復Response結構,它可能不知道返回的內容,但你至少應該看到通過線路發送的請求。您可以使用SoapUI加載這兩個WSDL並查看生成的請求類型。目標是發送相同的請求,只是使用文檔/文字而不是rpc/literal。

+0

感謝您的鏈接,我們將通過一個很好的看看。不,我們無法控制Web服務生產者。這一切似乎比它應該更復雜。 – cusimar9 2012-08-03 13:10:18

+0

我們無法將服務轉換爲doc-literal,因爲我們無法控制服務本身。我們已經嘗試在所有自動生成的類中添加[SoapRpcService(Use = SoapBindingUse.Literal)],但我們只是得到相同的錯誤。 – cusimar9 2012-08-03 16:03:14

+1

我認爲博客文章正在討論只從服務中獲取WSDL和模式,在本地保存它,按照描述修改它,從修改後的本地WSDL生成存根,然後仍然將客戶端指向原始端點地址。這是一篇舊的博客文章,我從來沒有嘗試過,但我認爲這就是他們的建議。 – davidfmatheson 2012-08-03 16:10:09