WCF未识别的消息版本。

4

我正在处理一个问题,我的控制台应用程序尝试通过WCF服务发送数据。我已经成功地通过另一个使用相同配置的WCF服务在我的其他控制台应用程序中发送了另一种类型的数据。

对于我在两个控制台应用程序中使用的配置:

var binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;
binding.MaxReceivedMessageSize = 2147483647;
var endpoint =new System.ServiceModel.EndpointAddress("the end point");

var theClient = new MyClient(binding,endpoint);
theClient .ClientCredentials.UserName.UserName = "Username";
theClient .ClientCredentials.UserName.Password = "Password";

在完成这种配置后,我使用了发送数据的方法。我还尝试通过Postman Chrome扩展程序发送从Fiddler获取的xml数据。我能够通过Postman发送xml,但不能在控制台应用程序中发送。我认为我的配置存在问题。堆栈跟踪:
Unrecognized message version. 
Server stack trace: 
   at System.ServiceModel.Channels.ReceivedMessage.ReadStartEnvelope(XmlDictionaryReader reader)
   at System.ServiceModel.Channels.BufferedMessage..ctor(IBufferedMessageData messageData, RecycledMessageState recycledMessageState, Boolean[] understoodHeaders, Boolean understoodHeadersModified)
   at System.ServiceModel.Channels.TextMessageEncoderFactory.TextMessageEncoder.ReadMessage(ArraySegment`1 buffer, BufferManager bufferManager, String contentType)
   at System.ServiceModel.Channels.HttpInput.DecodeBufferedMessage(ArraySegment`1 buffer, Stream inputStream)
   at System.ServiceModel.Channels.HttpInput.ReadBufferedMessage(Stream inputStream)
   at System.ServiceModel.Channels.HttpInput.ParseIncomingMessage(HttpRequestMessage httpRequestMessage, Exception& requestException)
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at Integrator.WCFSer.Z_BAPI_SLS_ORD_CREATE_WITH_DEL.ZBapiSlsOrdCreateWithDel(ZBapiSlsOrdCreateWithDelRequest request)
   at Integrator.WCFSer.Z_BAPI_SLS_ORD_CREATE_WITH_DELClient.Integrator.WCFSer.Z_BAPI_SLS_ORD_CREATE_WITH_DEL.ZBapiSlsOrdCreateWithDel(ZBapiSlsOrdCreateWithDelRequest request)
   at Integrator.WCFSer.Z_BAPI_SLS_ORD_CREATE_WITH_DELClient.ZBapiSlsOrdCreateWithDel(ZBapiSlsOrdCreateWithDel ZBapiSlsOrdCreateWithDel1)
   at Integrator.Program.Main(String[] args) mscorlibz\Integrator\Program.cs:line 113 mscorlib

有什么建议吗?
1个回答

0

首先,当您使用wsdl文件时,需要检查您的终结点地址。您可以参考this answer

如果您想指定特定的Soap版本,则必须将绑定修改为自定义绑定,如下所示:

var binding = new CustomBinding();

//custom text message and encoding binding configs
TextMessageEncodingBindingElement textBindingElement = new TextMessageEncodingBindingElement
{
    //specify your custom Soap message version below, in this case (Soap 1.1)
    MessageVersion = MessageVersion.CreateVersion(EnvelopeVersion.Soap11, 
    AddressingVersion.WSAddressing10), 
    WriteEncoding = Encoding.UTF8,
    ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max,

};

//custom transport binding including authentication configs
HttpTransportBindingElement httpBindingElement = new HttpTransportBindingElement
{
    AllowCookies = false,
    MaxBufferSize = int.MaxValue,
    MaxReceivedMessageSize = int.MaxValue,
    TransferMode = TransferMode.Buffered,
    UseDefaultWebProxy = false,
    ProxyAuthenticationScheme = AuthenticationSchemes.Basic,
    AuthenticationScheme = AuthenticationSchemes.Basic,

};

binding.Elements.Add(textBindingElement);
binding.Elements.Add(httpBindingElement);

var endpoint = new EndpointAddress("the end point");
var client = new MyClient(binding,endpoint);
client.ClientCredentials.UserName.UserName = "Username";
client.ClientCredentials.UserName.Password = "Password";

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接