Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global Function Filters In v2.0.12050.0 (and above) #3401

Closed
oluatte opened this issue Sep 5, 2018 · 8 comments
Closed

Global Function Filters In v2.0.12050.0 (and above) #3401

oluatte opened this issue Sep 5, 2018 · 8 comments
Assignees

Comments

@oluatte
Copy link

oluatte commented Sep 5, 2018

Hey folks,

How do we use global function filters in the newest version of the runtime?

We did this in the past by using the Config property on the ExtensionConfigContext to get the IExtensionRegistry service. That Config property is now gone and our code does not compile (much less run).

What are we missing?

@ArtyomBaranovskiy
Copy link

ArtyomBaranovskiy commented Sep 10, 2018

@mayoatbm I'm not sure if it's well documented already, but it seems possible to add global filters to internal DI within custom Startup class Configure call:

[assembly: WebJobsStartup(typeof(Startup), "Test")]

namespace Test
{
    public class Startup : IWebJobsStartup
    {
        public void Configure(IWebJobsBuilder builder)
        {
            //previously I was registering global filters inside the custom extension config provider
            builder.AddExtension<ExtensionConfigProvider>();
            //now it's even easier to register it
            builder.Services.AddSingleton<IFunctionFilter, FunctionFilter>();
        }
    }
}

It's working like a charm when building and running locally (bin\extensions.json is generated correctly and contains my custom Startup type).
However, when trying to Pack & Publish to Azure Function App from Visual Studio 15.8.3, I'm observing the issue not having my custom Startup type added to the publish\bin\extensions.json automatically, so I apply the workaround described in #3386.

@oluatte
Copy link
Author

oluatte commented Oct 3, 2018

@ArtyomBaranovskiy Thanks for the heads up, that does work locally.

@mathewc @paulbatum The situation with Function Filters doesn't look good in GA and going forward. We now get the obsolete warnings message below.

function filters obsolete

We've built key parts of our function apps on Filters (e.g. Scope cleanup for DI as in Azure/azure-webjobs-sdk#1865 and Azure/azure-webjobs-sdk#1934) and we very much need the functionality they provide (being able to run code before and after function executions).

Can you confirm that you'll be replacing this functionality and not removing it entirely? And what would the timeline be?

@mathewc
Copy link
Member

mathewc commented Oct 3, 2018

We are not removing Filters - we applied Obsolete attribute to those APIs because unfortunately we didn't have time to finish the feature before we had to release the v3 SDK. The work remaining that we didn't have time to complete was to rationalize filters with all the new DI work that was done. I'm hoping we can get to this soon and finish the feature. See Azure/azure-webjobs-sdk#1284 for more details.

@oluatte
Copy link
Author

oluatte commented Oct 3, 2018

@mathewc That's good news. The Obsolete warning was alarming because of how much we've built on filters. I would suggest adding this clarification somewhere in the release notes, function filter docs or even using a different annotation.

Thanks!

@oluatte oluatte closed this as completed Oct 25, 2018
@jeevanbalanmanoj
Copy link

I’m looking to handle exceptions at a function app level for my http triggered azure functions similar to how it is done in dotnetcore apps with ‘ExceptionFilterAttribute’. My goal is to catch all uncaught exceptions in the application at a single place and prepare the http response for the error scenario. I see that there is ‘IFunctionExceptionFilter’ which has a method ‘OnExceptionAsync’. However I’m not sure how I can get the context in this method so that I can manipulate Response to provide the right error messages. Also I believe this can only be done at a function level and not at the function app level. I also came across the [ErrorHandler] attribute here. However I’m not sure how I can use it.
Any help with this will be great. Below is the code that works in asp.net core mvc. I want to achieve the same in azure functions

OnException method in ExceptionFilterAttribute

public override void OnException(ExceptionContext context)
{
Exception ex = context.Exception;
context.Result = ResponseHelper.GetErrorResponse(ex);
}

Get ErrorResponse method returns an object which implements IActionResult

@AvinashRamalingam
Copy link

I’m looking to handle exceptions at a function app level for my http triggered azure functions similar to how it is done in dotnetcore apps with ‘ExceptionFilterAttribute’. My goal is to catch all uncaught exceptions in the application at a single place and prepare the http response for the error scenario. I see that there is ‘IFunctionExceptionFilter’ which has a method ‘OnExceptionAsync’. However I’m not sure how I can get the context in this method so that I can manipulate Response to provide the right error messages. Also I believe this can only be done at a function level and not at the function app level. I also came across the [ErrorHandler] attribute here. However I’m not sure how I can use it.
Any help with this will be great. Below is the code that works in asp.net core mvc. I want to achieve the same in azure functions

OnException method in ExceptionFilterAttribute

public override void OnException(ExceptionContext context)
{
Exception ex = context.Exception;
context.Result = ResponseHelper.GetErrorResponse(ex);
}

Get ErrorResponse method returns an object which implements IActionResult

Can we get this feature implemented part of V3? This article https://social.msdn.microsoft.com/Forums/en-US/98a7c43a-eac1-42bf-9fcc-23e3e9fff34d/should-we-go-ahead-and-implement-functionfilters-in-azure-function-in-production?forum=AzureFunctions

says we can go ahead and implement Azure Functions Filter with all the already implemented features. Warns only for the feature which is still in progress.

Please clarrify! Thanks.

@rohitgkr
Copy link

rohitgkr commented Nov 6, 2019

I am looking for a confirmation as well. Can someone confirm this is threadsafe based on the following comment?
Azure/azure-webjobs-sdk#1934 (comment)

@iamalexmang
Copy link

+1
What is the status on Function Filters?

@ghost ghost locked as resolved and limited conversation to collaborators Jan 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants