Azure / Azure/azure-functions-dotnet-worker

Attribute-based mechanism to hook before and after the trigger

Open
#1,949 2 comments 0 reactions 0 assignees View on GitHub
area: middleware needs-discussion
Dominant language
C#
Stars
466
Forks
215
Avg merge
3d 10h
Merged PRs (30d)
7

Description

# Summary

In #1811, I created a Pull-Request for the application of middleware using attributes. Here, I will explain the background of creating the Pull-Request and lay the groundwork for discussing the features necessary to meet the requirements.

# The features needed

I would like a mechanism to hook before and after a specific trigger. For example, I would like to do the following.

- Authenticate API keys at the time of executing HTTP requests
- Embed logs before and after the process
- Load cache and/or customer data (etc.) before executing the trigger

While this feature can currently be realized using the `.UseWhen()` method, this method only allows filtering by specifying the function name as a string. The implementation to apply the middleware and the function to be applied need to be written in separate files, which presents challenges in terms of maintainability. Therefore, by applying a "declarative approach like the `ActionFilterAttribute` provided in ASP.NET Core", I believe we can achieve both maintainability and convenience. Currently, Azure Functions does not have a filter feature like ASP.NET Core, so I created PR #1811 as a substitute using middleware feature. Please refer to #1811 for how to use it and what it feels like.

# Points to discuss

The following comment was made regarding PR #1811.

1. There are concerns about the reflection approach being used to find the target function (@fabiocav)
1. Using attributes to apply middleware is not preferable as it can cause confusion (@kshyju)
1. The concept of `ActionFilterAttribute` might be good (@kshyju)

Given the above exchanges, I would appreciate it if we could discuss the following.

- The pros and cons of using attributes for a declarative approach
- Whether it's less confusing to make middleware applicable with attributes, or to add a new filter feature (similar to middleware, like ASP.NET Core)

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reading PR #1811 and the discussion around it, then inspect the existing UseWhen() mechanism. Compare the attribute-based middleware proposal with an ASP.NET Core-style filter, including the reflection and maintainability concerns raised in the issue. Done requires an agreed design and implementation scope; this issue currently requests discussion rather than a defined change.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, csharp
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.