请求WCF服务协定时出现HTTP错误请求错误

请求WCF服务协定时出现HTTP错误请求错误,wcf,http,exception,wsdl,Wcf,Http,Exception,Wsdl,我有一个具有以下配置的WCF服务: <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="MetadataEnabled"> <serviceDebug includeExceptionDetailInFaults="true" /> <

我有一个具有以下配置的WCF服务:

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="MetadataEnabled">
                <serviceDebug includeExceptionDetailInFaults="true" />
                <serviceMetadata httpGetEnabled="true" />
            </behavior>
        </serviceBehaviors>
     </behaviors>
      <services>
          <service behaviorConfiguration="MetadataEnabled" name="MyNamespace.MyService">
              <endpoint name="BasicHttp"
                        address=""
                        binding="basicHttpBinding"
                        contract="MyNamespace.IMyServiceContract" />
              <endpoint name="MetadataHttp"
                        address="contract"
                        binding="mexHttpBinding" 
                        contract="IMetadataExchange" />
              <host>
                  <baseAddresses>
                      <add baseAddress="http://localhost/myservice" />
                  </baseAddresses>
              </host>
          </service>
    </services>
</system.serviceModel>

在服务元数据可用的地方,我收到一个HTTP 400错误请求。

通过检查WCF日志,我发现抛出了一个System.Xml.XmlException异常,其中包含一条消息:“无法读取消息体,因为它是空的。”
以下是日志文件的摘录:

<Exception>
<ExceptionType>
System.ServiceModel.ProtocolException, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
</ExceptionType>
<Message>There is a problem with the XML that was received from the network. See inner exception for more details.</Message>
<StackTrace>
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)
</StackTrace>
<InnerException>
<ExceptionType>System.Xml.XmlException, System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
<Message>The body of the message cannot be read because it is empty.</Message>
<StackTrace>
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)
</StackTrace>
</InnerException>
</Exception>

System.ServiceModel.ProtocolException,System.ServiceModel,版本=3.0.0.0,区域性=中性,PublicKeyToken=b77a5c561934e089
从网络接收的XML有问题。有关详细信息,请参阅内部异常。
在System.ServiceModel.Channels.HttpRequestContext.CreateMessage()中
位于System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext上下文,ItemDequeuedCallback回调)
位于System.ServiceModel.Channels.SharedHttpTransportManager.OnGetContextCore(IAsyncResult结果)
位于System.ServiceModel.Channels.SharedHttpTransportManager.OnGetContext(IAsyncResult结果)
位于System.ServiceModel.Diagnostics.Utility.AsynctRunk.UnhandledExceptionFrame(IAsyncResult结果)
在System.Net.LazyAsyncResult.Complete(IntPtr userToken)中
在System.Net.LazyAsyncResult.ProtectedInvokeCallback(对象结果,IntPtr userToken)
在System.Net.ListenerAsyncResult.WaitCallback(UInt32错误代码,UInt32个字节,NativeOverlapped*NativeOverlapped)
在System.Threading.\u IOCompletionCallback.PerformiCompletionCallback(UInt32错误代码,UInt32个字节,本机重叠*pOVERLAP)
System.Xml.XmlException,System.Xml,版本=2.0.0.0,区域性=neutral,PublicKeyToken=b77a5c561934e089
无法读取邮件正文,因为它是空的。
在System.ServiceModel.Channels.HttpRequestContext.CreateMessage()中
位于System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext上下文,ItemDequeuedCallback回调)
位于System.ServiceModel.Channels.SharedHttpTransportManager.OnGetContextCore(IAsyncResult结果)
位于System.ServiceModel.Channels.SharedHttpTransportManager.OnGetContext(IAsyncResult结果)
位于System.ServiceModel.Diagnostics.Utility.AsynctRunk.UnhandledExceptionFrame(IAsyncResult结果)
在System.Net.LazyAsyncResult.Complete(IntPtr userToken)中
在System.Net.LazyAsyncResult.ProtectedInvokeCallback(对象结果,IntPtr userToken)
在System.Net.ListenerAsyncResult.WaitCallback(UInt32错误代码,UInt32个字节,NativeOverlapped*NativeOverlapped)
在System.Threading.\u IOCompletionCallback.PerformiCompletionCallback(UInt32错误代码,UInt32个字节,本机重叠*pOVERLAP)
如果我改为浏览到URL:

一切正常,我得到了WSDL合同。在这一点上,我还可以完全删除“MetadataHttp”元数据端点,这不会有任何区别


我使用的是.NET3.5SP1。有人知道这里可能出了什么问题吗?

我想我发现了问题所在

如果我浏览到URL:

使用WcfTestClient应用程序,我可以成功检索服务元数据。
因此,只有当我通过web浏览器请求URL时,才会发生错误

HTTP错误请求错误源于浏览器发出HTTP GET请求,其中消息内容位于HTTP头中,而正文为空。
这正是WCFmexHttpBinding所抱怨的

为了通过web浏览器访问服务合同,您必须在服务行为中明确启用它:

<serviceBehaviors>
    <behavior name="MetadataEnabled">
        <serviceMetadata httpGetEnabled="true" />
    </behavior>
</serviceBehaviors>

然后,要请求的URL变为:


所以,我在发布这个问题时有点太快了。不过,我还是会保留它以备记录。

我能够通过将我的WCF服务从运行Visual Studio Development Server切换到使用本地IIS Web服务器来修复“400错误请求”问题(右键单击“服务器”下的“项目-->属性-->Web选项卡-->单选按钮”)。我希望这对其他人有所帮助,因为我花了两天时间才弄明白这一点。

一般来说,这是肥皂信封大小的问题。检查绑定配置以更改MaxBufferPoolSize、MaxReceivedMessageSize以允许大量内容。请记住,您必须同时在客户端和服务器端进行更改

另一个问题是MessageEnconding(另一个绑定参数),请确保客户端和服务器端使用相同的编码


最后,检查读卡器配额属性参数。

如果您不确定WCF代码抛出错误的原因,我强烈建议使用MS Service Trace Viewer。这对于识别像这样的通信和传输问题非常有帮助。查看本文了解更多详细信息。

我从SvcUtil使用HTTPS mex URL时遇到HTTP 400问题,尽管
httpsGetEnabled
设置为
true
。错误消息与真正的问题相去甚远,所以我在这里发布,以防其他人遇到同样的问题


我有一个自签名CA证书(TestRootCA),它是服务器证书(localhost)的颁发者。在客户端上,我导入了TestRootCA CER文件,但没有导入CRL(证书吊销列表)。似乎当您使用自签名CA时,您还必须导入CRL,否则服务器身份验证会以奇怪的方式失败,所有这些都不能说明真正的问题。更糟糕的是,失败发生在SSL握手期间,甚至在请求到达您的服务之前,因此您在WCF跟踪日志中不会看到错误。

我在使用多个en时也看到过这种行为