Reputation: 327
i'm trying to follow this example from Microsoft Docs to retrieve some configuration options from an appsettings.json file in my ASP.NET Core 2.1 application.
I've bound the class
public class MailServer
{
public string Host { get; set; }
public int Port { get; set; }
public bool UseSSL { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string FromAddress { get; set; }
public MailServer()
{
}
}
to the config in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// .....
services.Configure<MailServer>(Configuration.GetSection("MailServer"));
// .....
services.AddSingleton<IScheduledTask, ScheduledTask>();
}
In the startup i also added ScheduledTask as a singleton in order to access the context from that specific class - that's also where i want to access the options.
public interface IScheduledTask
{
void MonitorCloudHosts();
}
public class ScheduledTask : IScheduledTask
{
private readonly IServiceProvider _ServiceProvider;
private readonly MailServer _mailServer;
// note here you ask to the injector for IServiceProvider
public ScheduledTask(IServiceProvider serviceProvider, IOptions<MailServer> optionsAccessor)
{
_ServiceProvider = serviceProvider;
_mailServer = optionsAccessor.Value;
}
public void MonitorCloudHosts()
{
// Do some stuff
var xyz = _mailServer.Host;
}
}
this is my appsettings.json file:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"CloudMonitorConnection": "Server=.\\SQLEXPRESS;Database=CloudMonitor;Integrated Security=SSPI;MultipleActiveResultSets=true;",
"HangFireConnection": "Server=.\\SQLEXPRESS;Database=HangFire;Integrated Security=SSPI;"
},
"MailServer": {
"Host": "smtp.gmail.com",
"Port": "587",
"UseSSL": "True",
"Username": "[email protected]",
"Password": "xxxxxxx",
"FromAddress": "xxxxxxx<[email protected]>"
}
}
compiler gives error on calling _mailServer.Host saying "An object reference is required..." - but doesnt give error on optionsAccessor.Value
.
Shouldn't optionsAccessor.Value
return an instance of MailServer? Am I doing some rookie mistake here?
Upvotes: 2
Views: 2281
Reputation: 757
Property UseSSL
of MailServer
class is bool
but is set to "True"
in appsettings.json.
You should change appsettings.json like this:
"MailServer": {
"Host": "smtp.gmail.com",
"Port": 587,
"UseSSL": true,
"Username": "[email protected]",
"Password": "xxxxxxx",
"FromAddress": "xxxxxxx<[email protected]>"
}
EDIT: I have created an empty project with this code and it compiles. Maybe there's an error in using statement or strict mode is enabled?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace samp
{
public class MailServer
{
public string Host { get; set; }
public int Port { get; set; }
public bool UseSSL { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string FromAddress { get; set; }
public MailServer()
{
}
}
public interface IScheduledTask
{
void MonitorCloudHosts();
}
public class ScheduledTask : IScheduledTask
{
private readonly IServiceProvider _ServiceProvider;
private readonly MailServer _mailServer;
// note here you ask to the injector for IServiceProvider
public ScheduledTask(IServiceProvider serviceProvider, IOptions<MailServer> optionsAccessor)
{
_ServiceProvider = serviceProvider;
_mailServer = optionsAccessor.Value;
}
public void MonitorCloudHosts()
{
// Do some stuff
var xyz = _mailServer.Host;
}
}
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.Configure<MailServer>(Configuration.GetSection("MailServer"));
services.AddSingleton<IScheduledTask, ScheduledTask>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
}
}
}
Upvotes: 1