Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将依赖项注入IHostedService(计划作业)中?_C#_Asp.net Core_Dependency Injection_Service - Fatal编程技术网

C# 如何将依赖项注入IHostedService(计划作业)中?

C# 如何将依赖项注入IHostedService(计划作业)中?,c#,asp.net-core,dependency-injection,service,C#,Asp.net Core,Dependency Injection,Service,目标:我想构建一个cronjob,用当前数据覆盖incache内存。这应该是一项单独的服务 我有一个名为TimedHostedService的IHostedService和一个名为ExampleService的定制服务。应该将示例注入TimedHostedService,以便它可以从ExampleService调用方法。 ExampleService应该是覆盖内存的唯一服务 问题:程序试图将ExampleService注入TimedHostedService时崩溃。出现以下错误消息 Aggreg

目标:我想构建一个cronjob,用当前数据覆盖incache内存。这应该是一项单独的服务

我有一个名为TimedHostedService的IHostedService和一个名为ExampleService的定制服务。应该将示例注入TimedHostedService,以便它可以从ExampleService调用方法。 ExampleService应该是覆盖内存的唯一服务

问题:程序试图将ExampleService注入TimedHostedService时崩溃。出现以下错误消息

AggregateException:无法构造某些服务(验证服务描述符“ServiceType:Microsoft.Extensions.Hosting.IHostedService Lifetime:Singleton ImplementationType:Example_Project.Backend.Job.TimedHostedService”时出错:无法使用Singleton“Microsoft.Extensions.Hosting.IHostedService”中的作用域服务“Example_Project.Services.IExampleService”。) Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable ServiceDescriptor,ServiceProviderOptions选项)

InvalidOperationException:验证服务时出错 描述符的服务类型:Microsoft.Extensions.Hosting.IHostedService 生存期:单例实现类型: 示例_Project.Backend.Job.TimedHostedService':无法使用 来自的作用域服务“Example_Project.Services.IExampleService” singleton“Microsoft.Extensions.Hosting.IHostedService”

代码

StartUp.cs

public void ConfigureServices(IServiceCollection services)
    {
        /* Add MemoryCache */
        services.AddMemoryCache();

        /* Add CronJob / Scheduled Job */
        services.AddHostedService<TimedHostedService>();

        /* Add Dependency Injection of ExampleService */
        services.AddScoped<IExampleService, ExampleService>();
}
TimedHostedService.cs

public interface IExampleService
{
    void SetExample();
    IInventoryArticle[] GetExamples();
}

public class ExampleService : IExampleService
{
    public Examples[] GetExamples()
    { return null; }

    public void SetExample()
    { }

}
public class TimedHostedService : IHostedService, IDisposable
    {
        private readonly ILogger<TimedHostedService> _logger;
        private Timer _timer;
        private readonly IInventoryService _inventoryService;

        public TimedHostedService(
            ILogger<TimedHostedService> logger,
            IInventoryService inventoryService)
        {
            _logger = logger;
            _inventoryService = inventoryService; /// Problem Child
        }
}
公共类TimedHostedService:IHostedService,IDisposable
{
专用只读ILogger\u记录器;
私人定时器;
私有只读inventoryService\u inventoryService;
公共时间后台服务(
ILogger记录器,
IInventoryService库存服务)
{
_记录器=记录器;
_inventoryService=inventoryService;///问题子级
}
}

如果您希望使用作用域服务,您需要自己创建一个作用域

private readonly-IServiceProvider服务提供商;
公共时间HostedService(IServiceProvider服务提供商)
{
this.serviceProvider=serviceProvider;
//...
}
公共异步任务StartAsync(CancellationToken CancellationToken)
{
使用(var scope=serviceProvider.CreateScope())
{
var…=scope.ServiceProvider.GetService();
//...
}
}

如果您希望使用作用域服务,您需要自己创建一个作用域

private readonly-IServiceProvider服务提供商;
公共时间HostedService(IServiceProvider服务提供商)
{
this.serviceProvider=serviceProvider;
//...
}
公共异步任务StartAsync(CancellationToken CancellationToken)
{
使用(var scope=serviceProvider.CreateScope())
{
var…=scope.ServiceProvider.GetService();
//...
}
}
您可以检查文档()以创建范围服务,此外,您可以在此线程中引用我的回复并使用包和Cron表达式创建计划作业。您可以检查文档()此外,要创建作用域服务,您可以在此线程中引用my reply并使用包和Cron表达式创建计划作业。