【问题标题】:Azure Timer trigger finction Dependency Injection issueAzure Timer 触发函数依赖注入问题
【发布时间】:2021-06-03 05:32:58
【问题描述】:

My Timer azure Function 使用默认 DI。它使用 Code+Test - Test/Run 在本地和 Azure 中正常运行。 但是当它被计时器触发时,它会在注入的属性上出现 NullReferenceException 失败。

function.json:

{
  "generatedBy": "Microsoft.NET.Sdk.Functions-3.0.11",
  "configurationSource": "attributes",
  "bindings": [
    {
      "type": "timerTrigger",
      "schedule": "0 0 12 * * *",
      "useMonitor": true,
      "runOnStartup": false,
      "name": "myTimer"
    }
  ],
  "disabled": false,
  "scriptFile": "../bin/MyFunction.dll",
  "entryPoint": "MyFunctionApp.MyFunction.RunAsync"
}

功能:

 private readonly IMyService _myService;
 public MyFunction(IMyService myService)
 {
     _myService= myService;
 }

 [FunctionName("MyFunction")]
 public async Task RunAsync(
    [TimerTrigger("0 0 12 * * *")]
    TimerInfo myTimer,
    ILogger log)
 { ... }

服务:

 private readonly IMyClient client;

 public MyService(IMyClient _client)
 {
     client = _client;
 }
...
MyServiceMethod()
{
   client.MyClientMethod() <== it fails here
}

启动:

var myParams = ...;
builder.Services.AddTransient<IMyClient, MyClient>(provider =>
{
   return new MyClient(myParams);
});

builder.Services.AddTransient<IMyService, MyService>();

.Net Core 3.1

Microsoft.Azure.Functions.Extensions 1.1.0

Microsoft.Extensions.Logging.Abstractions 3.1.12

Microsoft.NET.Sdk.Functions-3.0.11

【问题讨论】:

  • 你在使用 ConfigureServices(IServiceCollection services) 方法吗?
  • 我使用 FunctionsStartup 类中的 void Configure(IFunctionsHostBuilder builder)。
  • 您是否在函数启动类之上使用 [assembly: FunctionsStartup(typeof(MyNamespace.Startup))] 属性?
  • @NAS 是的。我看到 configure() 运行正常。
  • @Xavr 您能否向我们展示完整的代码以用于调试目的?

标签: c# azure .net-core dependency-injection azure-functions


【解决方案1】:

降级后有效 Microsoft.Azure.Functions.Extensions 1.1.0 到 1.0.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-09
    • 2023-04-04
    • 2015-11-21
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    相关资源
    最近更新 更多