没有ASP.NET Core的.NET Core中的DI [英] DI in .NET Core without ASP.NET Core

查看:111
本文介绍了没有ASP.NET Core的.NET Core中的DI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用.NET Core编写一个守护进程,该进程的工作基本上类似于cron作业,并且仅按一定的时间间隔编排API / DB调用。因此,它不需要公开任何Web路由,因此不需要ASP.NET Core。

I'm looking to write a daemon process using .NET Core which will basically act much like a cron job and just orchestrate API/DB calls on some interval. As such, it has no need to expose any web routes, so there's no need for ASP.NET Core.

但是,afaik ASP.NET Core是您可以得到的地方。不错的 Startup 类,它可能需要您所有的DI管道和基于环境的配置。

However, afaik ASP.NET Core is where you get that nice Startup class with all the DI plumbing and environment-based configuration you might need.

我的查看方式,我有两个选择:

The way I see it, I have two options:


  1. 使用Forgo ASP.NET Core并自己连接DI框架。如果我走那条路线,该怎么办?

  2. 仅将DI。部分包含ASP.NET Core,但是然后如何生成在任何外部都永久运行的后台任务请求上下文?我的理解是,DI框架非常假设有某种传入请求来协调所有注入。


推荐答案


从.NET Core 2.1开始,可以/应该使用通用主机来完成。来自
.NET Core文档:

As of.NET Core 2.1 this can/should be done with a Generic Host. From .NET Core docs:

通用主机的目标是使HTTP管道与Web主机脱钩API可以实现更广泛的主机方案...

"The goal of the Generic Host is to decouple the HTTP pipeline from the Web Host API to enable a wider array of host scenarios..."

https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/host/generic-host?view= aspnetcore-2.1

public static async Task Main(string[] args)
{
  var builder = new HostBuilder()
    .ConfigureAppConfiguration((hostingContext, config) =>

    .ConfigureServices((hostContext, services) =>
    {
       // ...
    });

  await builder.RunConsoleAsync();
}

这篇关于没有ASP.NET Core的.NET Core中的DI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆