我在使用sap webservice时遇到了一些麻烦。我已经通过了许多教程以及msdn上的许多线程,但我无法找到解决问题的方法:
运行我的程序最终会出现此错误:
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服务的请求发送了两次:一次,一次没有验证。
它不适合我,因为
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>
答案 0 :(得分:1)
愚蠢的我!如果您在需要WEB服务参考时选择添加服务引用而不是Web服务引用,则会发生上述错误。用于将webREferenz添加到项目中: