c# - Asp.net core 2.0 Configure.Services<model>() binding isn't working with user secrets -


my problem configure.services<model>() not binding user secrets class. injected class inside service has null properties.

my secrets.json looks this:

{   "othersection": {     "another": {       "prop": "1234"     }   },   "emailsettings": {     "mailgun": {       "apikey": "omitted",       "baseuri": "omitted",       "requesturi": "omitted",       "from": "omitted",       "smtp": "omitted",       "smtpuser": "omitted",       "smtppass": "omitted",       "smtpport": 465     }   } } 

i have created user settings class:

public class mailgunsettings : ismtpdetails {     public string apikey { get; set; }     public string baseuri { get; set; }     public string requesturi { get; set; }     public string { get; set; }     public string smtp { get; set; }     public string smtpuser { get; set; }     public string smtppass { get; set; }     public int smtpport { get; set; } } 

in startup.cs configureservices() registering class so:

services.configure<mailgunsettings>(options => configuration.getsection("emailsettings:mailgun").bind(options)); 

and injecting emailsender service so:

private readonly ismtpdetails _smtpsettings;  public emailsender(ioptions<mailgunsettings> smtpsettings) {     this._smtpsettings = smtpsettings.value; } 

every property of smtpsettings.value null.

i able access configuration settings inside startup.cs with:

configuration["emailsettings:mailgun:apikey"]; 

i have tried installing following nuget package:

<packagereference include="microsoft.extensions.configuration.binder" version="2.0.0" /> 

it didn't help.

i can't see why it's not binding , not generating error message.

can help?

try without ismtpdetails interface:

public class mailgunsettings {     public string apikey { get; set; }     public string baseuri { get; set; }     public string requesturi { get; set; }     public string { get; set; }     public string smtp { get; set; }     public string smtpuser { get; set; }     public string smtppass { get; set; }     public int smtpport { get; set; } }  private readonly mailgunsettings _smtpsettings;  public emailsender(ioptions<mailgunsettings> smtpsettings) {     this._smtpsettings = smtpsettings.value; } 

if works need configure services.configure<ismtpdetails> , inject ioptions<ismtpdetails>

i recommend use mailgunsettings , rid of interface ismtpdetails, there no need interface .net core providing ioptions<t> interface.

and here article sanity check ioptions pattern: https://andrewlock.net/how-to-use-the-ioptions-pattern-for-configuration-in-asp-net-core-rc2/


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -