Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何安装\卸载插件中的 BackgroundService #5

Open
QJesus opened this issue Oct 14, 2019 · 2 comments
Open

如何安装\卸载插件中的 BackgroundService #5

QJesus opened this issue Oct 14, 2019 · 2 comments
Assignees
Labels
enhancement New feature or request
Projects

Comments

@QJesus
Copy link

QJesus commented Oct 14, 2019

比如在项目 DemoPlugin1 中添加一个自定义的 BackgroundService,目的是插件加载后就一秒输出一次当前时间,插件卸载时停止输出。

using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace DemoPlugin1
{
    public class Class1 : BackgroundService
    {
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                Console.WriteLine(DateTime.Now);
                await Task.Delay(1000);
            }
        }
    }
}

正常的开发方式,需要在 ConfigureServices() 方法中依赖注入

public void ConfigureServices(IServiceCollection services)
{
    services.AddHostedService<Class1>();
}

在插件中配置的 HostedService 要怎么才能运行\停止?

@lamondlu lamondlu added the enhancement New feature or request label Oct 14, 2019
@lamondlu lamondlu added this to To do in CoolCat Oct 14, 2019
@lamondlu
Copy link
Owner

这个示例现在暂时不支持HostService, 只支持简单的界面组件。
你的这个反馈不错,我后续把它考虑进来,写一篇
多谢支持

@lamondlu lamondlu self-assigned this Oct 18, 2019
@lamondlu lamondlu moved this from To do to In progress in CoolCat May 18, 2020
@f472918106
Copy link

MethodInfo method = typeof(ServiceCollectionHostedServiceExtensions).GetMethod("AddHostedService").MakeGenericMethod(t);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
CoolCat
  
In progress
Development

No branches or pull requests

3 participants