Entity framework TimerTrigger不注入EF数据库上下文
我有一个使用实体框架(3.0.11)的Azure函数(v3) 我试图在Entity framework TimerTrigger不注入EF数据库上下文,entity-framework,dependency-injection,azure-functions,timer-trigger,Entity Framework,Dependency Injection,Azure Functions,Timer Trigger,我有一个使用实体框架(3.0.11)的Azure函数(v3) 我试图在TimerTrigger上运行代码,但是在计时器触发器中注入数据库似乎不起作用 下面是一些(快速匿名)代码示例 CSPROJ <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netcoreapp3.1</TargetFramework> <Azure
TimerTrigger
上运行代码,但是在计时器触发器中注入数据库似乎不起作用
下面是一些(快速匿名)代码示例
CSPROJ
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AzureFunctions.Extensions.DependencyInjection" Version="1.1.3" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.10" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
- 执行与前面相同的操作,但通过将
添加到方法签名会导致类似的错误消息,添加行IServiceProvider服务
db=services.GetRequiredService()如果无法注入,则代码>不相关
- 例如,有些变量似乎可以在这个范围内注入
,但似乎没有任何东西可以在该对象上使用ExecutionContext
[FunctionName("MY_FANCY_FUCNTION")]
public async Task Run([TimerTrigger("%TimerInterval%")] TimerInfo myTimer, ILogger ilog, ExecutionContext context)
{
ilog.LogInformation($"TIMER EXECUTED IS DB NULL? '{db == null}'");
// note that the key part of this DOES log out as NOT NULL
// which is what we want.
return;
await Main(ilog, context);
}
[FunctionName("MY_FANCY_FUCNTION")]
public async Task Run([TimerTrigger("%TimerInterval%")] TimerInfo myTimer, ILogger ilog, ExecutionContext context)
{
ilog.LogInformation($"TIMER EXECUTED IS DB NULL? '{db == null}'");
// note that the key part of this DOES log out as NOT NULL
// which is what we want.
return;
await Main(ilog, context);
}
@StevePy在我的问题下面的评论是正确的。您可以使timertrigger的RUN方法非静态,并利用注入的能力。我以前读过,这是不可能的,但似乎信息已经过时了 有关更多信息,请参阅此博客帖子: 或者获取以下示例代码,以便自己在本地运行:
[FunctionName("MY_FANCY_FUCNTION")]
public async Task Run([TimerTrigger("%TimerInterval%")] TimerInfo myTimer, ILogger ilog, ExecutionContext context)
{
ilog.LogInformation($"TIMER EXECUTED IS DB NULL? '{db == null}'");
// note that the key part of this DOES log out as NOT NULL
// which is what we want.
return;
await Main(ilog, context);
}
[FunctionName("MY_FANCY_FUCNTION")]
public async Task Run([TimerTrigger("%TimerInterval%")] TimerInfo myTimer, ILogger ilog, ExecutionContext context)
{
ilog.LogInformation($"TIMER EXECUTED IS DB NULL? '{db == null}'");
// note that the key part of this DOES log out as NOT NULL
// which is what we want.
return;
await Main(ilog, context);
}
请注意,为了在适当的时候给予评分,我会在Steve回答时将已接受的答案切换到Steve的答案,但我现在将标记为已回答,以确保该问题有一个已接受的答案。尝试使用非静态运行方法。许多示例使用静态方法,在没有依赖项且该方法是纯方法的情况下,可以推荐使用该方法。(因为函数方法应该力求纯粹)参见TimerTriggers/w DI示例。尝试使用非静态运行方法。许多示例使用静态方法,在没有依赖项且该方法是纯方法的情况下,可以推荐使用该方法。(因为函数方法应该力求纯粹)请参阅TimerTriggers/w DI示例。@StevePy-有趣!我所做的阅读说明timerfunction需要一个静态运行函数。这是一条新的尝试途径。“我马上就去。”斯蒂夫比——非常感谢。我很高兴地报告,它的工作如预期的那样。看起来像我读过的上一篇文章,其中建议timertrigger上的RUN方法必须是静态的,但它已经过时了。显然,它是有效的。如果你在下面写下你的评论作为回答,我很乐意为你接受。不用担心,这是一个有趣的问题,需要仔细检查一下。随着情况的变化,在线文档来源可能会有点过时,有时您需要适用于某个特定版本的信息,而最流行的内容是更新或旧版本的信息。:)如果内容没有清楚地标明版本甚至日期,情况就更糟了。
[FunctionName("MY_FANCY_FUCNTION")]
public async Task Run([TimerTrigger("%TimerInterval%")] TimerInfo myTimer, ILogger ilog, ExecutionContext context)
{
ilog.LogInformation($"TIMER EXECUTED IS DB NULL? '{db == null}'");
// note that the key part of this DOES log out as NOT NULL
// which is what we want.
return;
await Main(ilog, context);
}
[FunctionName("MY_FANCY_FUCNTION")]
public async Task Run([TimerTrigger("%TimerInterval%")] TimerInfo myTimer, ILogger ilog, ExecutionContext context)
{
ilog.LogInformation($"TIMER EXECUTED IS DB NULL? '{db == null}'");
// note that the key part of this DOES log out as NOT NULL
// which is what we want.
return;
await Main(ilog, context);
}