使用ConfigurationBuilder设置基本路径


90

我正在尝试为正在构建的.Net Web应用程序设置应用程序基本路径。我在“配置”构建器上不断收到错误。这是我得到的错误。

DNX,Version=v4.5.1 error CS1061: 'ConfigurationBuilder' does not contain a definition for 'SetBasePath' and no extension method 'SetBasePath' accepting a first argument of type 'ConfigurationBuilder' could be found (are you missing a using directive or an assembly reference?)

我假设我会得到同样的错误我.AddJsonFile().AddEnvironmentVariables()。我做错什么了吗?我没有在我的project.json中添加正确的依赖项吗?我已经封装了startup.cs和project.json。

project.json

{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"tooling": {
"defaultNamespace": "TripPlanner"
},

"dependencies": {
  "Microsoft.AspNet.StaticFiles":  "1.0.0-rc1-final",
  "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
  "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
  "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
  "Microsoft.Framework.Configuration": "1.0.0-beta8",
  "Microsoft.Framework.DependencyInjection": "1.0.0-beta8"
  //"Microsoft.Extensions.PlatformAbstractions": "1.0.0-beta8"
},

"commands": {
  "web": "Microsoft.AspNet.Server.Kestrel"
},

"frameworks": {
  "dnx451": { },
  "dnxcore50": { }
},

"exclude": [
  "wwwroot",
  "node_modules"
],
"publishExclude": [
  "**.user",
  "**.vspscc"
 ]
}

startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.PlatformAbstractions;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.DependencyInjection;
using TripPlanner.Services;



namespace TripPlanner
{
  public class Startup
  {
    public static IConfigurationRoot Configuration;

    public Startup(IApplicationEnvironment appEnv){
        var builder = new ConfigurationBuilder()
            .SetBasePath(appEnv.ApplicationBasePath)
            .AddJsonFile("config.json")
            .AddEnvironmentVariables();

        Configuration = builder.Build();
    }

    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(Microsoft.Extensions.DependencyInjection.IServiceCollection services)
    {
        services.AddMvc();
        #if DEBUG
        services.AddScoped<IMailService, DebugMailService> ();
        #else
        services.AddScoped<IMailService, RealMailService> ();
        #endif
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app)
    {
       //app.UseDefaultFiles();
       app.UseStaticFiles();
       app.UseMvc(config =>
       {
           config.MapRoute(
               name: "Default",
               template: "{controller}/{action}/{id?}",
               defaults: new { controller  = "App", action = "Index"}
           );
       });

    }

    // Entry point for the application.
    public static void Main(string[] args) => Microsoft.AspNet.Hosting.WebApplication.Run<Startup>(args);
  }
}

错误出现在public startupstartup.cs顶部附近的函数中。

Answers:


176

我能够解决这个问题。如果尚未解决,请在project.json中尝试以下操作。添加以下内容:

"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-*",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final"

它应该工作


由于现在没有project.json文件,如何在EF Core 2.0中执行此操作?
ATHER

3
@ATHER:就像添加任何其他nuget一样,添加两个nuget。
Dejan

5
@ATHERdotnet add package <packageName>其中packageName在这种情况下是Microsoft.Extensions.Configuration.FileExtensions,或者在Visual Studio中添加nuget包
William Ardila

51

不知道是否有人仍然遇到此问题,但是我能够通过以下方式在dotnetcore控制台项目(netcoreapp2.0)中解决此问题:

dotnet add package Microsoft.Extensions.Configuration.Json

1
谢谢@bishop。在我的.net核心库中添加它可以解决我的问题。
华莱士B.麦克卢尔

是的,.Json通过NuGet安装通行也可以。
James Hirschorn

6

尝试在.csproj文件中添加以下内容:

<ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
</ItemGroup>

Microsoft.Extensions.Configuration.FileExtensions和Microsoft.Extensions.Configuration.Json正是缺少的软件包。
Ognyan Dimitrov,

3

如果您正在运行.NET Core 1.x.NET Standard 1.x,则应运行以下命令:

dotnet add package Microsoft.Extensions.Configuration.Json -v 1.1.1

如果您的项目在另一个文件夹中:

dotnet add .\src\MyProject package Microsoft.Extensions.Configuration.Json -v 1.1.1

...其中MyProject.csproj文件名。


3

只是再提出一个可能的问题...

我必须添加以下内容:

<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="3.0.0" />

为了对此进行编译:

var builder = new ConfigurationBuilder()
  .SetBasePath(Directory.GetCurrentDirectory())
  ...

编辑:我几个月前写了这个答案,但是我很确定它是用于控制台应用程序的。


1
像魅力一样工作!
Daniel Cohen

2

将以下内容添加到您的project.json中:

"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-*"

2

尝试将以下内容添加到project.json依赖项中: "Microsoft.Extensions.Configuration.CommandLine": "1.1.1",

或在project.csproj中:
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="1.1.1" />

这对我有用。


1

尝试将以下内容添加到project.json依赖项中:

"Microsoft.Extensions.Configuration": "1.0.0-*",
"Microsoft.Extensions.Configuration.Abstractions": "1.0.0-*",

谢谢你的建议!我尝试将两者都添加到project.json中,然后进行了“ dnu恢复”。但是我在Startup.cs中仍然遇到相同的错误。还有其他想法吗?我在这里找到了我正在使用的确切功能的文档。SetBasePath()
currymanswag '16

0

另外,不要忘记从“属性”窗口中将Json文件的“复制到输出目录”属性设置为“总是复制”。


0

其他要考虑的事项:

using Microsoft.Extensions.Configuration;

没有该“使用”语句,它将无法在Microsoft.Extensions.Configuration.FileExtensions中找到扩展方法。

由于我们也是:

using System.Configuration;

并且与“ ConfigurationBuilder”发生了名称冲突。添加

using Microsoft.Extensions.Configuration;

行...删除

using System.Configuration;

行,然后完全限定System.Configuration下的任何内容。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.