2017-03-13 14 views
0

ASP.NETコアWebAPIのサービスとしてAppSettingsにアクセスしようとしています。 Configuration.GetSection( "AppSettings")を実行すると、nullが返されますが、構成値[ConfigurationSet ":StorageConnectionKey:AccountName"]にアクセスできます。私は何が間違っているのか分かりません。サービスasp.netコアとしてのコンフィグレーションへのアクセス

マイStartup.csが

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using Microsoft.AspNetCore.Builder; 
using Microsoft.AspNetCore.Hosting; 
using Microsoft.Extensions.Configuration; 
using Microsoft.Extensions.DependencyInjection; 
using Microsoft.Extensions.Logging; 
using Library; 

namespace Athraya 
{ 
    public class Startup 
    { 
     public Startup(IHostingEnvironment env) 
     { 
      var builder = new ConfigurationBuilder() 
       .SetBasePath(env.ContentRootPath) 
       .AddJsonFile("appsettings.json") 
       // .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 
       .AddEnvironmentVariables(); 
      Configuration = builder.Build(); 
     } 

     public IConfiguration Configuration { get; set; } 

     // This method gets called by the runtime. Use this method to add services to the container. 
     public void ConfigureServices(IServiceCollection services) 
     { 
      // Add framework services. 
      services.AddMvc(); 

      services.AddOptions(); 


      services.Configure<AppSettings>(options => Configuration.GetSection("AppSettings")); 

      // *If* you need access to generic IConfiguration this is **required** 
      services.AddSingleton<IConfiguration>(Configuration); 
     } 

     // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
     public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
     { 
      loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
      loggerFactory.AddDebug(); 

      app.UseMvc(); 
     } 
    } 
} 

の下に示されており、私のAppSettingは、私は私がしようとしています

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Library 
{ 
    public class AppSettings 
    { 
     public StorageConnectionKey storageKey {get; set; } 
     public CloudContainerKey containerKey { get; set; } 
    } 
} 

    namespace Library 
{ 
    public class CloudContainerKey 
    { 
     public string ContainerName { get; set; } 
     public string FileName { get; set; } 
    } 
} 

    namespace Library 
{ 
    public class StorageConnectionKey 
    { 
     public string AccountName { get; set; } 
     public string AccountKey { get; set; } 
    } 
} 

を必要なクラスを持っているライブラリプロジェクトを持っている

{ 
    "AppSettings": { 
    "StorageConnectionKey": { 
     "AccountName": "myaccountName", 
     "AccountKey": "abc" 

    }, 
    "CloudContainerkey": { 
     "ContainerName": "mycontainername", 
     "FileName": "db.dat" 
    } 
    }, 
    "Logging": { 
    "IncludeScopes": false, 
    "LogLevel": { 
     "Default": "Warning" 
    } 
    } 
} 

ですコントローラで取得するには

public class ValuesController : Controller 
    { 
     private readonly AppSettings _appSettings; 

     public ValuesController(IOptions<AppSettings> settings) 
     { 
      _appSettings = settings.Value; 
     } 
} 

ここをクリックしてください。 IConfigurationインスタンスの使用を使用して、セットアップのAppSettingsへ

services.Configure<AppSettings>(Configuration.GetSection("AppSettings")); 

答えて

1

::私はそれはこのようにすべきだと思います

+0

あなたがそのようなサービスを持っている場合、私はまだnullを取得します。設定(Configuration.GetSection( "AppSettings")); –

+0

あなたは私のスタートアップを変更して、私が言及したように私がコントローラにアクセスできるように、どうすればいいと思いますか?私はこのブログのhttps://www.codefluff.com/getting-configuration-settings-asp-net-core-mvc/に従おうとしました。 ASP.NETコアで何かが変更されたことを私に教えてください。 –

+1

@Vijayaravindは答えを更新しました。オプションクラスのpropertisの名前を、パラメータ名の設定と同じにする必要があります。 – Set

0

services.Configure<AppSettings>(Configuration.GetSection("AppSettings")); 

はまた、あなたの設定パラメータとして、同じプロパティ名を使用する必要があります。あなたのAppSettingsに変更します。手動でオプションを設定するために使用されるアクションを登録することができます拡張メソッドを使用すると、あなたの場合は

public class AppSettings 
{ 
    public StorageConnectionKey StorageConnectionKey {get; set; } 
    public CloudContainerKey CloudContainerKey { get; set; } 
} 

を、あなたは、nullを持っています。あなたがメソッドの定義に見ると、あなたが表示されます。あなたは次のコードを使用する場合つまり

// 
// Summary: 
//  Registers an action used to configure a particular type of options. /// 
// 
// Parameters: 
// services: 
//  The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the services 
//  to. 
// 
// configureOptions: 
//  The action used to configure the options. 
// 
// Type parameters: 
// TOptions: 
//  The options type to be configured. 
// 
// Returns: 
//  The Microsoft.Extensions.DependencyInjection.IServiceCollection so that additional 
//  calls can be chained. 
public static IServiceCollection Configure<TOptions>(this IServiceCollection services, 
    Action<TOptions> configureOptions) where TOptions : class; 

を、あなたは、ラムダ関数を登録し、すでにAppSettingsのインスタンスを使用します。

services.Configure<AppSettings>(option => 
{ 
    // option here is the AppSettings and so we can override value like: 
    option.StorageConnectionKey = "some_new_value"; 
}); 
+0

これはどのようにあります私はもともとそれを持っていたし、まだ私はこのブログに基づいてそれを変更したので、Nullを持っているhttps://weblog.west-wind.com/posts/2016/may/23/strongly-typed-configuration-settings-in-aspnet-core –

関連する問題