gpt4 book ai didi

wcf - 请求 WCF 服务契约(Contract)时出现 HTTP Bad Request 错误

转载 作者:可可西里 更新时间:2023-11-01 15:09:03 26 4
gpt4 key购买 nike

我有一个具有以下配置的 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>

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

http://localhost/myservice/contract

在服务元数据可用的地方,我收到了 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>

如果我改为浏览 URL:

http://localhost/myservice?wsdl

一切正常,我得到了 WSDL 契约(Contract)。此时,我还可以完全删除 “MetadataHttp” 元数据端点,这不会有任何区别。

我使用的是 .NET 3.5 SP1。有没有人知道这里可能出了什么问题?

最佳答案

我想我找到问题所在了。

如果我浏览到 URL:

http://localhost/myservice/contract

使用 WcfTestClient 应用程序,我可以成功检索服务元数据。
所以错误实际上只发生在我通过网络浏览器请求 URL 时。

HTTP Bad Request 错误源于浏览器发出 HTTP GET 请求,其中消息的内容在 HTTP header 中,而正文为空。
这这正是 WCF mexHttpBinding 所提示的!

为了通过 Web 浏览器访问服务契约(Contract),您必须在服务行为中明确启用它:

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

请求的 URL 变为:

http://localhost/myservice?wsdl

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

关于wcf - 请求 WCF 服务契约(Contract)时出现 HTTP Bad Request 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/323551/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com