Azure / Azure/azure-functions-host
Question: Create Functions dynamically
- Dominant language
- C#
- Stars
- 2k
- Forks
- 482
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 38
Description
Hi,
I'm not certain whether this is the right place to ask, if not, please point me to in the right direction.
I find myself in a situation where I create a lot of functions that look like
```
public class GetTenants
{
readonly TenantHandler _handler;
public GetTenants(TenantHandler handler) => _handler = handler;
[FunctionName(nameof(GetTenants))]
public Task RunAsync(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "tenants")]
HttpRequest request) =>
_handler.GetAsync(request);
}
```
or
```
public class OnQueueMessage : AnOnQueueMessageFunction
{
public OnQueueMessage(IMessageReceiver receiver) : base(receiver, MetaDataConstants.QueueName) {}
[FunctionName(nameof(OnQueueMessage))]
public Task RunAsync([ServiceBusTrigger(MetaDataConstants.QueueName)]
Message message,
CancellationToken cancellationToken) =>
HandleAsync(message, cancellationToken);
}
```
As you can see I have already eliminated boilerplate as much as possible. However, because of the need to use attributes to register and bind the functions, I can go no further.
On the other hand, somehow the Functions runtime must detect these and register them for use internally. So one should think there must be an API that would allow me to say something like (pseudo-code):
```
runtime.Register(typeof(GetTenants), nameof(GetTenants))
.Method(f => f.RunAsync)
.UseHttpTrigger(AuthorizationLevel.Function, "get", Route="tenants");
runtime.Register(typeof(OnQueueMessage), nameof(OnQueueMessage))
.Method(f => f.RunAsync)
.UseServiceBusTrigger(MetaDataConstants.QueueName);
```
With some API like this, for the `OnQueueMesssage` Function which I use in several Function app projects (per microservice), I could just do the register line and even that I could refactor into a common module. And for stuff like `GetTenants`, I could probably generate them at runtime from other types I already go, again eliminating boilerplate.
Is there anything like that? Or if not, are you planning on exposing something of the kind?
Thanks a lot,
MR
Contributor guide
Research direction
The issue names no source files or tests; start by tracing the Functions runtime's attribute-based registration and binding behavior shown in the examples. Done means establishing whether dynamic registration is supported or planned, then documenting the answer and any concrete requirements.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure, csharp
- Domain
- backend, cloud
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100