Last post Jun 15, 2018 12:49 PM by k000der
Jun 15, 2018 10:21 AM|k000der|LINK
So this is pretty much the error I get:
An unhandled exception has occurred while executing the request. System.InvalidOperationException: This instance has already started one or more requests. Properties can only be modified before sending the first request.
Here is what happens:
I made a Rest call to a POST method, by using a HttpClient object (by setting it's properties, like BaseAddress). Inside the POST method, I'm also calling another Rest method (GET). This method also includes a reference to
client instance (normally you would call the GET method directly, but in this case, I also call it internally from the POST method).
I have defined the HttpClient definition at the top of my controller, like
private static HttpClient client = new HttpClient();
But whenever I call the second Rest method (GET) with the client instance, I get the error just mentioned.
I could just create a new instance of the HttpClient object inside the GET method, but I don't want to do that. Instead, I would like to reuse the one
client instance I have defined at the top of the controller, throughout the code.
I also know that the GET sets another BaseAddress property (POST and GET have their own Rest URLs they refer to), so this is probably where the error comes from. Yet, I'm unable to find a solution to this.
Jun 15, 2018 11:26 AM|PatriceSc|LINK
And it happens when changing the base address? The message seems to tell it should be configured once before the first call. So I would configure the HttpClient base address with the portion that never needs to be changed and I would pass the rest of the
addres with my Get or Post call. For now it seems you are trying to change the base address to the full address service before each call ?
Jun 15, 2018 12:49 PM|k000der|LINK
Yes, the BaseAddress changes in every call.
However, I changed the BaseAddress to be set once (and then creating the rest of the calling URL dynamically).
Thanks for the tip, it seemed to solve my issue :)