小编chr*_*oli的帖子

使用ASP.NET Core 2.0将简单的Injector组件注入IHostedService

在ASP.NET Core 2.0中,有一种方法可以通过实现IHostedService接口来添加后台任务(请参阅https://docs.microsoft.com/en-us/aspnet/core/fundamentals/hosted-services?view=aspnetcore-2.0).通过本教程,我能够使它工作的方式是在ASP.NET Core容器中注册它.我的目标是从队列中读取消息并在后台处理作业; 消息将发布到队列(通过控制器操作),然后在定时间隔内在后台处理.

// Not registered in SimpleInjector
services.AddSingleton<IHostedService, MyTimedService>(); 
Run Code Online (Sandbox Code Playgroud)

当我将此注册放在ASP.NET Core容器中时,它会在应用程序启动时自动启动该过程.但是,当我在SimpleInjector中注册时,服务不会自动启动.我相信这是因为我们只使用MvcControllers和MvcViewComponents注册SimpleInjector容器:

// Wire up simple injector to the MVC components
container.RegisterMvcControllers(app);
container.RegisterMvcViewComponents(app);
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是当我想开始从SimpleInjector注入组件寄存器(例如存储库,带有装饰器的通用处理程序......)到IHostedService的实现中时,如下所示:

public class TimedService : IHostedService, IDisposable
{
    private IJobRepository _repo;
    private Timer _timer;

    public TimedService(IJobRepository repo)
    {
        this._repo = repo;
    }
    ...
    ...
    ...
}
Run Code Online (Sandbox Code Playgroud)

由于IHostedService在ASP.NET Core而非Simple Injector中注册,因此在运行定时后台服务时收到以下错误:

未处理的异常:System.InvalidOperationException:尝试激活"Optimization.API.BackgroundServices.TimedService"时,无法解析类型"Optimization.Core.Interfaces.IJobRepository"的服务.

所以我的问题是,在Simple Injector中实现后台任务的最佳方法是什么?这是否需要一个独立的集成包而不是标准的MVC集成?我怎样才能将我的Simple Injector注册注入IHostedService?如果我们可以在Simple Injector中注册后自动启动服务,我认为这样可以解决这个问题.

感谢您在此提出任何建议以及有关此主题的任何建议!我可能做错了什么.在过去的一年里,我非常喜欢使用Simple Injector.

c# dependency-injection simple-injector asp.net-core-2.0

10
推荐指数
1
解决办法
2648
查看次数

Function App 未读取 Azure Functions v4 中包含用户机密的 local.settings.json 文件

我环顾四周,很惊讶没有人问过这个问题,但我正在尝试将用户机密与我的 Azure Function 应用程序(.NET 6,v4)一起使用。该值永远不会填充“我从 Visual Studio 本地运行”。

Function1.cs

public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            var secret = Environment.GetEnvironmentVariable("mysecret");

            return new OkObjectResult($"Secret is: {secret}");
        }
    }
Run Code Online (Sandbox Code Playgroud)

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  }
}
Run Code Online (Sandbox Code Playgroud)

secrets.json

{
  "mysecret": "test1",
  "Values:mysecret": "test2"
}
Run Code Online (Sandbox Code Playgroud)

FunctionApp1.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <UserSecretsId>24c7ab90-4094-49a9-94ef-c511ac530526</UserSecretsId>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="5.0.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.1" />
  </ItemGroup>
  <ItemGroup> …
Run Code Online (Sandbox Code Playgroud)

.net c# visual-studio azure-functions .net-6.0

1
推荐指数
1
解决办法
6423
查看次数