2016-03-15 96 views
29

我正在嘗試爲正在構建的.Net Web應用程序設置應用程序基本路徑。我在配置生成器上不斷收到錯誤。這是我得到的錯誤。使用ConfigurationBuilder設置基本路徑

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); 
    } 
} 

的錯誤是右近startup.cs頂部public startup功能。

回答

64

我能解決這個問題。如果您尚未解決問題,請在project.json中嘗試以下 。添加以下內容:

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

,它應該工作

+0

因爲現在沒有project.json文件,所以在EF Core 2.0中怎麼做? – ATHER

+1

@ATHER:只需添加兩個nugets就像添加其他nugets一樣。 – Dejan

+1

@ATHER'dotnet add package '這裏的packageName是'Microsoft.Extensions.Configuration.FileExtensions',或者在Visual Studio中添加nuget包 –

1

嘗試添加以下到您的project.json依賴關係:

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

感謝您的建議!我嘗試將兩者都添加到project.json中,然後執行「dnu restore」。但是我的Startup.cs中仍然出現同樣的錯誤。任何其他想法?我找到了我正在使用的確切函數的文檔** [here](https://github.com/aspnet/Configuration/blob/1.0.0-beta8/src/Microsoft.Framework.Configuration.FileExtensions/FileConfigurationExtensions。 CS)**。 'SetBasePath()' – currymanswag

1

以下內容添加到您的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" />

這爲我工作。