When I run a REST API written in .NET Core 2.0 that uses HttpSys from within Visual Studio 2017, I get the following output:
Exe path : C:\Program Files\dotnet\dotnet.exe Directory : C:\Program Files\dotnet Connection string : Server=(localdb)\mssqllocaldb;Database=MyApp;Trusted_Connection=True;MultipleActiveResultSets=true Hosting environment: Development Content root path: C:\Users\Torsten\source\repos\MyApp\MyApp Now listening on: http://*:5000 Application started. Press Ctrl+C to shut down.
When I run the same application from the command line with
dotnet myapp
I get this output:
dotnet MyApp.dll Exe path : C:\Program Files\dotnet\dotnet.exe Directory : C:\Program Files\dotnet Connection string : info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0] User profile is available. Using 'C:\Users\Torsten\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest. System.ArgumentNullException: Value cannot be null. Parameter name: connectionString
Why is the connection string not recognized when started from the command line?
The connection string is defined in appsettings.json as:
{
"ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=MyApp;Trusted_Connection=True;MultipleActiveResultSets=true" },
And the Startup.cs reads:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
Console.WriteLine("Exe path : " + pathToExe);
Console.WriteLine("Directory : " + Path.GetDirectoryName(pathToExe));
Console.WriteLine("Command line arguments :" + String.Join(", ",Environment.GetCommandLineArgs()));
Console.WriteLine("Connection string : " + Configuration["ConnectionStrings:DefaultConnection"]);
services.AddDbContext<MyAppContext>(options =>
options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"]));
services.AddMvc();
}
Configuration
, because that's the one that's not initializing properly. Dependency injection is fun! – Jeroen MostertStart-process -FilePath dotnet.exe MyApp.dll -workingdirectory .
Same result. – tobre[Environment]::CurrentDirectory = pwd
first. – Jeroen Mostert