Program Tip

ConfigurationBuilder를 사용하여 기본 경로 설정

programtip 2020. 11. 3. 18:50
반응형

ConfigurationBuilder를 사용하여 기본 경로 설정


내가 구축중인 .Net 웹 앱의 애플리케이션 기본 경로를 설정하려고합니다. 구성 빌더에서 계속 오류가 발생합니다. 이것은 내가 얻는 오류입니다.

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 상단 근처의 함수에 있습니다.


문제를 해결할 수있었습니다. 아직 해결하지 않은 경우 project.json에서 다음을 시도하십시오. 다음을 추가하십시오.

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

그리고 그것은 작동합니다


이 문제가 여전히 발생하는지 확실하지 않지만 dotnetcore 콘솔 프로젝트 (netcoreapp2.0)에서 다음을 통해이 문제를 해결할 수있었습니다.

dotnet add package Microsoft.Extensions.Configuration.Json

If you`re running a .NET Core 1.x or .NET Standard 1.x, you should run this command:

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

If your project is inside another folder:

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

...where MyProject is the name of the .csproj file.


Try adding the following in your .csproj file:

<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>

Add the following to your project.json:

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

Try adding the following to your project.json dependencies: "Microsoft.Extensions.Configuration.CommandLine": "1.1.1",

Or in project.csproj:
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="1.1.1" />

This worked for me.


Try adding the following to your project.json dependencies:

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

Additionally do not forget to set "Copy to Output Directory" property to "Copy always" of Json file from Properties window.

참고URL : https://stackoverflow.com/questions/36001695/setting-base-path-using-configurationbuilder

반응형