dotnet 运行 - MissingMethodException

dotnet run - MissingMethodException

我正在尝试将 RC1 项目迁移到 RC2。我的项目恢复和建设成功。但是当我输入 dotnet 运行 时,我得到了异常:找不到方法:'Microsoft.Extensions.Configuration.IConfigurationBuilder Microsoft.Extensions.Configuration.IConfigurationBuilder.Add(Microsoft.Extensions.Configuration.IConfigurationProvider)'.

这是我的启动构造函数:

public Startup(IHostingEnvironment env)
    {
        Log.Logger = new LoggerConfiguration().MinimumLevel.Debug().
            WriteTo.RollingFile(@"C:\log\file-{Date}.txt",
                outputTemplate:
                    "{Timestamp: yyyy-MM-dd HH:mm:ss.fff zzz} {Level}:{EventId} [{SourceContext}] {Message}{NewLine}{Exception}")
            .CreateLogger();

        var builder = new ConfigurationBuilder() //<-error in this line
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json")
            .AddJsonFile("config.json")
            .AddEnvironmentVariables();

        Configuration = builder.Build();
    }

这是我的 project.json:

    {
     "version": "1.0.0-*",
     "buildOptions": {
     "emitEntryPoint": true
  },
  "runtimeOptions": {
    "gcServer": false,
    "gcConcurrent": true
  },
  "dependencies": {
    "bootstrap": "4.0.0-alpha2",
    "BootstrapNotifications": "0.3.2",
    "bootstrap-notify": "0.1.0",
    "Dapper": "1.50.0-rc2",
    "Microsoft.AspNet.Antiforgery": "1.0.0-rc1-final",
    "Microsoft.AspNet.Http.Extensions": "1.0.0-rc1-final",
    "Microsoft.AspNet.Tooling.Razor": "1.0.0-rc1-final",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Http.Extensions": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.JsonPatch": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc.Formatters.Json": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc.Razor": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Mvc.ViewFeatures": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.Server.WebListener": "0.1.0-rc2-final",
    "Microsoft.AspNetCore.Session": "1.0.0-rc2-final",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0-rc2-final",
    "Microsoft.Exchange.WebServices": "2.2.0",
    "Microsoft.Extensions.Configuration.Abstractions": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Binder": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc2-final",
    "Microsoft.Extensions.Primitives": "1.0.0-rc2-final",
    "Microsoft.Framework.Configuration.EnvironmentVariables": "1.0.0-beta6",
    "Microsoft.Framework.ConfigurationModel": "1.0.0-beta4",
    "Microsoft.Framework.ConfigurationModel.Json": "1.0.0-beta4",
    "Microsoft.Framework.Logging": "1.0.0-beta8",
    "Microsoft.NetCore.App": {
      "type": "platform",
      "version": "1.0.0-rc2-16357"
    },
    "Microsoft.NETCore.Platforms": "1.0.1-rc2-24027",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc2-final",
    "Newtonsoft.Json": "8.0.3",
    "OctoPack": "3.0.60",
    "PagedList": "1.17.0",
    "PagedList.Mvc": "4.5.0",
    "Serilog.Extensions.Logging": "1.0.0-rc2-10104",
    "Serilog.Framework.Logging": "1.0.0-rc1-final-10083",
    "Serilog.Sinks.Literate": "2.0.0-rc-25",
    "Serilog.Sinks.RollingFile": "2.0.0-rc-703",
    "System.Console": "4.0.0-rc2-24027",
    "System.Linq": "4.1.0-rc2-24027",
    "System.Linq.Dynamic": "1.0.6",
    "System.Runtime.Extensions": "4.1.0-rc2-24027",
    "System.Xml.XmlDocument": "4.0.1-rc2-24027"
  },
  "exclude": [
    "wwwroot",
    "node_modules"
  ],
  "frameworks": {
    "net46": {
      "imports": [
        "dnxcore50",
        "portable-net45+win8"
      ],
      "dependencies": {
      }
    }
  },
  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "appsettings.json",
      "web.config"
    ]
  }
}

您使用的软件包版本不匹配。具体来说,异常是由这一行引起的:

"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc1-final",

其他包都是RC2,这个是RC1。这意味着 AddEnvironmentVariables() 是 RC1 版本,它会尝试从其依赖项中调用方法,这在 RC2 中已更改。

要解决此问题,请仅使用 RC2 包。

该解决方案与 svick 的回答非常相关。

我进行了清理并重建,错误已清除...