我在配置带有选项的服务时面临问题。在方法1中,GreetingService
构造函数正在接收callbackAction,而在方法2中,options.Value.CallBackAction
在构造函数中作为null传递。
因此调用GreetingService.Greet(string)
对Method-I有效,而对Method-II抛出null异常。
如何使用Method-II传递回调委托
appsettings.json
{
"GreetingService": {
"From": "James"
}
}
GreetingService.cs
using Microsoft.Extensions.Options;
namespace ConsoleApp
{
public interface IGreetingService
{
void Greet(string name);
}
public class GreetingServiceOptions
{
public string? From { get; set; }
public Action<string> CallBackAction { get; set; }
}
public class GreetingService : IGreetingService
{
private readonly string? _from;
private readonly Action<string> _callBackAction;
public GreetingService(IOptions<GreetingServiceOptions> options)
{
_from = options.Value.From;
_callBackAction = options.Value.CallBackAction;
}
public void Greet(string name)
{
Console.WriteLine($"Hello, {name}! Greetings from {_from}");
_callBackAction($"Hello, {name}! Greetings from {_from}");
}
}
}
program.cs
using ConsoleApp;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
//------------------------------Method-I-----------------------------------------------
//using var host = Host.CreateDefaultBuilder()
// .ConfigureServices(services =>
// {
// services.Configure<GreetingServiceOptions>(options
// =>
// {
// options.From = "James";
// options.CallBackAction = delegate (string callBackmessage)
// {
// Console.WriteLine(callBackmessage);
// };
// });
// services.AddSingleton<IGreetingService, GreetingService>();
// }).Build();
//---------------------------------------------------------------------------------------
//------------------------------Method-II-----------------------------------------------
using var host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) =>
{
var configuration = context.Configuration;
var options = configuration
.GetSection("GreetingService")
.Get<GreetingServiceOptions>();
services.Configure<GreetingServiceOptions>(config =>
{
options.From = config.From;
options.CallBackAction = delegate (string callBackmessage)
{
Console.WriteLine(callBackmessage);
};
});
services.AddSingleton<IGreetingService, GreetingService>();
}).Build();
//---------------------------------------------------------------------------------------
var serviceGreeting = host.Services.GetRequiredService<IGreetingService>();
serviceGreeting.Greet("Thomas");
Console.ReadLine();
方法2配置了错误的实例
var host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) => {
var configuration = context.Configuration;
//Instance extracted from configuration
GreetingServiceOptions options = configuration.GetSection("GreetingService")
.Get<GreetingServiceOptions>();
//configure IOptions from the extracted instance
services.Configure<GreetingServiceOptions>(config => {
//NOTE: the config is being modified here
config.From = options.From;
config.CallBackAction = delegate (string callBackmessage) {
Console.WriteLine(callBackmessage);
};
});
services.AddSingleton<IGreetingService, GreetingService>();
}).Build();
我认为混乱来自于变量的命名。
查看以下编辑
var host = Host.CreateDefaultBuilder()
.ConfigureServices((context, services) => {
IConfiguration configuration = context.Configuration;
//Instance extracted from configuration
GreetingServiceOptions greetingsConfig = configuration.GetSection("GreetingService")
.Get<GreetingServiceOptions>();
services.Configure<GreetingServiceOptions>(options => {
options.From = greetingsConfig.From;
options.CallBackAction = delegate (string callBackmessage) {
Console.WriteLine(callBackmessage);
};
});
services.AddSingleton<IGreetingService, GreetingService>();
}).Build();
你的属性分配应该是相反的
services.Configure<GreetingServiceOptions>(config =>
{
config.From = options.From;
config.CallBackAction = delegate (string callBackmessage)
{
Console.WriteLine(callBackmessage);
};
});