请求WCF服务合同时出现HTTP错误请求错误 [英] HTTP Bad Request error when requesting a WCF service contract

查看:294
本文介绍了请求WCF服务合同时出现HTTP错误请求错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < system.serviceModel> 
<行为>
< serviceBehaviors>
< behavior name =MetadataEnabled>
< serviceDebug includeExceptionDetailInFaults =true/>
< serviceMetadata httpGetEnabled =true/>
< / behavior>
< / serviceBehaviors>
< / behavior>
< services>
< service behaviorConfiguration =MetadataEnabledname =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>

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


http:/ / localhost / myservice / contract


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


通过检查WCF日志,我发现使用消息抛出了一个 System.Xml.XmlException 异常:消息的正文不能被读取,因为它是空的。
这是日志文件的提取:

 <例外> 
< ExceptionType>
System.ServiceModel.ProtocolException,System.ServiceModel,Version = 3.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089
< / ExceptionType>
< Message>从网络接收到的XML存在问题。有关详细信息,请参阅内部异常。< / Message>
< StackTrace>
在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.AsyncThunk.UnhandledExceptionFrame(IAsyncResult结果)
在系统。 Net.LazyAsyncResult.Complete(IntPtr userToken)
在System.Net.LazyAsyncResult.ProtectedInvokeCallback(对象结果,IntPtr userToken)
在System.Net.ListenerAsyncResult.WaitCallback(UInt32 errorCode,UInt32 numBytes,NativeOverlapped * nativeOverlapped )
在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>消息的正文不能被读取,因为它是空的。< / Message>
< StackTrace>
在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.AsyncThunk.UnhandledExceptionFrame(IAsyncResult结果)
在系统。 Net.LazyAsyncResult.Complete(IntPtr userToken)
在System.Net.LazyAsyncResult.ProtectedInvokeCallback(对象结果,IntPtr userToken)
在System.Net.ListenerAsyncResult.WaitCallback(UInt32 errorCode,UInt32 numBytes,NativeOverlapped * nativeOverlapped )
在System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode,UInt32 numBytes,NativeOverlapped * pOVERLAP)
< / StackTrace>
< / InnerException>
< / Exception>

如果我改为浏览网址:



< blockquote>

http:// localhost / myservice?wsdl


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



使用.NET 3.5 SP1。有没有人知道这里有什么错误?

解决方案

我想我发现问题是什么。 >

如果我浏览到URL:


http:// localhost / myservice / contract


WcfTestClient 应用程序我可以成功地检索服务元数据。$
所以错误只有当我通过网络浏览器请求URL时才会发生。



HTTP错误请求错误来自于浏览器发出HTTP GET请求,其中消息的内容在HTTP头中,并且正文为空。 />这正是WCF mexHttpBinding 正在抱怨的原因!_
为了通过网络浏览器获得服务合同,您必须明确地启用它在服务行为中:

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

请求的URL将变为:


http:// localhost / myservice?wsdl


所以,原来我发布这个问题有点太快了。不过,我会保留这个记录。


I have a WCF service with the following configuration:

<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>

When hosting the service in the WcfSvcHost.exe process, if I browse to the URL:

http://localhost/myservice/contract

where the service metadata is available I get an HTTP 400 Bad Request error.

By inspecting the WCF logs I found out that an System.Xml.XmlException exception is being thrown with the message: "The body of the message cannot be read because it is empty."
Here is an extract of the log file:

<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>

If I instead browse to the URL:

http://localhost/myservice?wsdl

everything works just fine and I get the WSDL contract. At this point, I can also remove the "MetadataHttp" metadata endpoint completely, and it wouldn't make any difference.

I'm using .NET 3.5 SP1. Does anyone have an idea of what could be wrong here?

解决方案

I think I found out what the problem is.

If I browse to the URL:

http://localhost/myservice/contract

with the WcfTestClient application I can successfully retrieve the service metadata.
So the error really only occurs when I request the URL through a web browser.

The HTTP Bad Request error comes from the fact that the browser issues an HTTP GET request where the contents of the message are in the HTTP headers, and the body is empty.
This is exactly what the WCF mexHttpBinding is complaining about!

In order to get to the service contract via a web browser you will have to explicitly enable it in the service behavior:

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

The URL to request becomes then:

http://localhost/myservice?wsdl

So, it turns out I was a little too quick in posting this question. However, I'll keep it anyway just for the record.

这篇关于请求WCF服务合同时出现HTTP错误请求错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆