0

I have created two Web services: the first one is an ASP .Net Core for a RESTfull HTTP service; the second one is an ASP .Net Core with MVC. The first one has APIs which store a few data in the database. The second one has Views that call the first service's APIs.

Here is the code in the second service which calls the first service:

    public async Task<IEnumerable<T>> GetAllAsync(string url)
    {
        var request = new HttpRequestMessage(HttpMethod.Get, url);

        var client = _httpClient.CreateClient();

        HttpResponseMessage response = await client.SendAsync(request);
        if (response.StatusCode == System.Net.HttpStatusCode.OK)
        {
            var jsonString = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<IEnumerable<T>>(jsonString);
        }

        return null;
    }

Here are a few things I tried:

a. Calling the first service from Postman is fine.

b. Calling other websites such as https://www.google.com in the same code works fine.

c. The Port is open.

d. I have also tried turning off Windows Firewall.

e. Have not installed any Anti-Virus or Firewall.

f. Both the services are running in IIS Express.

g. Tried different browsers to launch the services.

h. The URL is correct. No typo or any other mistakes.

Could anyone please tell me what's the problem here? I can call the APIs using the Postman. but, when I call this in the code, I will get BadGateway (reason says cannotConnect). There is no exception.

Thanks,

Regards,

Bharath

3 Answers3

0

Allow CORS fro caller API's URL into source APIs

SIbghat
  • 281
  • 3
  • 5
0

It may be caused by connection timeout when request the data from mvc, it takes a process request longer than 2 minutes to complete. You can increase the timeout in iis.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <system.webServer>
  <handlers>
    <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" 
       resourceType="Unspecified"/>
  </handlers>
  <aspNetCore requestTimeout="00:20:00"  processPath="%LAUNCHER_PATH%" 
      arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" 
     stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
 </system.webServer>
</configuration>
Karney.
  • 4,803
  • 2
  • 7
  • 11
0

I solved the problem by adding

.UseKestrel(o => { o.Limits.KeepAliveTimeout =TimeSpan.FromMinutes(10);})

in program.cs

Then add requestTimeout="00:20:00" in web.config