我有以下应用程序设置.核心项目中的json:
"ABC": {
"Url": "someurl",
"Id": "as",
"sec": "bc",
"Username": "un",
"Password": "pw",
}
我创建了一个POCO:
public class ABC
{
public string Username { get; set; }
public string Password { get; set; }
}
这是我的节目.cs课程:
public class Program
{
public IConfiguration Configuration { get; }
public static void Main(string[] args)
{
var logger = LogManager.GetCurrentClassLogger();
try
{
CreateHostBuilder(args).Build().Run();
}
catch (OperationCanceledException oex)
{
logger.Error(oex, "Error during shutdown");
Environment.ExitCode = 3;
}
}
public void ConfigureServices(IServiceCollection services)
{
// Adds services required for using options.
services.AddOptions();
services.AddSingleton<ABC>();
// Registers the following lambda used to configure options.
services.Configure<ABC>(Configuration.GetSection("ABC"));
//register other services
services.AddSingleton<ABC>();
}
public static IHostBuilder CreateHostBuilder(string[] args)
var config = hostContext.Configuration;
// load ABC into an object
var ABC = config.GetSection("ABC").Get<ABC>();
ABC.AppSection = "ABC";
services.AddSingleton(ABC);
services.AddHostedService<Service>()
.Configure<EventLogSettings>(config =>
{
config.LogName = "Aname";
config.SourceName = "Asource";
});
})
.ConfigureLogging((options) =>
{
options.AddFilter<EventLogLoggerProvider>(level => level >= Microsoft.Extensions.Logging.LogLevel.Information);
options.AddNLog("some.log");
})
.UseWindowsService();
}
}
我想访问以下类中appsettings的值(在另一个项目中):
公共类OtherClass:SomeotherClass,SomeInterface
public OtherClass(ILogger<something> logger, IZXC ZXC)
: base(logger, ZXC)
{
}
public void DoRunDotNetPython(CancellationToken token, command zxc, List<NameValuePair> asd)
{
some code
//access the values here
}
我是个新手.net和任何形式的帮助将不胜感激.我也只添加了我认为相关的细节,如果我需要添加其他内容,请告诉我您可能需要的细节.