请求WCF服务合同时出现HTTP错误请求错误

Enr*_*lio 12 wcf wsdl exception http

我有一个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>
Run Code Online (Sandbox Code Playgroud)

WcfSvcHost.exe进程中托管服务时,如果我浏览到URL:

HTTP://本地主机/为MyService /合同

服务元数据可用的地方我收到HTTP 400 Bad Request错误.

通过检查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>
Run Code Online (Sandbox Code Playgroud)

如果我改为浏览到URL:

HTTP://本地主机/ WSDL为MyService

一切正常,我得到了WSDL合同.此时,我还可以完全删除"MetadataHttp"元数据端点,它不会有任何区别.

我正在使用.NET 3.5 SP1.有没有人知道这里可能出现什么问题?

Enr*_*lio 12

我想我发现了问题所在.

如果我浏览到URL:

HTTP://本地主机/为MyService /合同

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

HTTP错误的请求错误来自于一个事实,即浏览器发出HTTP GET请求,其中该消息的内容是在HTTP报头,身体是空的.
这正是WCF mexHttpBinding所抱怨的!

要通过Web浏览器获取服务合同,您必须在服务行为中明确启用它:

<serviceBehaviors>
    <behavior name="MetadataEnabled">
        <serviceMetadata httpGetEnabled="true" />
    </behavior>
</serviceBehaviors>
Run Code Online (Sandbox Code Playgroud)

然后,请求的URL成为:

HTTP://本地主机/ WSDL为MyService

所以,事实证明我发布这个问题有点太快了.但是,我会保留它只是为了记录.


Lut*_*uty 5

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

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

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