无法绑定参数,因为绑定不支持参数类型(Azure Functions的HttpTrigger) [英] Cannot bind parameter, cause the parameter type is not supported by the binding (HttpTrigger of Azure Functions)
问题描述
我必须迁移整体的一部分才能独立运行迁移的部分,但是我在天蓝色函数中是新手.多个HttpTriggers包含不受支持的参数类型.( IMultiplierService
)
I have to migrate a part of a monolith to be able to run the migrated part independently, but i'm new in azure functions.
Multiple HttpTriggers contain an a unsupported parameter type. (IMultiplierService
)
公共静态异步任务< IActionResult>GetMultiplier([HttpTrigger(AuthorizationLevel.Anonymous,"get",Route ="multipliers/{id:guid}")] HttpRequest请求,字符串ID,IMultiplierService multiplierService){...}
我在线阅读并理解 string id
是对路由中 {id:guid}
的引用,但我无法在线找到目的是什么这样的接口作为参数给出.
I read online and understand that the string id
is a reference to the {id:guid}
in the route, but i could not find online what the purpose is of such an interface given as a parameter.
( IMultiplierService
是一个类似于CRUD的接口.包含诸如'GetById'或'GetAll'之类的方法.)
(IMultiplierService
is a CRUD like interface. Contains method like 'GetById' or 'GetAll'.)
谁能解释一下如何支持这种自定义类作为HttpTrigger Azure函数的参数输入.
Can anyone explain how to support such a custom class as parameter input for the HttpTrigger Azure Function.
如果您有疑问或需要更多信息.继续吧.
If you have questions or need more information. Go ahead.
推荐答案
将crud like接口插入到Azure函数中的正确方法是使用依赖注入.您不再需要创建静态函数.您需要做的就是在启动类中注册接口及其实现,以便azure函数运行时注入接口正确实现的实例.考虑以下使用ISqlHelper接口的Azure函数的示例.我将我的非静态函数类编写如下
The proper way to insert the crud like interface into the azure functions is to use dependency injection. You dont need to create static functions anymore. All you need to do is register the interface and its implementation in the startup class so that the azure functions runtime inject an instance of correct implementation of your interface. Consider following example of a azure function which uses a ISqlHelper interface. I write my non static function class as follows
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的类实例注册为
And I register my instance of class which implements ISqlHelper in my startup class as
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddTransient<ISqlHelper, SqlHelper>();
}
}
For more information on how to do it refer Dependency Injection in Azure Functions
这篇关于无法绑定参数,因为绑定不支持参数类型(Azure Functions的HttpTrigger)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!