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:
- (source) Add
android:usesCleartextTraffic="true" android:networkSecurityConfig="@xml/network_security_config"to the app'sAndroidManifest.xmland create a new xml file inResources\xml\network_security_config.xmlwith 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>
(source) Set the HttpClient implementation to Android and the SSL/TLS implementation to Native TLS1.2+ on Project Options > Android Options > Advanced Options;
(source) Edit the
CreateHostBuildermethod onProgram.cslike 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.