Azure-Functions
Azure-Functions copied to clipboard
ASP NET Core MVC to Azure functions without changing any code
Is there a tool or framework in the ASP.NET Core ecosystem that works identically to Spring's Cloud Function, allowing you to directly convert Controllers into Functions without the need to re-program them?
I have an API in ASP NET Core, with code like this:
[HttpGet] [Route("ListAirlines")] public async Task<ActionResult> gxep_listairlines( ) { //here my code }
I need to execute all my API as Azure functions without changing that code. I saw that now there is support for ASP NET Core integration with Azure functions. However, I do not see how to re-use my controllers code.
I tried creating a function "proxy" for all my controllers, by inyecting the ApplicationBuilder.
Program.cs:
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
services.AddControllers();
IMvcBuilder builder = services.AddMvc(option => option.EnableEndpointRouting = false);
services.AddSingleton<IApplicationBuilder>(sp =>
{
var appBuilder = new ApplicationBuilder(sp);
appBuilder.UseRouting();
appBuilder.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
return appBuilder;
});
AzureFunctionsStartup.ConfigureServices(services);
})
Azure function code:
public class HttpTriggerHandler
{
private ICacheService2 _redis;
private IApplicationBuilder _applicationBuilder;
public HttpTriggerHandler(IApplicationBuilder applicationBuilder, ICacheService2 redis)
{
_applicationBuilder = applicationBuilder;
if (redis != null && redis.GetType() == typeof(Redis))
_redis = redis;
}
public async Task<HttpResponseData> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "put", "delete", Route = "{*route}")] HttpRequestData
req,
string route, FunctionContext executionContext)
{
var httpResponseData = req.CreateResponse();
HttpContext httpAzureContextAccessor = new GXHttpAzureContextAccessor(req, httpResponseData, _redis);
await _applicationBuilder.Build().Invoke(httpAzureContextAccessor);
return httpResponseData;
}
}
}
But it doesn't work. It seems to be executing the service twice.. I have the following runtime error.
An attempt was made to transition a task to a final state when it had already completed