gpt4 book ai didi

c# - 我可以获取触发器本身之外的函数名称吗?

转载 作者:行者123 更新时间:2023-12-03 02:22:04 24 4
gpt4 key购买 nike

我想在运行时获取执行函数的名称。当我将一些数据存储到数据库时,我想添加函数名称,例如“此文档最后由函数'xxx'修改”。我的函数应用程序有多个函数,每个函数都可以修改同一组文档。显而易见的想法是依赖于 ExecutionContext,但是当我在触发器之外时我无法得到它。

The dependency injection container only holds explicitly registered types. The only services available as injectable types are what are setup in the Configure method. As a result, Functions-specific types like BindingContext and ExecutionContext aren't available during setup or as injectable types.

https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection#caveats

我基本上需要 Microsoft.Azure.WebJobs.ExecutionContext.FunctionName 的值。

    public class TriggerFunction
{
private readonly MyService myService;

public TriggerFunction(MyService myService)
{
this.myService = myService ?? throw new ArgumentNullException(nameof(myService));
}

[FunctionName("TriggerFunction")]
public void Run([TimerTrigger("0/15 * * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
myService.Foo();
}
}

public class MyService
{
public MyService(ILogger<MyService> logger)
{
Logger = logger ?? throw new ArgumentNullException(nameof(logger));
// would be nice to have ExecutionContext here
}

public void Foo()
{
Logger.LogInformation("Foo");
// How do I get "TriggerFunction" here without passing it explicitly as a parameter?
}
public ILogger<MyService> Logger { get; }
}

public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddScoped<MyService>();
}
}

我们正在使用进程内运行的 .net core 3.1。

最佳答案

您的选择之一是使用Activity 类。 Azure Function 运行时使用它来跟踪操作/检测。您可以使用 Activty.Current.AddBaggage 添加您自己的信息。此外,事件包含的信息通过流程内的异步调用流动,因此也可以安全地在基于任务的场景中使用。

    public class TriggerFunction
{
private readonly MyService myService;

public TriggerFunction(MyService myService)
{
this.myService = myService ?? throw new ArgumentNullException(nameof(myService));
}

[FunctionName("TriggerFunction")]
public void Run([TimerTrigger("0/15 * * * * *")]TimerInfo myTimer, ILogger log, ExecutionContext context)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

Activity.Current?.AddBaggage("functionName", context.FunctionName);

myService.Foo();
}
}

public class MyService
{
public MyService(ILogger<MyService> logger)
{
Logger = logger ?? throw new ArgumentNullException(nameof(logger));
// would be nice to have ExecutionContext here
}

public void Foo()
{
Logger.LogInformation($"Foo calling in Function { Activity.Current?.GetBaggageItem("functionName") }");
// How do I get "TriggerFunction" here without passing it explicitly as a parameter?
}
public ILogger<MyService> Logger { get; }
}

public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddScoped<MyService>();
}
}

要深入了解 Activity 类,请参阅 this blogpost

关于c# - 我可以获取触发器本身之外的函数名称吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68480242/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com