The response message returned by the Response property of this exception should be immediately returned to the client. No further handling of the request message is required.

Topics: Web Api
Dec 1, 2011 at 1:08 PM

This my api code:

        [WebGet(UriTemplate = "{id}")]
        public HttpResponseMessage<Models.Country> Get(int id) {

            var country = _countryRepo.GetSingle(id);

            if (country == null) {
                var response = new HttpResponseMessage();
                response.StatusCode = HttpStatusCode.NotFound;
                response.Content = new StringContent("Country not found");
                throw new HttpResponseException(response);
            }

            var countryResponse = new HttpResponseMessage<Models.Country>(country);
            countryResponse.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddHours(6));
            return countryResponse;
        }

As you see, if the country is null, I throw an 404 exception. But When I throw this I get this error message:

Microsoft.ApplicationServer.Http.Dispatcher.HttpResponseException: The response message returned by the Response property of this exception should be immediately returned to the client. No further handling of the request message is required.

The end user see 500 internal error instead of 404. What am I doing wrong here?

Coordinator
Dec 1, 2011 at 11:59 PM

Could you please show me the stack trace?

Daniel Roth

Dec 3, 2011 at 11:04 AM

@DanielRoth

Microsoft.ApplicationServer.Http.Dispatcher.HttpResponseException: The response message returned by the Response property of this exception should be immediately returned to the client. No further handling of the request message is required.

Generated: Thu, 01 Dec 2011 13:04:45 GMT

 
Microsoft.ApplicationServer.Http.Dispatcher.HttpResponseException: The response message returned by the Response property of this exception should be immediately returned to the client.  No further handling of the request message is required.
   at API.Accommodation.CountryApi.Get(Int32 id) in d:\Apps\API\src\API\Accommodation\CountryApi.cs:line 44
   at SyncInvokeGet(Object , Object[] , Object[] )
   at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
Coordinator
Dec 5, 2011 at 7:43 PM

Looks like this exception is just the HttpResponseException that is getting thrown by the operation.

I’m not sure why you are seeing a 500 instead of a 404. I tried to reproduce the 500 issue with the code below using Preview 6, but I get back the expected 404:

namespace WebApplication14

{

public class Country { }

public class TestApi

{

[WebGet(UriTemplate = "{id}")]

public HttpResponseMessage<Country> Get(int id)

{

Country country = null;

if (country == null)

{

var response = new HttpResponseMessage();

response.StatusCode = HttpStatusCode.NotFound;

response.Content = new StringContent("Country not found");

throw new HttpResponseException(response);

}

var countryResponse = new HttpResponseMessage<Country>(country);

countryResponse.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddHours(6));

return countryResponse;

}

}

public class Global : System.Web.HttpApplication

{

protected void Application_Start(object sender, EventArgs e)

{

RouteTable.Routes.MapServiceRoute<TestApi>("test");

}

}

}

Response HTTP/1.1 404 Not Found

Daniel Roth

Dec 6, 2011 at 8:18 PM

I am facing the same exact problem here.

When throwing the not found exception, it's like the code continues to run and then the code tries to return the object. My code is even simpler than this one:

[WebGet(UriTemplate = "{id}")]
public LoteCarga Obter(int id)
{
	var lote = _repo.Obter(id);

	if (lote == null)
		throw new HttpResponseException(HttpStatusCode.NotFound);

	return lote;
}

Any pointers?

Thanks!

Coordinator
Dec 6, 2011 at 9:40 PM

Do you see a response getting sent? What error code do you see in the response?

Daniel Roth

Dec 6, 2011 at 11:03 PM

Daniel,

The response is not being sent and it ends with an 500 error due to the same exception pointed earlier in the topic.

Coordinator
Dec 7, 2011 at 4:40 AM

Can you confirm that you are seeing this behavior on Preview 6?

Daniel Roth

Dec 7, 2011 at 1:13 PM

Yes, this behavior is confirmed on Preview 6.

Dec 9, 2011 at 4:35 PM

I've been having the same issue. Instead, I've been using something like:

 

[WebGet(UriTemplate = "{id}")]
public HttpResponseMessage<Models.Country> Get(int id)
{
    var country = _countryRepo.GetSingle(id);
	
    if (country == null)
        return new HttpResponseMessage<Models.Country>(HttpStatusCode.NotFound);

    return new HttpResponseMessage<Models.Country>(country);
}

 

But I can't set the response.Content to a string with this method, it just returns null for json and a nil Country tag for xml.

Coordinator
Dec 9, 2011 at 5:27 PM

Please open an issue in the Issue Tracker and attach a simple project that reproduces the issue and we will take a look.

Daniel Roth