1

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);
    }

And here is my service instance

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);
        }
    }

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

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
    }

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);
        }
    }

and finally, the response object.

[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; }
    }

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.

Josh
  • 16,286
  • 25
  • 113
  • 158
  • As I'm looking at this, I'm wondering if it is the request or the response that is the problem. I have message tracing enabled, but no log is being generated, which lead me to the assumption that it was the request that was the problem instead of the response. – Josh Oct 14 '10 at 14:35
  • On a side note, RulesException and all classes within are serializable, although it too has an IEnumberable collection as one of it's properties. – Josh Oct 14 '10 at 14:37
  • I just ruled out the Response by commenting out Results and Exception. It still returned a 400 error. – Josh Oct 14 '10 at 14:42

6 Answers6

2

I had the same issue, apparently it was caused by a trailing forward slash behind the .svc in the URL. Don't place the slash there .. and it worked.

Wotuu
  • 828
  • 1
  • 8
  • 18
2

I ran into the same issue, but caused by a different problem.

I implemented my own service host for the WCF REST service and accidentally derived my class from System.ServiceModel.ServiceHost instead of System.ServiceModel.Web.WebServiceHost

I could activate the service by browsing to the root URL, but when I tried to access one of the exposed resources, I would get a HTTP 400 error. Hooking up a debugger showed the exception mentioned above.

Hope this helps somebody someday.

MvdD
  • 22,082
  • 8
  • 65
  • 93
2

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

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

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

open your visual studio command window

Then you have to run aspnet_regiis -i

And you need to run servicemodelreg -ia

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.

Josh
  • 16,286
  • 25
  • 113
  • 158
1

We had the same problem many times, please host on IIS, its way much better with respect to performance and scalability.

When there is a problem in WCF service, and getting fall or too much busy. this problems occurs sometime.

Regards,

Mazhar Karimi

1

Same error messaged was caused by endpoint binding set to basicHttpBinding in App.config (even though System.ServiceModel.Web.WebServiceHost was used as @MvdD's answer):

<endpoint address="" binding="basicHttpBinding" contract="MySite.Common.Services.Web.IRDCService">

the correct binding value for WebServiceHost is webHttpBinding (possibly ws as well)

<endpoint address="" binding="webHttpBinding" contract="MySite.Common.Services.Web.IRDCService">

please note, contrary to basicHttpBinding, there does not seem to be corresponding webHttpsBinding counterpart for https, security mode transport should be used instead.

wondra
  • 3,271
  • 3
  • 29
  • 48
0

So just ran into this exact error after deploying a service to IIS 10 on Windows Server 2016 and trying to navigate to the service screen.

After fighting with it for awhile, I went to add multiple endpoints with different bindings, thinking it might not like the ws2007FederationHttpBinding I was using.

Here's the original endpoint configuration:

<services>
  <service name="Vendor.Services.Provider.Host.Implementation.ProviderService">
    <endpoint
      address="https://svc01dev/Services/Provider/4/0/ProviderService.svc"
      binding="ws2007FederationHttpBinding" 
      bindingConfiguration="Vendor.Services.Provider.Contracts.ServiceContracts.IProviderService_ws2007FederationHttpBinding" 
      contract="Vendor.Services.Provider.Contracts.ServiceContracts.IProviderService"/>
  </service>
</services>

I deleted the address from the endpoint, getting ready to try a multi-endpoint service:

<services>
  <service name="Vendor.Services.Provider.Host.Implementation.ProviderService">
    <endpoint
      binding="ws2007FederationHttpBinding" 
      bindingConfiguration="Vendor.Services.Provider.Contracts.ServiceContracts.IProviderService_ws2007FederationHttpBinding" 
      contract="Vendor.Services.Provider.Contracts.ServiceContracts.IProviderService"/>
  </service>
</services>

Just to see what would happen, I navigated to the service screen again, and it popped up. No more error. I'm going to look into this some more, but if anyone knows, I'd sure like to hear it.