gpt4 book ai didi

Azure函数: Inherit HttpTrigger functions/routes from base class

转载 作者:行者123 更新时间:2023-12-02 06:36:18 24 4
gpt4 key购买 nike

我在许多 Azure Function 应用程序中拥有一些常见功能。每个 Function App 在每个 Function App 拥有的公共(public)资源上公开完全相同的查询操作。相同的端点、相同的依赖关系等。到目前为止,已经有大量的复制粘贴来维持这种情况。

因此,我将其重构为一个基本抽象类,该类位于所有服务使用的公共(public)项目中。它看起来像这样:

    public abstract class DeadLetterApiBaseFunction : BaseFunction
{
private readonly IDeadLetterBlobClient _deadLetterBlobClient;
private readonly string ROLE_READ;
private readonly string ROLE_WRITE;
private readonly ILogger _logger;

public DeadLetterApiBaseFunction(
ILogger logger,
IMiddlewareService middleware,
IDeadLetterBlobClient deadLetterBlobClient,
string ROLE_READ,
string ROLE_WRITE) : base(logger, middleware)
{
_logger = logger;
_deadLetterBlobClient = deadLetterBlobClient;
this.ROLE_READ = ROLE_READ;
this.ROLE_WRITE = ROLE_WRITE;
}

[FunctionName(nameof(GetDeadLetterBlobItemsForContainer))]
[ProducesResponseType(typeof(ResponsePayload<List<BlobItemWithContent>>), Status200OK)]
[ProducesResponseType(Status500InternalServerError)]
[ProducesResponseType(Status400BadRequest)]
[QueryStringParameter("container", "Name of the container", DataType = typeof(string))]
public async Task<ActionResult<ResponsePayload<List<BlobItemWithContent>>>> GetDeadLetterBlobItemsForContainer(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "v1/dead-letters/blobs-for-container")]
HttpRequest request)
{
// .... do stuff
}
}
}

这个想法是每个服务都会像这样子类化:

    public class DeadLetterApiFunction : DeadLetterApiBaseFunction
{
public DeadLetterApiFunction(
ILogger<DeadLetterApiFunction> logger,
IMiddlewareService middleware,
IDeadLetterBlobClient deadLetterBlobClient) : base(
logger: logger,
middleware: middleware,
deadLetterBlobClient: deadLetterBlobClient,
ROLE_READ: "blarg",
ROLE_WRITE: "blarg2")
{
}
}

看起来很棒吧?但是,当您运行该函数时,端点不会被公开 - 不会检测到该函数。

有没有办法获取从基类公开的 Azure Functions HttpTrigger 函数?或者只是不支持?我们使用 .NET Core 3.1 和 Azure Functions 3。

这里有一个相关的问题,但答案采取了不同的路径:

Use Azure Functions in abstract base class?

最佳答案

由于 HTTP 触发器是特定路由的单一入口点,因此我将在单独的类中抽象出 HTTP 触发器,然后在创建为单例时将其注入(inject)到 DeadLetterApiFunction 类中Startup.cs 文件。这是可能的,因为 Azure 函数支持实例函数来启用 D​​I。

public class HttpTrigger
{
public HttpTrigger()
{
}

[FunctionName(nameof(GetDeadLetterBlobItemsForContainer))]
[ProducesResponseType(typeof(ResponsePayload<List<BlobItemWithContent>>), Status200OK)]
[ProducesResponseType(Status500InternalServerError)]
[ProducesResponseType(Status400BadRequest)]
[QueryStringParameter("container", "Name of the container", DataType = typeof(string))]
public async Task<ActionResult<ResponsePayload<List<BlobItemWithContent>>>> GetDeadLetterBlobItemsForContainer(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "v1/dead-letters/blobs-for-container")]
HttpRequest request)
{
// .... do stuff
}
}

关于Azure函数: Inherit HttpTrigger functions/routes from base class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73669611/

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