0

I developed a mobile app and an ASP.Net Core Web API for a project we have in our enterprise. When I try to run the app accessing my API, it crashes. In debug, I keep getting this exception (sometimes the number at the end changes, but I don't know what it means):

System.Exception: 'unexpected end of stream on com.android.okhttp.Address@91704093'

A friend recommended me to run the app while running dotnet MyAPI.dll --urls "http://*:myportnumber command on cmd in our API server. Here's the result:

dotnet MyAPI.dll --urls "http://*:myportnumber"
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://[::]:myportnumber
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\IISsites\MyAPIFolder
warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
      Failed to determine the https port for redirect.
info: Microsoft.Hosting.Lifetime[0]
      Application is shutting down...

There is this small warning, which I removed by following this solution, but then the API access on the app began to take forever to load, so I decided to just ignore the warning.

While this command is in execution on the server, the API works like a charm on the app, but when I stop running it, the exception returns or the app crashes.

In order to solve the exception, I have tried to:

  1. (source) Add android:usesCleartextTraffic="true" android:networkSecurityConfig="@xml/network_security_config" to the app's AndroidManifest.xml and create a new xml file in Resources\xml\network_security_config.xml with the following code:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="false">
        <domain includeSubdomains="true">https://ourenterpriseurl.com.br/api</domain>
    </domain-config>
</network-security-config>
  1. (source) Set the HttpClient implementation to Android and the SSL/TLS implementation to Native TLS1.2+ on Project Options > Android Options > Advanced Options;

  2. (source) Edit the CreateHostBuilder method on Program.cs like this:

public static IHostBuilder CreateHostBuilder(string[] args)
{
    var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("hostsettings.json", optional: true)
        .AddCommandLine(args)
        .Build();

    return Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseConfiguration(config)
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseUrls("http://*:myportnumber")
            .UseStartup<Startup>();
        });
}

The content of hostsettings.json is:

{
  "urls": "http://*:myportnumber"
}

So, how do I get rid of this exception in order to make my project work?


ABOUT THE API: it is developed in ASP.Net Core 5.0; accesses stored procedures in our SQL Server database with it's own SQL credentials; it is published on the IIS of an OnPremises server of ours; it becomes publicly available for our mobile app via FortiWeb; the connection string is encrypted using Aes and CryptoStream and the resulting string is written in appsettings.json; the connection string is decrypted in Startup.cs.

18
  • How long does it take for error message to occur? Is it immediate, 30 seconds, or different time? Does it always fail at same time? Does it happen with all data or only some data? I'm guessing but thinking the command to database is taking more than 30 seconds and need to change the command timeout. Commented Aug 30, 2022 at 12:54
  • It is immediate! The first API method to be called is a POST method, and as I send the request, the exception happens... it is not a timeout Commented Aug 30, 2022 at 12:57
  • Do search for "failed to determine the https port for redirect". Here is one result : thecodebuzz.com/… Commented Aug 30, 2022 at 13:02
  • I tried both solutions presented on the article, none worked :/ as far as I know, our publication server already handles https redirection... I guess Commented Aug 30, 2022 at 14:06
  • Did you search web for other solutions? Have seen issues where connection to proxy server was using https and connection to URL was using http, or proxy was using http and URL was using https. Port 443 is TLS for https. Port 80 or 8080 is http. If you are behind a firewall you may be using port 80ot 8080 to proxy. Maybe using port 80 or 8080 instead of 443 will solve issue. Commented Aug 30, 2022 at 14:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.