问题描述
如何配置WCF客户端以使其与托管在不返回内容类型的服务器上的第三方WS服务一起工作?
How to configure WCF client to work with third party WS service hosted on the server that doesn't return content-type in the reply?
问题是这样的配置为使用basicHttpBinding的WCF客户端会引发异常: content-type is required ...
Problem is that such WCF client configured to use basicHttpBinding throw an exception: "content-type is required"...
我应该使用自定义绑定还是拒绝WCF?
Should I use custom bindings or refuse WCF?
P.S。 .NET 3.5
P.S. .NET 3.5
PPS
消息:SOAP消息传递需要HTTP Content-Type标头,但没有找到
服务器堆栈跟踪:
在System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest请求,HttpWebResponse响应,HttpChannelFactory工厂,WebException responseException)
P.P.SMessage: An HTTP Content-Type header is required for SOAP messaging and none was foundServer stack trace: at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory factory, WebException responseException)
在System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel .HttpChannelRequest.WaitForReply(TimeSpan超时)
在System.ServiceModel.Channels.RequestChannel.Request(消息,TimeSpan超时)
在System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息,TimeSpan超时)
在System.ServiceModel.Channels.ServiceChannel.Call(字符串操作,布尔型单向,ProxyOperationRuntime操作,Object [] ins,Object [] out,TimeSpan超时)在System.ServiceModel.Channels.ServiceChannel中。 Call(String action,Boolean oneway,ProxyOperationRuntime operation,Object [] in s,Object [] outs)
在System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime operation)
在System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage消息)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout) at System.ServiceModel.Dispatcher.RequestChannelBinder.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.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
在[0]处抛出异常:System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg)在System.Runtime处的
。 Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData,Int32类型)blablabla上的
(自定义代码)
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 blablabla (custom code)
推荐答案
我觉得我找到了该方案砍死了
I feel I found the scheme how it can be hacked.
从
继承新类System.ServiceModel.Channels.HttpChannelFactory + HttpRequestChannel + HttpChannelRequest
并重写WaitForReply虚拟方法
Inherit new class fromSystem.ServiceModel.Channels.HttpChannelFactory+HttpRequestChannel+HttpChannelRequestand override WaitForReply virtual method
从
System.ServiceModel.Channels.HttpChannelFactory + HttpRequestChannel
继承新类,并重写CreateRequest(Message)接口方法
Inherit new class fromSystem.ServiceModel.Channels.HttpChannelFactory+HttpRequestChanneland override CreateRequest(Message) interface method
从新类继承
System.ServiceModel.Channels.HttpChannelFactory
并重写OnCreateChannel(EndpointAddress remoteAddress,Uri via)方法
Inherit new class formSystem.ServiceModel.Channels.HttpChannelFactoryand override OnCreateChannel(EndpointAddress remoteAddress, Uri via) method
问题:全部这些类是私有的和内部的(!)。解决方案,请使用以下命令在运行时创建它们:Reflection / emit / TypeBuilder / codedom。所有提到的方法都很简短(只有WaitForReply是冗长的),所有方法都是虚拟的-我们真的很幸运。
problem: all those classes are private and internal (!). solution, create them on run-time using: reflection/emit/TypeBuilder/codedom. All mentioned methods are short (only WaitForReply is verbose) and all methods are virtual - there we are really lucky.
然后从Http(s)TransportBindingElement
继承并覆盖BuildChannelFactory(BindingContext context);
then inherit from Http(s)TransportBindingElementand override BuildChannelFactory(BindingContext context);
然后创建自定义绑定
:)
PS我不确定是否可以创建从内部受保护的类,其他程序集继承的新类型,但我想这是可能的。
P.S. I'm not sure is it possible to create new type inheriting it from internal, protected class, from other assembly, but I guess it is possible.
这篇关于如何配置WCF客户端以使其与托管在不返回内容类型的服务器上的第三方WS服务一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!