Azure Timer 触发功能依赖注入问题
Azure Timer trigger finction Dependency Injection issue
我的 Timer azure Function 使用默认的 DI。它使用 Code+Test 在本地和 Azure 中正常运行 - Test/Run。
但是当它由计时器触发时,它会在注入的 属性.
上出现 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>();
.网络核心 3.1
Microsoft.Azure.Functions.Extensions 1.1.0
Microsoft.Extensions.Logging.Abstractions 3.1.12
Microsoft.NET.Sdk.Functions-3.0.11
下载后有效
Microsoft.Azure.Functions.Extensions 1.1.0 到 1.0.0
我的 Timer azure Function 使用默认的 DI。它使用 Code+Test 在本地和 Azure 中正常运行 - Test/Run。 但是当它由计时器触发时,它会在注入的 属性.
上出现 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>();
.网络核心 3.1
Microsoft.Azure.Functions.Extensions 1.1.0
Microsoft.Extensions.Logging.Abstractions 3.1.12
Microsoft.NET.Sdk.Functions-3.0.11
下载后有效 Microsoft.Azure.Functions.Extensions 1.1.0 到 1.0.0