是否可以在现有的ASPNET.Core Web项目中调用和/或使用Worker Service? [英] Can a Worker Service be called and/or used inside an existing ASPNET.Core web project?

查看:54
本文介绍了是否可以在现有的ASPNET.Core Web项目中调用和/或使用Worker Service?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读和学习.Net Core 3.0中提供的新Worker Service功能.我一直在使用Microsoft的此链接:

I've been reading and learing about the new Worker Service features provided in .Net Core 3.0. I have been using this link from Microsoft: Background tasks with hosted services in ASP.NET Core

我不明白的是,这些工作人员服务概念是否可以引入到现有的ASPNET Web项目(如Razor Pages网站)中?还是必须创建一个新项目,然后使用主机操作系统为此提供的任何机制将该项目作为服务部署?

What I don't understand is this, can these worker service concepts be introduced into an existing ASPNET Web Project, like a Razor Pages site? Or must you create a new project and then deploy that project as a service using whatever mechanism the host OS proivdes for that?

推荐答案

是的,您可以在ASP.NET Core应用程序中托管任意数量的托管服务( IHostedService ).在版本3中,ASP.NET Core使用通用主机( Host.CreateDefaultBuilder ),该主机是在应用程序启动时托管这些托管服务的框架.实际上,ASP.NET Core Web应用程序本身就是 IHostedService .

Yes, you can host any number of hosted services (IHostedService) within ASP.NET Core applications. With version 3, ASP.NET Core uses the generic host (Host.CreateDefaultBuilder) which is the framework that is hosting these hosted services when the application starts. In fact, the ASP.NET Core web application is an IHostedService itself.

要将其他托管服务添加到ASP.NET Core应用程序,只需在服务集合中注册其他托管服务,例如在Startup的 ConfigureServices 中:

To add additional hosted services to your ASP.NET Core application, just register additional hosted services with your service collection, e.g. within the Startup’s ConfigureServices:

services.AddHostedService<MyHostedService>();

当应用程序运行时,该服务将与ASP.NET Core Web服务器一起启动.

That service will then launch together with the ASP.NET Core web server when the application runs.

文档中提到的Worker SDK实际上是与ASP.NET Core应用程序一起使用的Web SDK的子集. Microsoft.NET.Sdk.Worker 基本上是 Microsoft.NET.Sdk.Web ,没有诸如Razor编译和 wwwroot 文件夹之类的特定于Web的东西.它基本上设置了自动文件遍历,例如用于 appsettings.json 并执行核心 Microsoft.NET.Sdk 所没有的其他一些有用的事情.

The Worker SDK that is mentioned in the documentation is actually a subset of the Web SDK that you are using with ASP.NET Core application. Microsoft.NET.Sdk.Worker is basically Microsoft.NET.Sdk.Web without the web-specific stuff like Razor compilation and wwwroot folder stuff. It basically sets up automatic file globbing e.g. for the appsettings.json and does some other useful things that the core Microsoft.NET.Sdk does not have.

这最终意味着,当您使用Web SDK时,您已经拥有Worker SDK提供的所​​有内容.因此,您无需仅指定Worker SDK即可承载其他后台服务.

Ultimately this means, that when you are using the Web SDK, then you already have everything the Worker SDK offers. So you do not need to specify the Worker SDK just to host additional background services.

这篇关于是否可以在现有的ASPNET.Core Web项目中调用和/或使用Worker Service?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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