2016-05-15 11 views
0

ASP.Net 5 MVCで再生する。この質問は、完全な答えではなく飛び回っています。私がしたいことは、AppSettingsにアクセスできるヘルパークラスを持つことです。私はコントローラとビューでアクセスできますが、自分のカスタムクラスでアクセスする方法を理解していません。そうしたスタートアップを設定してください。カスタムクラスのアプリケーション設定ASP.Net 5 MVC 6

public Startup(IHostingEnvironment env) 
    { 
     // Set up configuration sources. 

     var builder = new ConfigurationBuilder() 
      .AddJsonFile("config.json") 
      .AddJsonFile($"config.{env.EnvironmentName}.json", optional: true); 

     builder.AddEnvironmentVariables(); 
     Configuration = builder.Build(); 
    } 

    public IConfigurationRoot Configuration { get; set; } 

    // 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(IServiceCollection services) 
    { 
     services.AddMvc(); 
     services.AddOptions(); 
     services.Configure<AppSettings>(Configuration.GetSection("AppSettings")); 
    } 
................. 
................. 

答えて

1

、カスタムクラスであなたのAppSettingsプロパティにアクセスするような静的なインスタンスとしての構成を作るために:例のConnectionStringのために(アプリケーション内の任意の場所を

public static IConfigurationRoot Configuration { get; set; } 

とあなたのAppSettingsの使用を作ります)のように:あなたがあなた自身のクラスでそれを必要とする場合

var connectionString = Startup.Configuration["Data:DefaultConnection:ConnectionString"]; 
0

、それはそのクラスのコンストラクタに、または、それをパラメータとして渡すことはおそらく正しいです。例えば;

public class Notifications 
{ 
    public Notifications(AppSettings settings) { 
     this.settings = settings; 
    } 

    public void SendEmail(string subject, string body) { 
     SmptClient.Send(subject, body, settings["email address"]); 
    } 
} 

通常、コントローラから渡します。

これはグローバル変数を避けています。これは常に良いことです。あなたのconfig.jsonファイルでそう

2

、あなたはその

services.Configure<SmtpEmailSetting>(Configuration.GetSection("smtp"));

これはあなたのSmtpEmailSettingのルックスであるような何かをする必要があなたのConfigureServices方法で次に設定

{ 
    "smtp": { 
     "SenderEmail": "[email protected]", 
     "SenderFrom": "Test User" 
    } 
} 

を次したとしlike

public class SmtpEmailSetting 
{ 
    public string SenderEmail { get; set; } 
    public string SenderFrom { get; set; } 
} 

は、これはあなたがIOptions<>クラスのジェネリック型パラメータとしてコンストラクタで使用されるべきである

public class SendEmailService 
{ 
    private readonly SmtpEmailSetting _smtpEmailSetting; 

    public SendEmailService(IOptions<SmtpEmailSetting> smtpOptions) 
    { 
     _smtpEmailSetting = smtpOptions.Value; 
    } 

    public void SendEmail() 
    { 
     var fromEmail = _smtpEmailSetting.SenderEmail; 
     var displayName = _smtpEmailSetting.SenderFrom; 
    } 
} 

だから基本的にはあなたが(あなたが呼び出すことを好むものは何でも)あなたの設定やオプションを使用するすべてのサービスやコントローラーであなたの設定にアクセスする方法です。希望しますそれは助けてください

0

ちょうどadeel41の答えに追加するには、これは正しいと素晴らしい作品ですが、私自身は、依存性注入を使用するときにIOptionオブジェクトをドラッグしたくありませんでした。

は、だから私は

services.AddSingleton<ISmtpEmailSettings>(Configuration.GetSection("SmtpSettings").Get<SmtpEmailSettings>()); 

ような何かを行うことを好む最も重要なのGet構文はオブジェクトにあなたのJSONをデシリアライズするGetSectionに追加されます。

0

RC1の時に、私はthis postからインスピレーションを得ました。これはRick Strahlによってうまく機能しました。既に提案されている他のアプローチと非常によく似ています。

この質問は、RTMリリースの時点での調査結果に更新されます。 Configuration.GetSection(string topLevelKey)のようには思えませんが、少なくとも私の場合は常にnullを返します(たとえ設定ソースが正しく設定されていても)。いくつかの検索の後

this other SO threadは使用して、正しい方向に私を指摘:

// create empty config object 
var smtpEmailSetting = new SmtpEmailSetting(); 

// fill it from configuration section 
Configuration.GetSection("smtp").Bind(smtpEmailSetting); 

// use it, e.g. by registering it into DI 
services.Configure<SmtpEmailSetting>(smtpEmailSetting); 

HTH

関連する問題