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

ASP NET Core MVC to Azure functions without changing any code #2546

Open
sjuarezgx opened this issue Sep 30, 2024 · 0 comments
Open

ASP NET Core MVC to Azure functions without changing any code #2546

sjuarezgx opened this issue Sep 30, 2024 · 0 comments
Labels

Comments

@sjuarezgx
Copy link

sjuarezgx commented Sep 30, 2024

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 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant