gpt4 book ai didi

azure - 使用非静态触发器创建 IFunctionProvider

转载 作者:行者123 更新时间:2023-12-02 06:57:09 30 4
gpt4 key购买 nike

我有一个 Azure Function 的 IFunctionProvider 实现,它为 /healthcheck 创建路由。我的函数元数据的入口点是一个名为 Somenamespace.TestTrigger.RenderHealthCheck 的方法。我的代码如下所示:

    public class TestTrigger
{
public static async Task<IActionResult> RenderHealthCheck(HttpRequest req)
{
return new OkObjectResult("OK");
}
}

public class HealthCheckTrigger : IFunctionProvider
{
public ImmutableDictionary<string, ImmutableArray<string>> FunctionErrors { get; }

public async Task<ImmutableArray<FunctionMetadata>> GetFunctionMetadataAsync()
{
var assembly = Assembly.GetExecutingAssembly();

var functionMetadata = new FunctionMetadata()
{
Name = nameof(TestTrigger.RenderHealthCheck),
FunctionDirectory = null,
ScriptFile = $"assembly:{assembly.FullName}",
EntryPoint = $"{typeof(TestTrigger).FullName}.{nameof(TestTrigger.RenderHealthCheck)}",
Language = "DotNetAssembly"
};

var jo = JObject.FromObject(new HttpBindingMetadata()
{
Methods = new List<string> { HttpMethods.Get },
Route = "HealthCheck",
AuthLevel = AuthorizationLevel.Anonymous,
});

var binding = BindingMetadata.Create(jo);
functionMetadata.Bindings.Add(binding);

var functionMetadataList = new List<FunctionMetadata>
{
functionMetadata
};

return await Task.FromResult(functionMetadataList.ToImmutableArray()).ConfigureAwait(false);
}
}

这工作正常,当我运行该函数并点击 /HealthCheck 端点时,我收到了 OK 消息。但是,我希望使 RenderHealthCheck 方法成为非静态的,这样我就可以在构造函数中使用依赖项注入(inject)并访问应用程序中的各种服务。我将代码更改为:

public class TestTrigger
{
private readonly HealthCheckService healthCheckService;

public TestTrigger(HealthCheckService healthCheckService)
{
this.healthCheckService = healthCheckService;
}

public async Task<IActionResult> RenderHealthCheck(HttpRequest req)
{
return new OkObjectResult("OK");
}
}

现在,当我到达端点时,我收到以下异常:

Executed 'Functions.RenderHealthCheck' (Failed, Id=c02a9ba6-1ed3-4c07-8b38-214d345b6ff1, Duration=488ms)
Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.RenderHealthCheck
---> System.NullReferenceException: Object reference not set to an instance of an object.
at lambda_method(Closure , TestTrigger , Object[] )
at Microsoft.Azure.WebJobs.Host.Executors.TaskMethodInvoker`2.InvokeAsync(TReflected instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\TaskMethodInvoker.cs:line 21
at Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker.MethodInvoker`2.InvokeAsync(Object target, Object[] parameters) in D:\a\1\s\src\WebJobs.Script\Description\DotNet\DotNetFunctionInvoker.cs:line 533
at Microsoft.Azure.WebJobs.Script.Description.DotNetFunctionInvoker.InvokeCore(Object[] parameters, FunctionInvocationContext context) in D:\a\1\s\src\WebJobs.Script\Description\DotNet\DotNetFunctionInvoker.cs:line 272
at Microsoft.Azure.WebJobs.Script.Description.FunctionInvokerBase.Invoke(Object[] parameters) in D:\a\1\s\src\WebJobs.Script\Description\FunctionInvokerBase.cs:line 82
at Microsoft.Azure.WebJobs.Script.Description.FunctionGenerator.Coerce[T](Task`1 src) in D:\a\1\s\src\WebJobs.Script\Description\FunctionGenerator.cs:line 225
at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.InvokeAsync(Object instance, Object[] arguments) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs:line 52
at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.InvokeWithTimeoutAsync(IFunctionInvoker invoker, ParameterHelper parameterHelper, CancellationTokenSource timeoutTokenSource, CancellationTokenSource functionCancellationTokenSource, Boolean thro
wOnTimeout, TimeSpan timerInterval, IFunctionInstance instance) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 555
at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithWatchersAsync(IFunctionInstanceEx instance, ParameterHelper parameterHelper, ILogger logger, CancellationTokenSource functionCancellationTokenSource) in C:\projects\azure-webjobs-sdk-r
qm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 503
at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ExecuteWithLoggingAsync(IFunctionInstanceEx instance, FunctionStartedMessage message, FunctionInstanceLogEntry instanceLogEntry, ParameterHelper parameterHelper, ILogger logger, CancellationToken
cancellationToken) in C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs:line 281
--- End of inner exception stack trace --

我在 FunctionMetadataHttpBindingMetadata 中没有看到任何似乎控制入口点是否静态的内容。实例方法根本不支持吗?如果没有的话,这似乎是一个巨大的限制。 IFunctionProvider 上的任何类型的文档基本上都是零,因此不太确定从哪里开始。任何帮助将不胜感激!

最佳答案

如果您确实希望能够将 HealthCheckService 注入(inject)到静态函数中,可以为其创建自定义绑定(bind)并从 FunctionExecutionContext 解析服务。

基于https://microsoft.github.io/AzureTipsAndTricks/blog/tip247.html ,我能够执行以下操作:

[assembly: WebJobsStartup(typeof(HealthBindingStartup))]

[Binding]
[AttributeUsage(AttributeTargets.Parameter)]
public class HealthAttribute : Attribute
{
}

[Extension(nameof(HealthBinding))]
public class HealthBinding : IExtensionConfigProvider
{
public void Initialize(ExtensionConfigContext context)
{
var rule = context.AddBindingRule<HealthAttribute>();
rule.BindToInput((HealthAttribute attr, ValueBindingContext context) =>
{
var service = context.FunctionContext.CreateObjectInstance<HealthCheckService>();

return Task.FromResult(service);
});
}
}

public static class HealthBindingExtension
{
public static IWebJobsBuilder AddHealthBinding(this IWebJobsBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

builder.AddExtension<HealthBinding>();
return builder;
}
}

public class HealthBindingStartup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
builder.AddHealthBinding();
}
}

有了这个,您可以更新您的函数并添加[Health] HealthCheckService 服务,并让此绑定(bind)注入(inject)您想要的服务。

此绑定(bind)的元数据是:

{ "name": "service", "type": "health", "direction": "in" }

这是通过忽略任何属性的 IFunctionProvider 添加函数时所需要的。

关于azure - 使用非静态触发器创建 IFunctionProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69371155/

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