C# 从命令行运行.NET Core 2.0 API

C# 从命令行运行.NET Core 2.0 API,c#,windows,.net-core,connection-string,C#,Windows,.net Core,Connection String,当我在Visual Studio 2017中运行以.NET Core 2.0编写的REST API时,我得到以下输出: Exe路径:C:\Program Files\dotnet\dotnet.Exe目录:C:\Program 文件\dotnet连接字符串: 服务器=(localdb)\mssqllocaldb;数据库=MyApp;Trusted_Connection=True;MultipleActiveResultSets=true 托管环境:开发内容根路径: C:\Users\Torste

当我在Visual Studio 2017中运行以.NET Core 2.0编写的REST API时,我得到以下输出:

Exe路径:C:\Program Files\dotnet\dotnet.Exe目录:C:\Program 文件\dotnet连接字符串: 服务器=(localdb)\mssqllocaldb;数据库=MyApp;Trusted_Connection=True;MultipleActiveResultSets=true 托管环境:开发内容根路径: C:\Users\Torsten\source\repos\MyApp\MyApp正在侦听 上:http://*:5000应用程序已启动。按Ctrl+C组合键关闭

当我从命令行使用

dotnet myapp
我得到这个输出:

dotnet MyApp.dll Exe路径:C:\Program Files\dotnet\dotnet.exe目录:C:\Program Files\dotnet Connection 字符串:信息: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0] 用户配置文件可用。使用“C:\Users\Torsten\AppData\Local\ASP.NET\DataProtection密钥”作为密钥 存储库和Windows DPAPI可在静止时加密密钥。 System.ArgumentNullException:值不能为null。参数名称: 连接字符串

为什么从命令行启动时无法识别连接字符串

连接字符串在appsettings.json中定义为:

{
  "ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=MyApp;Trusted_Connection=True;MultipleActiveResultSets=true" },
Startup.cs的内容如下:

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();
    }
公共类启动
{
public void配置服务(IServiceCollection服务)
{
var pathToExe=Process.GetCurrentProcess().MainModule.FileName;
Console.WriteLine(“Exe路径:”+pathToExe);
Console.WriteLine(“目录:”+Path.GetDirectoryName(pathToExe));
WriteLine(“命令行参数:+String.Join(“,”,Environment.GetCommandLineArgs());
WriteLine(“连接字符串:+Configuration[“ConnectionString:DefaultConnection”]);
services.AddDbContext(选项=>
使用SQLServer(配置[“ConnectionString:DefaultConnection”]);
services.AddMvc();
}

无法读取ConnectionString的原因是,默认情况下appsettings.json不会复制到输出目录。只有将“复制到输出目录”配置为“复制如果更新”或“复制始终”,它才会复制到输出目录。 您可以通过查看
Configure
方法中的
env.ContentRootPath
来区分差异

从Visual Studio中启动时,ContentRootPath是项目目录

C:\Users\someone\source\repos\MyApp\MyApp

从命令行启动时,ContentRootPath是输出目录

C:\Users\someone\source\repos\MyApp\MyApp\bin\debug\netcoreapp2.0


无法读取ConnectionString的原因是,默认情况下appsettings.json不会复制到输出目录。只有将Copy to output directory配置为Copy if newer或Copy时,才会将其复制到输出目录。 您可以通过查看
Configure
方法中的
env.ContentRootPath
来区分差异

从Visual Studio中启动时,ContentRootPath是项目目录

C:\Users\someone\source\repos\MyApp\MyApp

从命令行启动时,ContentRootPath是输出目录

C:\Users\someone\source\repos\MyApp\MyApp\bin\debug\netcoreapp2.0


从项目文件夹中重新运行带-v开关的dotnet命令以查看结果。如果您看到对的引用。。\microsoft.entityframeworkcore.tools.dotnet\1.0.0..问题在于必须更新的CLIToolReference

从旧版本的asp.net core迁移项目时,我遇到了相同的问题。迁移后,DotNetCliToolReference不会自动更新。 更新yourproject.csproj文件以使用2.0.0版本的CLI,如下面的代码段所示:

<ItemGroup>

        ...
          <DotNetCliToolReference 
               Include="Microsoft.EntityFrameworkCore.Tools.DotNet" 
               Version="2.0.0" />
</ItemGroup>

...

从项目文件夹中重新运行带-v开关的dotnet命令以查看结果。如果您看到对的引用。。\microsoft.entityframeworkcore.tools.dotnet\1.0.0..问题在于必须更新的CLIToolReference

从旧版本的asp.net core迁移项目时,我遇到了相同的问题。迁移后,DotNetCliToolReference不会自动更新。 更新yourproject.csproj文件以使用2.0.0版本的CLI,如下面的代码段所示:

<ItemGroup>

        ...
          <DotNetCliToolReference 
               Include="Microsoft.EntityFrameworkCore.Tools.DotNet" 
               Version="2.0.0" />
</ItemGroup>

...

您必须询问正在创建的
配置,因为这是一个没有正确初始化的配置。依赖项注入很有趣!您是否从应用程序目录运行命令?配置加载默认为当前工作目录..是的。我从应用程序目录运行它。我还尝试d与
启动进程-FilePath dotnet.exe MyApp.dll-workingdirectory一起运行。
结果相同。但看起来像PowerShell。PowerShell中的当前位置与Windows中的当前目录不同。请尝试
[环境]::CurrentDirectory=pwd
首先。你必须询问正在创建的
配置,因为这是一个没有正确初始化的配置。依赖项注入很有趣!你是从应用程序的目录运行命令吗?配置加载默认为当前工作目录..是的。我是从ap运行它应用程序目录。我还尝试了
启动进程-FilePath dotnet.exe MyApp.dll-workingdirectory。
相同的结果。但看起来像PowerShell。PowerShell中的当前位置与Windows中的当前目录不同。请尝试
[环境]::CurrentDirectory=pwd
首先。还要检查您的C:\Users\Torsten\AppData\Local\ASP.NET\DataProtection Keys\..xml文件,以引用Microsoft.AspNetCore.DataProtection,Version=2.0.0.0。如果您处于开发环境中,请在