The HttpClient instance already started one or more requests.

Topics: Web Api
Nov 17, 2011 at 4:26 PM
Edited Nov 17, 2011 at 5:46 PM

Can anyone explain to me why I am getting an exception with the message, "The HttpClient instance already started one or more requests" when I try to call a second API?

The steps to reproduce are:

1. Start the service (hosted in IIS so all APIs are created using MapServiceRoute in the Global.asax)

2. Make a request to any service method. (This works fine.)

3. Make a request to a service method on another API. I get the exception.

Any ideas why?

BTW, I am calling the services using the TestClient and/or Fiddler. Not sure why HttpClient is even part of the equation when I'm not working with client-side code, but...

Coordinator
Nov 19, 2011 at 12:41 AM

Did you dispose the response message?

Daniel Roth

Nov 21, 2011 at 11:12 PM

Not sure how I can do that.  For one, I only have one service method that returns HttpResponseMessage at this point and secondly, I certainly can't dispose an object that is being returned from the method.

 

Coordinator
Nov 21, 2011 at 11:20 PM

The error is on the client, right? I was referring to disposing of the response message received on the client.

Daniel Roth

Nov 22, 2011 at 1:26 PM

I'm not using a client application.  I am generating all calls from the Test Client (provided by the Web API) or Fiddler. The exception is happening in the server-side code when I attempt to place a call to a second API method.

Coordinator
Nov 23, 2011 at 3:42 AM

That is weird – stack trace?

Daniel Roth

Nov 23, 2011 at 5:50 PM
Edited Nov 23, 2011 at 8:19 PM
System.InvalidOperationException occurred
  Message=The HttpClient instance already started one or more requests. Properties can only be modified before sending the first request.
  Source=Microsoft.Net.Http
  StackTrace:
       at System.Net.Http.DelegatingHandler.CheckDisposedOrStarted()
  InnerException:

Based on the stack trace, it looks like the problem originates in one of the DelegatingHandlers I have registered with the configuration.

So I did a little digging and found that the Func I assigned to the MessageHandlerFactory property of my WebApiConfiguration object is called each time an API is called for the first time (after the application is started).  I am currently using MEF to inject the list of handlers and this list is returned from the Func.  Knowing that MEF treats everything as a singleton by default, I'm wondering if this is the problem.

Is the expected behavior for the MessageHandlerFactory to return new instances of the handler each time it is called?

 

Nov 28, 2011 at 7:32 PM

I did confirm that the exception does not occur when the WebApiConfiguration.MessageHandlerFactory returns new instances of my DelegatingHandlers.

Feb 14, 2012 at 3:58 PM
SonOfPirate wrote:

I did confirm that the exception does not occur when the WebApiConfiguration.MessageHandlerFactory returns new instances of my DelegatingHandlers.

You are a star. I changed my windsor installer to transient lifestyle and it works perfectly now. Many many thanks.