2014-02-26 33 views
0

我正在努力消費SAP Web服務。我經歷過很多教程,並在MSDN上的多個線程,但我不能找到我的問題的解決方案:消費SAP Web服務 - 請求發送兩次,每次都失敗

運行我的程序與此錯誤結束:

CommunicationException not handled. 
-> unknown Messageversion 

System.ServiceModel.CommunicationException wurde nicht behandelt. 
HResult=-2146233087 
Message=Unbekannte Nachrichtenversion. 
Source=mscorlib 
StackTrace: 
Server stack trace: 
    bei System.ServiceModel.Channels.ReceivedMessage.ReadStartEnvelope(XmlDictionaryReader reader) 
    bei System.ServiceModel.Channels.BufferedMessage..ctor(IBufferedMessageData messageData, RecycledMessageState recycledMessageState, Boolean[] understoodHeaders, Boolean understoodHeadersModified) 
    bei System.ServiceModel.Channels.TextMessageEncoderFactory.TextMessageEncoder.ReadMessage(ArraySegment`1 buffer, BufferManager bufferManager, String contentType) 
    bei System.ServiceModel.Channels.HttpInput.DecodeBufferedMessage(ArraySegment`1 buffer, Stream inputStream) 
    bei System.ServiceModel.Channels.HttpInput.ReadBufferedMessage(Stream inputStream) 
    bei System.ServiceModel.Channels.HttpInput.ParseIncomingMessage(HttpRequestMessage httpRequestMessage, Exception& requestException) 
    bei System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) 
    bei System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout) 
    bei System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout) 
    bei System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) 
    bei System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) 
    bei System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message) 
Exception rethrown at [0]: 
    bei System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) 
    bei System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) 
    bei ConsoleApplication1.ServiceReference2.ZTST_MASS_CREATE_USER_gut.ZTST_MASS_USER_CREATE(ZTST_MASS_USER_CREATERequest request) 
    bei ConsoleApplication1.ServiceReference2.ZTST_MASS_CREATE_USER_gutClient.ConsoleApplication1.ServiceReference2.ZTST_MASS_CREATE_USER_gut.ZTST_MASS_USER_CREATE(ZTST_MASS_USER_CREATERequest request) in C:\Users\tstaat\documents\visual studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Service References\ServiceReference2\Reference.cs:Zeile 138. 
    bei ConsoleApplication1.ServiceReference2.ZTST_MASS_CREATE_USER_gutClient.ZTST_MASS_USER_CREATE(ZTST_USER_PWD[] USER) in C:\Users\tstaat\documents\visual studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Service References\ServiceReference2\Reference.cs:Zeile 144. 
    bei ConsoleApplication1.Program.Main(String[] args) in C:\Users\tstaat\documents\visual studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:Zeile 61. 
    bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
    bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
    bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
    bei System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
    bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    bei System.Threading.ThreadHelper.ThreadStart() 
InnerException: 

什麼似乎不可思議對我說:

我測試了通過SoapUI創建的請求。我不得不刪除的評論,然後正常工作:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:sap-com:document:sap:rfc:functions"> 
    <soapenv:Header/> 
    <soapenv:Body> 
     <urn:ZTST_MASS_USER_CREATE> 
     <USER> 
      <item> 
      <!-- erased comment --> 
       <USER>o</USER> 
       <PWD>kkkk</PWD> 
      </item> 
     </USER> 
     </urn:ZTST_MASS_USER_CREATE> 
    </soapenv:Body> 
</soapenv:Envelope> 

所以最後我的代碼看起來是這樣的:

var binding = new BasicHttpBinding(); 
      binding.MaxReceivedMessageSize = int.MaxValue; 
      binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic; 
      binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; 
      binding.AllowCookies = true; 

      var address = new EndpointAddress("http://host/relativeurn); 

      //create client 
      ServiceReference2.ZTST_MASS_CREATE_USER_gutClient client = new ServiceReference2.ZTST_MASS_CREATE_USER_gutClient(binding, address); 

      Console.WriteLine("Created Client"); 

      //define credentials 
      client.ClientCredentials.UserName.UserName = "User"; 
      client.ClientCredentials.UserName.Password = "PWD"; 


      ServiceReference2.ZTST_USER_PWD[] parameter = new ServiceReference2.ZTST_USER_PWD[2]; 
      parameter.Initialize(); 

      parameter[] = something 

      client.open(); 

      parameter = client.ZTST_MASS_USER_CREATE(parameter); 

的是,在Web服務請求發送第二個錯誤兩次:一次一次沒有認證。

我通過這個線程去:http://social.msdn.microsoft.com/Forums/vstudio/en-US/d9f99777-f702-4c0e-8198-b110f8eacdd9/net-c-console-application-calling-sap-webservice?forum=wcf

它並不完全適合我,造成

parameter = client.ZTST_MASS_USER_CREATE(parameter); 

我計劃兩個請求;第二個是由服務器(先不進行身份驗證)接受,但返回一個錯誤:

<?xml version="1.0" encoding="utf-8"?> 
    <wsdl:definitions targetNamespace="urn:sap-com:document:sap:rfc:functions" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="urn:sap-com:document:sap:rfc:functions" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> 
     <wsdl:documentation> 
      <sidl:sidl xmlns:sidl="http://www.sap.com/2007/03/sidl"/> 
     </wsdl:documentation> 
     <wsp:UsingPolicy wsdl:required="true"/> 
     <wsp:Policy wsu:Id="BN_BN_ZTST_MASS_CREATE_USER"> 
      <saptrnbnd:OptimizedXMLTransfer uri="http://xml.sap.com/2006/11/esi/esp/binxml" xmlns:saptrnbnd="http://www.sap.com/webas/710/soap/features/transportbinding/" wsp:Optional="true"/> 
      <saptrnbnd:OptimizedXMLTransfer uri="http://www.w3.org/2004/08/soap/features/http-optimization" xmlns:saptrnbnd="http://www.sap.com/webas/710/soap/features/transportbinding/" wsp:Optional="true"/> 
       <wsp:ExactlyOne xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:sapsp="http://www.sap.com/webas/630/soap/features/security/policy" xmlns:sp="http://docs.oasis-open.org/ws-sx/ws-securitypolicy/200702" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:wst="http://docs.oasis-open.org/ws-sx/ws-trust/200512" xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex"> 
        <wsp:All> 
         <sp:TransportBinding> 
          <wsp:Policy> 
           <sp:TransportToken> 
            <wsp:Policy> 
             <sp:HttpsToken> 
              <wsp:Policy> 
              <sp:HttpBasicAuthentication/> 
              </wsp:Policy> 
             </sp:HttpsToken> 
            </wsp:Policy> 
           </sp:TransportToken> 
           <sp:AlgorithmSuite> 
            <wsp:Policy> 
             <sp:TripleDesRsa15/> 
            </wsp:Policy> 
           </sp:AlgorithmSuite> 
           <sp:Layout> 
            <wsp:Policy> 
             <sp:Strict/> 
            </wsp:Policy> 
           </sp:Layout> 
          </wsp:Policy> 
         </sp:TransportBinding> 
        </wsp:All> 
       </wsp:ExactlyOne> 
     </wsp:Policy> 
     <wsp:Policy wsu:Id="IF_IF_ZTST_MASS_CREATE_USER_gut"> 
      <sapsession:Session xmlns:sapsession="http://www.sap.com/webas/630/soap/features/session/"> 
      <sapsession:enableSession>false</sapsession:enableSession> 
      </sapsession:Session> 
       <sapcentraladmin:CentralAdministration xmlns:sapcentraladmin="http://www.sap.com/webas/700/soap/features/CentralAdministration/" wsp:Optional="true"># 
      <sapcentraladmin:BusinessApplicationID>3EB6184C46517B16E1000000AC10FEEB</sapcentraladmin:BusinessApplicationID> 
      </sapcentraladmin:CentralAdministration> 
     </wsp:Policy> 
     <wsp:Policy wsu:Id="OP_IF_OP_ZTST_MASS_USER_CREATE"> 
      <sapcomhnd:enableCommit xmlns:sapcomhnd="http://www.sap.com/NW05/soap/features/commit/">false</sapcomhnd:enableCommit> 
      <sapblock:enableBlocking xmlns:sapblock="http://www.sap.com/NW05/soap/features/blocking/">true</sapblock:enableBlocking> 
      <saptrhnw05:required xmlns:saptrhnw05="http://www.sap.com/NW05/soap/features/transaction/">no</saptrhnw05:required> 
      <saprmnw05:enableWSRM xmlns:saprmnw05="http://www.sap.com/NW05/soap/features/wsrm/">false</saprmnw05:enableWSRM> 
     </wsp:Policy> 
     <wsdl:types> 
     <xsd:schema attributeFormDefault="qualified" targetNamespace="urn:sap-com:document:sap:rfc:functions"> 
       <xsd:simpleType name="char12"> 
        <xsd:restriction base="xsd:string"> 
        <xsd:maxLength value="12"/> 
        </xsd:restriction> 
       </xsd:simpleType> 
       <xsd:simpleType name="char40"> 
        <xsd:restriction base="xsd:string"> 
        <xsd:maxLength value="40"/> 
        </xsd:restriction> 
       </xsd:simpleType> 
       <xsd:complexType name="ZTST_USER_PWD"> 
        <xsd:sequence> 
         <xsd:element name="USER" type="tns:char12"/> 
         <xsd:element name="PWD" type="tns:char40"/> 
        </xsd:sequence> 
       </xsd:complexType> 
      <xsd:complexType name="ZTST_USER_PWD_TAB"> 
       <xsd:sequence> 
        <xsd:element name="item" type="tns:ZTST_USER_PWD" minOccurs="0" maxOccurs="unbounded"/> 
       </xsd:sequence> 
      </xsd:complexType> 
     <xsd:element name="ZTST_MASS_USER_CREATE"> 
      <xsd:complexType> 
       <xsd:sequence> 
        <xsd:element name="USER" type="tns:ZTST_USER_PWD_TAB"/> 
       </xsd:sequence> 
      </xsd:complexType> 
     </xsd:element> 
     <xsd:element name="ZTST_MASS_USER_CREATEResponse"> 
      <xsd:complexType> 
       <xsd:sequence> 
        <xsd:element name="OUT_TAB" type="tns:ZTST_USER_PWD_TAB"/> 
       </xsd:sequence> 
      </xsd:complexType> 
     </xsd:element> 
     </xsd:schema> 
    </wsdl:types> 
     <wsdl:message name="ZTST_MASS_USER_CREATE"> 
      <wsdl:part name="parameters" element="tns:ZTST_MASS_USER_CREATE"/> 
     </wsdl:message> 
     <wsdl:message name="ZTST_MASS_USER_CREATEResponse"> 
      <wsdl:part name="parameters" element="tns:ZTST_MASS_USER_CREATEResponse"/> 
     </wsdl:message> 
     <wsdl:portType name="ZTST_MASS_CREATE_USER_gut"> 
      <wsp:Policy> 
       <wsp:PolicyReference URI="#IF_IF_ZTST_MASS_CREATE_USER_gut"/> 
      </wsp:Policy> 
     <wsdl:operation name="ZTST_MASS_USER_CREATE"> 
      <wsp:Policy> 
       <wsp:PolicyReference URI="#OP_IF_OP_ZTST_MASS_USER_CREATE"/> 
      </wsp:Policy> 
      <wsdl:input message="tns:ZTST_MASS_USER_CREATE"/> 
      <wsdl:output message="tns:ZTST_MASS_USER_CREATEResponse"/> 
     </wsdl:operation> 
    </wsdl:portType> 
     <wsdl:binding name="ZTST_MASS_CREATE_USER" type="tns:ZTST_MASS_CREATE_USER_gut"> 
      <wsp:Policy> 
       <wsp:PolicyReference URI="#BN_BN_ZTST_MASS_CREATE_USER"/> 
      </wsp:Policy> 
      <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> 
      <wsdl:operation name="ZTST_MASS_USER_CREATE"> 
       <soap:operation soapAction="" style="document"/> 
       <wsdl:input> 
        <soap:body use="literal"/> 
       </wsdl:input> 
       <wsdl:output> 
        <soap:body use="literal"/> 
       </wsdl:output> 
       </wsdl:operation> 
     </wsdl:binding> 
      <wsdl:service name="ZTST_MASS_CREATE_USER"> 
       <wsdl:port name="ZTST_MASS_CREATE_USER" binding="tns:ZTST_MASS_CREATE_USER"> 
        <soap:address location="http://something"/> 
       </wsdl:port> 
      </wsdl:service> 
     </wsdl:definitions> 

更新:

這是我appl.config

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
     <bindings> 
      <customBinding> 
       <binding name="ZTST_MASS_CREATE_USER"> 
        <httpTransport authenticationScheme="Basic" /> 
       </binding> 
      </customBinding> 
     </bindings> 
     <client>   
      <endpoint address="http://somehost/relativeblabla" 
       binding="customBinding" bindingConfiguration="ZTST_MASS_CREATE_USER" 
       contract="ServiceReference2.ZTST_MASS_CREATE_USER_gut" name="ZTST_MASS_CREATE_USER" /> 
     </client> 
    </system.serviceModel> 
</configuration> 

回答

1

愚蠢的我!如果您選擇添加服務引用(而不是Web服務引用),則需要使用WEB SERVICE REFERENCE時會發生上述錯誤。對於添加webREferenz到您的項目:

First Step

Second Step

Third Step