in src/WebJobs.Script.WebHost/Middleware/FunctionInvocationMiddleware.cs [99:159]
private async Task<IActionResult> GetResultAsync(HttpContext context, IFunctionExecutionFeature functionExecution)
{
if (functionExecution.Descriptor == null)
{
return new NotFoundResult();
}
if (context.Request.IsColdStart() && !context.Items.ContainsKey(ScriptConstants.AzureFunctionsColdStartKey))
{
// for cold start requests we want to measure the request
// pipeline dispatch time
// important that this stopwatch is started as early as possible
// in the pipeline (in this case, in our first middleware)
context.Items[ScriptConstants.AzureFunctionsColdStartKey] = ValueStopwatch.StartNew();
}
PopulateRouteData(context);
bool authorized = await AuthenticateAndAuthorizeAsync(context, functionExecution.Descriptor);
if (!authorized)
{
return new UnauthorizedResult();
}
// If the function is disabled, return 'NotFound', unless the request is being made with Admin credentials
if (functionExecution.Descriptor.Metadata.IsDisabled() &&
!AuthUtility.PrincipalHasAuthLevelClaim(context.User, AuthorizationLevel.Admin))
{
return new NotFoundResult();
}
if (functionExecution.CanExecute)
{
// Add the request to the logging scope. This allows the App Insights logger to
// record details about the request.
ILoggerFactory loggerFactory = context.RequestServices.GetService<ILoggerFactory>();
ILogger logger = loggerFactory.CreateLogger(functionExecution.Descriptor.LogCategory);
var scopeState = new Dictionary<string, object>()
{
[ScriptConstants.LoggerHttpRequest] = context.Request,
[ScriptConstants.AzureFunctionsRequestIdKey] = context.Request.GetRequestId(),
};
using (logger.BeginScope(scopeState))
{
await functionExecution.ExecuteAsync(context.Request, cancellationToken: context.RequestAborted);
if (context.Items.TryGetValue(ScriptConstants.AzureFunctionsDuplicateHttpHeadersKey, out object value))
{
logger.LogDebug($"Duplicate HTTP header from function invocation removed. Duplicate key(s): {value?.ToString()}.");
}
}
}
if (context.Items.TryGetValue(ScriptConstants.AzureFunctionsHttpResponseKey, out object result) && result is IActionResult actionResult)
{
return actionResult;
}
return new OkResult();
}