skip to Main Content

The code below works great in a console application.

When it runs on IIS in a web application it times out on the client.GetAync(URL) line.

Any ideas?

       static async Task<byte[]> GetLogo(string URL, string AccessToken)
   {
       byte[] imageData = null;

       using (HttpClient client = new HttpClient())
       {
           client.DefaultRequestHeaders.Authorization = new   AuthenticationHeaderValue("Authorization", "Bearer " + AccessToken);
           HttpResponseMessage response = await client.GetAsync(URL);
           if (response.IsSuccessStatusCode)
           {
               imageData = await response.Content.ReadAsByteArrayAsync();

           }

       }

       return imageData;
   }

Thanks,

2

Answers


  1. Search for "Event Viewer" in Windows, then navigate to the Windows Logs folder, and finally open the Application section. In this page, you can view the logs of IIS errors.

    You can also log errors through the Web.config file.
    Below, I’m providing you with a sample Web.config file. Note that this Web.config file is based on .NET 8 Also, you need to replace ‘YourAppName’ with the name of your application.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <location path="." inheritInChildApplications="false">
        <system.webServer>
          <handlers>
            <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
          </handlers>
          <aspNetCore processPath="dotnet" arguments=".YourAppName.dll" stdoutLogEnabled="true" stdoutLogFile=".logsstdout" hostingModel="inprocess" />
        </system.webServer>
      </location>
    </configuration>
    

    Also, please note that the "logsstdout" folders should exist alongside the Web.Config file, and proper access should be granted to them.

    Login or Signup to reply.
  2. It depends on the URL you are trying to access. If you are trying to access a url that is being served by IIS, then you cant access the fully qualified url without adding the URL to the local host file and set the IP to 192.162.0.1

    Also, you are using should be injecting the HttpClient in the constructor, something like:

    private readonly HttpClient _httpClient;

    public Service(HttpClient httpClient)
    {
        _httpClient = httpClient;
    }
    

    then in your method use something like, which is using async:

    using (var request = new HttpRequestMessage(HttpMethod.Get, URL))
            {
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);
    
                using (HttpResponseMessage response = await _httpClient.SendAsync(request))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        var imageData = await response.Content.ReadAsByteArrayAsync();
                    }
                    else
                    {
                        // Handle unsuccessful response
                    }
                }
            }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search