Skip to content

Commit

Permalink
refactor to handle IActionResult
Browse files Browse the repository at this point in the history
  • Loading branch information
satvu committed Apr 11, 2023
1 parent 5099bac commit 4ada7e2
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Azure.Functions.Worker.Core.Http;
using Microsoft.Azure.Functions.Worker.Extensions.Http.AspNet;
using Microsoft.Extensions.Primitives;
Expand Down Expand Up @@ -33,7 +36,13 @@ public async Task Invoke(HttpContext context)

// TODO: Discuss whether we need to handle invocationId (a StringValues obj) being more than one string?
// Likely not since this info is sent from host which we control?
await _coordinator.SetContextAsync(invocationId, context);
FunctionContext functionContext = await _coordinator.SetContextAsync(invocationId, context);

if (functionContext.GetInvocationResult().Value is IActionResult actionResult)
{
ActionContext actionContext = new ActionContext(context, context.GetRouteData(), new ActionDescriptor());
await actionResult.ExecuteResultAsync(actionContext);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public DefaultHttpCoordinator()
_httpContextReferenceList = new ConcurrentDictionary<string, HttpContextReference>();
}

public Task SetContextAsync(string invocationId, HttpContext context)
public Task<FunctionContext> SetContextAsync(string invocationId, HttpContext context)
{
var httpContextRef = _httpContextReferenceList.AddOrUpdate(invocationId,
s => new HttpContextReference(invocationId, context), (s, c) =>
Expand All @@ -38,11 +38,11 @@ public Task<HttpContext> GetContextAsync(string invocationId, CancellationToken
}

// TODO:See about making this not public
public void CompleteInvocation(string functionId)
public void CompleteInvocation(string functionId, FunctionContext functionContext)
{
if (_httpContextReferenceList.TryGetValue(functionId, out var httpContextRef))
{
httpContextRef.CompleteFunction();
httpContextRef.CompleteFunction(functionContext);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next
await next(context);

// allows asp.net middleware to continue
_coordinator.CompleteInvocation(invocationId);
_coordinator.CompleteInvocation(invocationId, context);
}

private void AddHttpContextToFunctionContext(FunctionContext funcContext, HttpContext httpContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public static IHostBuilder ConfigureAspNetCoreIntegration(this IHostBuilder buil
});
});

builder.ConfigureServices(services =>
{
services.AddMvc();
});

return builder;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.Azure.Functions.Worker.Core.Http
{
internal class HttpContextReference
{
private TaskCompletionSource<HttpContext> _functionCompletionTask = new TaskCompletionSource<HttpContext>();
private TaskCompletionSource<FunctionContext> _functionCompletionTask = new TaskCompletionSource<FunctionContext>();
private TaskCompletionSource<HttpContext> _httpContextValueSource = new TaskCompletionSource<HttpContext>();
private string _invocationId;
private CancellationToken _token;
Expand All @@ -25,7 +25,7 @@ public HttpContextReference(string invocationId, HttpContext context)
_httpContextValueSource.SetResult(context);
}

public TaskCompletionSource<HttpContext> FunctionCompletionTask { get => _functionCompletionTask; set => _httpContextValueSource = value; }
public TaskCompletionSource<FunctionContext> FunctionCompletionTask { get => _functionCompletionTask; set => _functionCompletionTask = value; }

public TaskCompletionSource<HttpContext> HttpContextValueSource { get => _httpContextValueSource; set => _httpContextValueSource = value; }

Expand All @@ -34,7 +34,7 @@ internal void SetCancellationToken(CancellationToken token)
_token = token;
}

internal void CompleteFunction()
internal void CompleteFunction(FunctionContext functionContext)
{
if (_httpContextValueSource.Task.IsCompleted)
{
Expand All @@ -44,7 +44,7 @@ internal void CompleteFunction()
}
else
{
_functionCompletionTask.SetResult(_httpContextValueSource.Task.Result);
_functionCompletionTask.SetResult(functionContext);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ namespace Microsoft.Azure.Functions.Worker.Core.Http
{
internal interface IHttpCoordinator
{
public Task SetContextAsync(string functionId, HttpContext context);
public Task<FunctionContext> SetContextAsync(string functionId, HttpContext context);

public Task<HttpContext> GetContextAsync(string invocationId, CancellationToken cancellationToken);

public void CompleteInvocation(string invocationId);
public void CompleteInvocation(string invocationId, FunctionContext functionContext);
}
}

0 comments on commit 4ada7e2

Please sign in to comment.