WCF服务返回400错误:邮件正文为空,因此无法读取 [英] WCF Service returning 400 error: The body of the message cannot be read because it is empty

查看:72
本文介绍了WCF服务返回400错误:邮件正文为空,因此无法读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WCF服务,这令人有些头疼.我启用了跟踪,我有一个带有数据协定的对象正在构建并传递,但是我在日志中看到此错误:

I have a WCF service that is causing a bit of a headache. I have tracing enabled, I have an object with a data contract being built and passed in, but I am seeing this error in the log:

<TraceData>
            <DataItem>
                <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Error">
                    <TraceIdentifier>http://msdn.microsoft.com/en-US/library/System.ServiceModel.Diagnostics.ThrowingException.aspx</TraceIdentifier>
                    <Description>Throwing an exception.</Description>
                    <AppDomain>efb0d0d7-1-129315381593520544</AppDomain>
                    <Exception>
                        <ExceptionType>System.ServiceModel.ProtocolException, System.ServiceModel, Version=4.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, Action callback)
                            at System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult result)
                            at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest()
                            at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()
                            at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(Object state)
                            at System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
                            at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
                            at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
                        </StackTrace>
                        <ExceptionString>
                            System.ServiceModel.ProtocolException: There is a problem with the XML that was received from the network. See inner exception for more details. ---&amp;gt; System.Xml.XmlException: The body of the message cannot be read because it is empty.
                            --- End of inner exception stack trace ---
                        </ExceptionString>
                        <InnerException>
                            <ExceptionType>System.Xml.XmlException, System.Xml, Version=4.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, Action callback)
                                at System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult result)
                                at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest()
                                at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()
                                at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(Object state)
                                at System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
                                at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
                                at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
                            </StackTrace>
                            <ExceptionString>System.Xml.XmlException: The body of the message cannot be read because it is empty.</ExceptionString>
                        </InnerException>
                    </Exception>
                </TraceRecord>
            </DataItem>
        </TraceData>

所以,这是我的服务界面:

So, here is my service interface:

[ServiceContract]
    public interface IRDCService
    {
        [OperationContract]
        Response<Customer> GetCustomer(CustomerRequest request);

        [OperationContract]
        Response<Customer> GetSiteCustomers(CustomerRequest request);
    }

这是我的服务实例

public class RDCService : IRDCService
    {
        ICustomerService customerService;

        public RDCService()
        {
            //We have to locate the instance from structuremap manually because web services *REQUIRE* a default constructor
            customerService = ServiceLocator.Locate<ICustomerService>();
        }

        public Response<Customer> GetCustomer(CustomerRequest request)
        {
            return customerService.GetCustomer(request);
        }

        public Response<Customer> GetSiteCustomers(CustomerRequest request)
        {
            return customerService.GetSiteCustomers(request);
        }
    }

Web服务(服务器端)的配置如下:

The configuration for the web service (server side) looks like this:

<system.serviceModel>
        <diagnostics>
            <messageLogging logMalformedMessages="true" logMessagesAtServiceLevel="true"
                logMessagesAtTransportLevel="true" />
        </diagnostics>
        <services>
            <service behaviorConfiguration="MySite.Web.Services.RDCServiceBehavior"
                name="MySite.Web.Services.RDCService">
                <endpoint address="http://localhost:27433" binding="wsHttpBinding"
                    contract="MySite.Common.Services.Web.IRDCService">
                    <identity>
                        <dns value="localhost:27433" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="MySite.Web.Services.RDCServiceBehavior">
                    <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                    <serviceMetadata httpGetEnabled="true"/>
                    <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
                    <serviceDebug includeExceptionDetailInFaults="true"/>


                    <dataContractSerializer maxItemsInObjectGraph="6553600" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>

这是我的请求对象的样子

Here is what my request object looks like

[DataContract]
    public class CustomerRequest : RequestBase
    {
        [DataMember]
        public int Id { get; set; }

        [DataMember]
        public int SiteId { get; set; }
    }

和RequestBase:

And the RequestBase:

[DataContract]
    public abstract class RequestBase : IRequest
    {
        #region IRequest Members

        [DataMember]
        public int PageSize { get; set; }

        [DataMember]
        public int PageIndex { get; set; }

        #endregion
    }

还有我的IRequest界面

And my IRequest interface

public interface IRequest
    {
        int PageSize { get; set; }
        int PageIndex { get; set; }
    }

我在服务调用周围有一个包装器类.这是课程.

And I have a wrapper class around my service calls. Here is the class.

public class MyService : IMyService
    {
        IRDCService service;

        public MyService()
        {
            //service = new MySite.RDCService.RDCServiceClient();
            EndpointAddress address = new EndpointAddress(APISettings.Default.ServiceUrl);
            BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
            binding.TransferMode = TransferMode.Streamed;
            binding.MaxBufferSize = 65536;
            binding.MaxReceivedMessageSize = 4194304;

            ChannelFactory<IRDCService> factory = new ChannelFactory<IRDCService>(binding, address);
            service = factory.CreateChannel();
        }

        public Response<Customer> GetCustomer(CustomerRequest request)
        {
            return service.GetCustomer(request);
        }

        public Response<Customer> GetSiteCustomers(CustomerRequest request)
        {
            return service.GetSiteCustomers(request);
        }
    }

最后是响应对象.

[DataContract]
    public class Response<T>
    {
        [DataMember]
        public IEnumerable<T> Results { get; set; }

        [DataMember]
        public int TotalResults { get; set; }

        [DataMember]
        public int PageIndex { get; set; }

        [DataMember]
        public int PageSize { get; set; }

        [DataMember]
        public RulesException Exception { get; set; }
    }

因此,当我构建CustomerRequest对象并将其传递时,由于某种原因,它会将服务器作为空请求发送到服务器.有什么想法吗?我尝试增加对象图和消息大小.当我调试时,它将停止在包装类中,并显示400错误.我不确定是否存在序列化错误,但是考虑到对象协定是4个整数属性,我无法想象它会导致问题.

So, when I build my CustomerRequest object and pass it in, for some reason it's hitting the server as an empty request. Any ideas why? I've tried upping the object graph and the message size. When I debug it stops in the wrapper class with the 400 error. I'm not sure if there is a serialization error, but considering the object contract is 4 integer properties I can't imagine it causing an issue.

推荐答案

如果遇到此问题,则需要在本地IIS上运行WCF应用程序.

If you run into this problem, you need to run your WCF application on your local IIS.

转到wcf应用程序项目的属性",然后选择使用本地iis Web服务器".

Go to Properties for the wcf application project and select "Use local iis web server".

您需要在添加/删除Windows功能中启用Microsoft .Net Framework 3.1-> Windows Communication Foundation http激活.

And you need to enable Microsoft .Net Framework 3.1 -> Windows communication foundation http activation in add/remove windows features.

打开Visual Studio命令窗口

open your visual studio command window

然后,您必须运行aspnet_regiis -i

Then you have to run aspnet_regiis -i

您需要运行servicemodelreg -ia

And you need to run servicemodelreg -ia

大量运行此命令,出现错误,搜索google,冲洗,然后重复.花了大约一个小时才弄清所有问题.皮塔饼.

It was a lot of run this, get an error, search google, rinse, repeat. It took about an hour to get all figured out. PITA.

这篇关于WCF服务返回400错误:邮件正文为空,因此无法读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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