【发布时间】: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