I have an ASP .NET app that use MySQL instead of SQL Server. This app run perfectly on development environment (Windows machine), both on Debug and Release.
However when I publish and deploy on a Ubuntu Server machine (linux-arm64, self-contained) and run it, I got NRE in which MySQL connectionString
is null.
Here is the relevant part in Startup.cs
:
//string mySqlConnectionStr = Configuration.GetConnectionString("DefaultConnection");
// NOTE: hard-code because connection string is null on linux;
const string mySqlConnectionStr = "Server=localhost; port=3306; Database=aspnet_p24_mysql; user=root; password=root; Persist Security Info=False; Connect Timeout=300";
services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySql(mySqlConnectionStr, opts => opts.ServerVersion(ServerVersion.AutoDetect(mySqlConnectionStr)))
);
And here is appsettings.json
, even though I tried hard-code the connection string:
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost; port=3306; Database=aspnet_p24_mysql; user=root; password=root; Persist Security Info=False; Connect Timeout=300"
},
...
}
I did search around and found some related questions/answers, but none matched my situation, since I didn’t make any typo on ConnectionStrings
, and the issue only happen on the deployed ubuntu machine.
2
Answers
It seems that running the app from mounted External HDD (connect via USB) cause this problem.
I have two copy of the exact same app, one on internal disk and one on external disk. The one on internal disk work perfectly fine, while the one on external disk either report null connection string or just a simple message
Stack OverflownAborted (Core dumped)
on two lines.Troubleshooting Steps:
Check your publish files in linux server, check whether the
appsettings.json
file is exist.If it exist, please check the Connectionstrings in your file.
If not exist or the
appsettings.json
is different. please try to include theappsettings.json
file when you deploy it.Try to use AddJsonFile in startup.cs file.