我有一个WCF服务,具有以下配置:
在WcfSvcHost.exe进程中托管服务时,如果我浏览到URL:
HTTP://本地主机/为MyService /合同
服务元数据可用的地方我收到HTTP 400 Bad Request错误.
通过检查WCF日志,我发现抛出了System.Xml.XmlException异常,并显示消息:" 无法读取消息正文,因为它是空的. "
以下是日志文件的摘录:
System.ServiceModel.ProtocolException, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 There is a problem with the XML that was received from the network. See inner exception for more details. at System.ServiceModel.Channels.HttpRequestContext.CreateMessage() at System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext context, ItemDequeuedCallback callback) at System.ServiceModel.Channels.SharedHttpTransportManager.OnGetContextCore(IAsyncResult result) at System.ServiceModel.Channels.SharedHttpTransportManager.OnGetContext(IAsyncResult result) at System.ServiceModel.Diagnostics.Utility.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result) at System.Net.LazyAsyncResult.Complete(IntPtr userToken) at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken) at System.Net.ListenerAsyncResult.WaitCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped) at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP) System.Xml.XmlException, System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 The body of the message cannot be read because it is empty. at System.ServiceModel.Channels.HttpRequestContext.CreateMessage() at System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext context, ItemDequeuedCallback callback) at System.ServiceModel.Channels.SharedHttpTransportManager.OnGetContextCore(IAsyncResult result) at System.ServiceModel.Channels.SharedHttpTransportManager.OnGetContext(IAsyncResult result) at System.ServiceModel.Diagnostics.Utility.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result) at System.Net.LazyAsyncResult.Complete(IntPtr userToken) at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken) at System.Net.ListenerAsyncResult.WaitCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped) at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
如果我改为浏览到URL:
HTTP://本地主机/ WSDL为MyService
一切正常,我得到了WSDL合同.此时,我还可以完全删除"MetadataHttp"元数据端点,它不会有任何区别.
我正在使用.NET 3.5 SP1.有没有人知道这里可能出现什么问题?
我想我发现了问题所在.
如果我浏览到URL:
HTTP://本地主机/为MyService /合同
使用WcfTestClient应用程序,我可以成功检索服务元数据.
因此,只有当我通过Web浏览器请求URL时才会出现错误.
在HTTP错误的请求错误来自于一个事实,即浏览器发出HTTP GET请求,其中该消息的内容是在HTTP报头,身体是空的.
这正是WCF mexHttpBinding所抱怨的!
要通过Web浏览器获取服务合同,您必须在服务行为中明确启用它:
然后,请求的URL成为:
HTTP://本地主机/ WSDL为MyService
所以,事实证明我发布这个问题有点太快了.但是,我会保留它只是为了记录.