我正在尝试将基于.NET Framework的许多项目移植到.NET Core.这涉及移植许多类库以及使用这些库的顶级控制台/ Web应用程序.
我的部分要求是我的顶级应用程序应该支持基于插件的系统,我可以通过引用这些类库的不同子集来轻松地交换功能.我用过MEF来做这个.例如,我的一个ASP.NET核心Web应用程序涉及通过一个设备与设备通信ICommunicationService,我有不同的Nuget包导出该服务的不同实现:
[Export(typeof(ICommunicationService))]
[Shared]
public sealed class UsbCommunicationService : ICommunicationService
{
}
Run Code Online (Sandbox Code Playgroud)
目前,这些类库引用Common.Logging并将记录器实例化为只读静态字段:
[Export(typeof(ICommunicationService))]
[Shared]
public sealed class UsbCommunicationService : ICommunicationService
{
...
private static readonly ILog Log = LogManager.GetLogger<UsbCommunicationService>();
....
}
Run Code Online (Sandbox Code Playgroud)
我Log4Net在我的顶级应用程序中使用,并通过引用Common.Logging.Log4Net适配器来促进我的类库中的日志记录.
但是,我知道ASP.NET Core依赖于Microsoft的新日志抽象框架Microsoft.Extensions.Logging,并且ASP.NET Core应用程序应该设计为通过构造函数依赖注入记录器来支持日志记录,如下所示:
public class HomeController : Controller
{
private ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
_logger.LogInformation("Index action requested at {requestTime}", DateTime.Now);
return View(); …Run Code Online (Sandbox Code Playgroud) 我已经用过:
user-secret set "AWSAccessKey" "my access key"和
user-secret set ""AWSSecretKey" "my secret key"
让我的钥匙进入秘密商店.
我有这个课程,我将用它来通过Amazon SES发送电子邮件,但我不知道如何将我的密钥插入其中.
public class MailHelper
{
public void SendEmailSes(string senderAddress, string receiverAddress, string subject, string body)
{
using (var client = new AmazonSimpleEmailServiceClient(
new BasicAWSCredentials("[AWSAccessKey]", "[AWSSecretKey]"),
RegionEndpoint.USEast1))
{
var sendRequest = new SendEmailRequest
{
Source = senderAddress,
Destination = new Destination { ToAddresses = new List<string> { receiverAddress } },
Message = new Message
{
Subject = new Content(subject),
Body = new Body { Text …Run Code Online (Sandbox Code Playgroud)