c# - 如何将 Nancy 与 Autofac 和 ASP.NET Core windows 服务集成
问题描述
让我尽量保持简单。我的最终目标是创建windows service
使用asp.net core 2.1
和Nancy
框架。
经过大量研究,我发现以下步骤
设置 Windows 服务 - https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/windows-service?view=aspnetcore-2.1
将 Nancy 连接到 .net 核心应用程序
问题陈述
我的 Windows 服务正在使用使用DI.net standard
开发的类库。Autofac
现在我如何整合Autofac
而不是Nancy's tiny IOC container
?
我尝试安装nuget
Install-Package Nancy.Bootstrappers.Autofac -Version 2.0.0-clinteastwood
并且,编写代码如下
protected override void ConfigureApplicationContainer(ILifetimeScope container)
{
_services.Configure<Settings>(_configuration.GetSection("MySettings"));
var deviceId = _configuration["Settings:DeviceId"];
var containerFactory = new ContainerFactory();
container.Update(builder =>
{
builder.RegisterInstance(containerFactory).As<IContainerFactory>();
containerBuilder.Register(c => new Engine(containerFactory));
containerBuilder.Register(i =>
new HubClient(deviceId, containerFactory))
.As<HubClient>().SingleInstance();
});
base.ConfigureApplicationContainer(container);
}
而且,在这里我想注入我的类实例。但如何?
public static void Main(string[] args)
{
var isService = !(Debugger.IsAttached || args.Contains("--console"));
var builder = CreateWebHostBuilder(args.Where(arg => arg != "--console").ToArray());
if (isService)
{
var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
var pathToContentRoot = Path.GetDirectoryName(pathToExe);
builder.UseContentRoot(pathToContentRoot);
}
var host = builder.Build();
if (isService)
{
//this line fails
IContainerFactory containerfactory = host.Services.GetService<IContainerFactory>();
host.RunAsCustomService();
}
else
{
host.Run();
}
}
如何获取/注入Engine和HubClient实例?
解决方案
推荐阅读
- kotlin - 如何在kotlin native中推迟一段代码的执行?
- jwt - 使用模拟时 jwt 令牌中的 resource_access 字段中缺少 clientId
- jenkins - 将 Jenkins 作业参数添加到所有作业
- aws-lambda - 错误 BadRequestException:您必须使用特定于客户的端点
- rename - 是否有允许将给定名称与别名匹配的代码策略?
- ag - 如何将一个 ag 搜索找到的文件用作第二个 ag 搜索的域?
- azure-sql-database - sp_execute_remote 有时无法返回 - 为什么?/ 解决方法
- kubernetes - 启用 istio 时访问 SMTP 服务器
- filtering - 我得到了一个数字带通滤波器——它是什么?
- flutter - 如何在颤动中选择多个日期?