我正在尝试遵循this example from Microsoft Docs来从ASP.NET Core 2.1应用程序中的appsettings.json文件中检索一些配置选项。
我上课了
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()
{
}
}
转到Startup.cs中的配置
public void ConfigureServices(IServiceCollection services)
{
// .....
services.Configure<MailServer>(Configuration.GetSection("MailServer"));
// .....
services.AddSingleton<IScheduledTask, ScheduledTask>();
}
在启动时,我还添加了ScheduledTask作为单例,以便从该特定类访问上下文-这也是我要访问选项的地方。
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;
}
}
这是我的appsettings.json文件:
{
"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": "xxxxxxx@gmail.com",
"Password": "xxxxxxx",
"FromAddress": "xxxxxxx<xxxxxxx@gmail.com>"
}
}
编译器在调用_mailServer.Host时显示错误,提示“需要对象引用...”-但是在optionsAccessor.Value
上未显示错误。
optionsAccessor.Value
是否应返回MailServer的实例?我在这里做菜鸟错误吗?
答案 0 :(得分:1)
UseSSL
类的属性MailServer
为bool
,但在appsettings.json中设置为"True"
。
您应该像这样更改appsettings.json:
"MailServer": {
"Host": "smtp.gmail.com",
"Port": 587,
"UseSSL": true,
"Username": "xxxxxxx@gmail.com",
"Password": "xxxxxxx",
"FromAddress": "xxxxxxx<xxxxxxx@gmail.com>"
}
编辑: 我用此代码创建了一个空项目,并将其编译。也许在使用语句时出错或启用了严格模式?
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();
}
}
}