Reputation: 363
I want to inject instances of the objects directly in Azure function method/body like this:
[FunctionName("StartJob")]
public async Task<IActionResult> Start(
[HttpTrigger(AuthorizationLevel.Function, "Post", Route = "v1/job")] HttpRequest req,
IStartJobHandler handler)
{
...
But I got an runtime error when I do this:
The 'StartJob' function is in error: Microsoft.Azure.WebJobs.Host: Error indexing method 'StartJob'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'handler' to type IStartJobHandler. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
If I inject the same instance in constructor - everything is good. (So no need to suggest me check my startup class and so on :))
I don't want to inject in the constructor though. Because the instance belongs only for one specific function and the idea is to isolate the instances between functions.
So, the question is: can I actually inject custom classes in azure function method directly, without writing additional code, like ILogger for example:
[FunctionName("StartJob")]
public async Task<IActionResult> Start(
[HttpTrigger(AuthorizationLevel.Function, "Post", Route = "v1/job")] HttpRequest req,
ILogger logger)
{
...
Or, it's just not supported?
Thanks.
Upvotes: 3
Views: 1678
Reputation: 14080
Injecting custom class directly into Function is not possible, you still need to inject constructor to achieve your idea. If you don't choose to inject the constructor, the dependencies will be unavailable. Writing additional code is necessary. This is the right way:
Upvotes: 2