Azure / Azure/azure-functions-dotnet-worker
ASP NET Core MVC to Azure functions without changing any code
- Dominant language
- C#
- Stars
- 466
- Forks
- 215
- Avg merge
- 3d 10h
- Merged PRs (30d)
- 7
Description
# Issue Transfer
This issue has been transferred from the Azure SDK for .NET repository, [#46382](https://github.com/Azure/azure-sdk-for-net/issues/46382).
### Please be aware that @sjuarezgx is the author of the original issue and include them for any questions or replies.
## Details
### Library name and version
Microsoft.Azure.Functions.Worker.Sdk 1.17.4
### Query/Question
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 Azure 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](https://learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide?tabs=windows#aspnet-core-integration). 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. I use that "ApplicationBuilder" to invoke the service controller registered.
So, I'm integrating the ASP Net core MVC with Azure functions, which I'm not sure if it will result in a performant solution (if it worked..)
Program.cs:
```csharp
var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureServices(services =>
{
services.AddControllers();
IMvcBuilder builder = services.AddMvc(option => option.EnableEndpointRouting = false);
services.AddSingleton(sp =>
{
var appBuilder = new ApplicationBuilder(sp);
appBuilder.UseRouting();
appBuilder.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
return appBuilder;
});
AzureFunctionsStartup.ConfigureServices(services);
})
```
Azure function code:
```csharp
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 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
### Environment
Azure Functions v4
NET 8
Isolated model
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the ASP.NET Core integration documentation linked in the issue, then inspect Program.cs and HttpTriggerHandler to understand the proposed controller proxy. The reported task-transition error and the lack of a defined supported approach make the outcome a design-level determination of whether controller reuse is possible without code changes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure, csharp
- Domain
- api, backend, cloud
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100