1

I'm setting up a new server for web api, but when I try to make a post request from two individual client, server only responds to the first one. From second one, I always get 500 Internal Server Error.

I tried to make all methods in the server as async but same error has occured.

I call the web service as below:

using(var client = new HttpClient())
{
    client.BaseAddress = new Uri("http://serverdomain/ApiName/")
    var response = client.PostAsync("controller/method", 
keyvaluepairContent);
    result = response.Result.Content.ReadAsAsync<List<string>>().Result;
}

And the relevant service code is below:

[Route("controller/method")]
[AcceptVerbs("POST")]
public List<string> Foo([FromBody] someparams)
{
    //some logic
}

I wrote the config file as :

config.Routes.MapHttpRoute(
    name : "DefaultApi",
    routeTemplate : "{controller}/{action}/{id}",
    defaults : new { id = RouteParameter.Optinal }
);

For one client at the same time, the server is working very well. I get what I need. However, when two clients make requests even for different methods, the delayed one always gets 500 Internal Error. Debugger says that this code below cannot parse the result, that is beacuse the response is not a string list but the error above.

result = response.Result.Content.ReadAsAsync<List<string>>().Result;

I think my code is fine, but I need to configure my web api. I did searched about it but no result.

  • 1
    This is not an answer to the question but i noticed you are using `HttpClient` wrong, look at [this](https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/) – JohnChris Jun 18 '19 at 07:09
  • Possible duplicate of [How to design parallel web api in c#?](https://stackoverflow.com/questions/51737057/how-to-design-parallel-web-api-in-c) – JohnChris Jun 18 '19 at 07:28
  • 1
    what kind of exception is thrown in server? – Szymon Tomczyk Jun 18 '19 at 07:36

1 Answers1

0

As mentioned in a comment first, you are using HttpClient wrong please look at this.

As for your WebAPI to be able to respond to multiple calls asynchronously you need to wrap your code in an appropriate method signature like so:

[Route("controller/method")]
[AcceptVerbs("POST")]
public async Task<IHttpActionResult> Foo([FromBody] someparams)
{
    //some logic
}

Some valuable info from another stackoverflow answer here

And another simpler answer here

Like you mentioned in your question you tried making all the methods async but the issue is that you need to use Task

JohnChris
  • 1,360
  • 15
  • 29
  • Wrapping the method in async Task has nothing to do with Internal Server Error returned by the server. It basically just make the incoming thread idle, so that it can process subsequent requests. Default thread pool has 20 threads, so it's not the reason why second request returns Internal Server Error. – Szymon Tomczyk Jun 18 '19 at 07:34
  • true, might be something else, but he is asking how you do it, so I feel like I provided enough information for him to figure it out.. we will see when he responds – JohnChris Jun 18 '19 at 07:56
  • Thank you JohnChris. I applied what you advised but got the same error. Unfortunatelly, cannot see the server logs due to the some business rules, I cannot access the service server. In my PC, remote access is disabled and so, only my client can make a request at the same time to local web service which is perfectly works. I am trying to find a way to get error and can see the logs. Both cannot be done at the same time :( – Ömer Faruk Aktaş Jun 18 '19 at 08:31
  • 1
    For the advise you did for using HttpClient, I think it does not cause this problem, but i am sure it will improve the performans. I will try it as soon as after i found a solution to this. Thank you. :) – Ömer Faruk Aktaş Jun 18 '19 at 08:34