简体   繁体   English

从asp.net core 2.1中的控制器访问BackgroundService

[英]access BackgroundService from controller in asp.net core 2.1

I just need to access my BackgroundService from a controller. 我只需要从控制器访问我的BackgroundService Since BackgroundServices are injected with 因为BackgroundServices是注入的

services.AddSingleton<IHostedService, MyBackgroundService>()

How can I use it from a Controller class? 如何在Controller类中使用它?

最后,我在控制器中注入了IEnumerable<IHostedService> ,并按类型进行过滤: background.FirstOrDefault(w => w.GetType() == typeof(MyBackgroundService)

This is how I solved it: 这就是我解决它的方式:

public interface IHostedServiceAccessor<T> where T : IHostedService
{
  T Service { get; }
}

public class HostedServiceAccessor<T> : IHostedServiceAccessor<T>
  where T : IHostedService
{
  public HostedServiceAccessor(IEnumerable<IHostedService> hostedServices)
  {
    foreach (var service in hostedServices) {
      if (service is T match) {
        Service = match;
        break;
      }
    }
  }

  public T Service { get; }
}

Then in Startup : 然后在Startup

services.AddTransient<IHostedServiceAccessor<MyBackgroundService>, HostedServiceAccessor<MyBackgroundService>>();

And in my class that needs access to the background service... 在我班上需要访问后台服务......

public class MyClass
{
  private readonly MyBackgroundService _service;

  public MyClass(IHostedServiceAccessor<MyBackgroundService> accessor)
  {
    _service = accessor.Service ?? throw new ArgumentNullException(nameof(accessor));
  }
}

Add BackgroundService in ConfigureServices function: 在ConfigureServices函数中添加BackgroundService:

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


        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

Inject in the Controller: 在控制器中注入:

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
    private readonly IHostedService listenerService;

    public ValuesController(IHostedService listenerService)
    {
        this.listenerService = listenerService;
    }
}

I used BackgroundService to spin up multiple listeners for AWSSQS Listeners. 我使用BackgroundService来为AWSSQS Listener启动多个监听器。 If consumer wants to spin new listener then it could be done by POSTing to a Controller method (end point). 如果消费者想要旋转新的监听器,则可以通过POST到Controller方法(终点)来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 ASP.NET Core 2.1 中的计时器上运行 BackgroundService - How to run BackgroundService on a timer in ASP.NET Core 2.1 asp.NET Core timed BackgroundService 不能在 Controller 中使用 StopAsync() 停止 - asp.NET Core timed BackgroundService cannot be stopped in Controller with StopAsync() .NET 5 WindowsService with ASP.NET Core and BackgroundService - .NET 5 WindowsService with ASP.NET Core and BackgroundService 控制器ASP.net Core 2.1中的Jwt角色身份验证 - Jwt Role authentication in controller ASP.net core 2.1 ASP.NET Core 2.1 Razor 表单,发布未到达控制器 - ASP.NET Core 2.1 Razor Form, Post not reaching Controller 从ASP.NET Core中的ExceptionFilterAttribute访问Controller - Access Controller from ExceptionFilterAttribute in ASP.NET Core 从控制器ASP.NET Core 2.1到特定客户端的SignalR调用 - SignalR call to specific client from controller ASP.NET Core 2.1 返回自定义ValidationResult并从Asp.Net Core中的控制器访问它 - Return Custom ValidationResult and access it from controller in Asp.Net Core 无法从ASP.NET Core 2.1控制器获取IdentityUser数据 - Unable to get IdentityUser data from ASP.NET Core 2.1 controller 配置来自 controller 的连接字符串(ASP.NET Core MVC 2.1) - Configure connection string from controller (ASP.NET Core MVC 2.1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM