c# - 无法绑定参数,导致绑定不支持参数类型(Azure Functions的HttpTrigger)
问题描述
我必须迁移单体的一部分才能独立运行迁移的部分,但我是天蓝色函数的新手。多个 HttpTrigger 包含不受支持的参数类型。( IMultiplierService
)
public static async Task<IActionResult> GetMultiplier( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "multipliers/{id:guid}")] HttpRequest req, string id, IMultiplierService multiplierService){ ... }
我在网上阅读并了解这string id
是对路线中的引用{id:guid}
,但我无法在网上找到作为参数给出的这种接口的目的是什么。
(IMultiplierService
是一个类似于 CRUD 的接口。包含像“GetById”或“GetAll”这样的方法。)
谁能解释如何支持这样一个自定义类作为 HttpTrigger Azure 函数的参数输入。
如果您有疑问或需要更多信息。前进。
解决方案
将类似 crud 的接口插入到 azure 函数中的正确方法是使用依赖注入。您不再需要创建静态函数。您需要做的就是在启动类中注册接口及其实现,以便 azure 函数运行时注入接口正确实现的实例。考虑以下使用 ISqlHelper 接口的 azure 函数示例。我编写我的非静态函数类如下
public class NotifyMember
{
private readonly ISqlHelper _sqlHelper;
public NotifyMember(ISqlHelper sqlHelper)
{
_sqlHelper = sqlHelper ?? throw new ArgumentNullException(nameof(sqlHelper));
}
[FunctionName(nameof(NotifyMember))]
public async Task Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "multipliers/{id:guid}")] HttpRequest req,
string id, ILogger logger)
{//Perform function work here}
}
我将在我的启动类中实现 ISqlHelper 的类实例注册为
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddTransient<ISqlHelper, SqlHelper>();
}
}
有关如何执行此操作的更多信息,请参阅Azure Functions 中的依赖注入
推荐阅读
- ruby-on-rails - 有人能告诉我这条线的含义吗?
- python - 检查文件线程的长度是否安全?
- docker - Docker 服务更新 docker-compose.yml 的所有部分
- angular - Angular Material mat-table cdk v9.2.0:无法绑定到“matHeaderRowDefPosition”,因为它不是“mat-header-row”的已知属性
- android - 如何将用户输入从 android 应用程序存储到 IBM Cloudant
- c - if 或语句在应该返回 true 时没有返回
- php - 如何使用 laravel Horizon 管理排队作业的优先级?
- android - 无法解析构造函数意图
- python - 基本问题:如何在 HTML 上正确地将数据从表单发送到 Flask。我究竟做错了什么?
- django - Django ManyToMany 关系避免重复