Azure / azure-functions-dotnet-worker

Azure Functions out-of-process .NET language worker
MIT License
419 stars 182 forks source link

Microsoft.NET.Sdk.Web in Azure Functions Worker #778

Closed hkusulja closed 2 years ago

hkusulja commented 2 years ago

Hello, I am starting a new project which needs to run for a longer period, so I am trying to use the latest technology currently available. Project requires having the HTTP REST API. As I see this means: C# 10, .NET 6 and Azure Functions runtime v4 As per Azure Functions roadmap I will start with isolated proces (out of process) and this NuGet for Azure Worker.

However, I am confused comparing dotnet new webapi and this Azure Functions Worker.

I concluded start and basic difference in .csproj file having: <Project Sdk="Microsoft.NET.Sdk.Web"> compared to other online examples with Azure Worker: <Project Sdk="Microsoft.NET.Sdk">

I read the difference on .NET SDK

Using ASP.NET (Sdk.Web) has integrated handling of configuration, appsettings.json and lot of other things that are required for my project.

I also read the .NET isolated process guide

Please help me to have one folder/ .csproj project with Sdk.Web (and ASP.NET Web API) while still using Azure Functions Worker NuGet. And how to properly bind the controller / Azure Function HTTP Triger to ASP.NET WebAPI call ? How to use WebApplication.CreateBuilder instead HostBuilder(); in initial file (Program.cs) where I need to define dependency injection etc.?

Thank you

hkusulja commented 2 years ago

Maybe, I am totally misunderstanding, so please clarify the proper use and reason why is that. Thank you

WestDiscGolf commented 2 years ago

@hkusulja if I've understood your question properly what you're asking isn't currently the way it works AFAIK. If you do file>new in visual studio or create a new function app using the cli to create an isolated az func project you'll have access to a host builder in the program.cs. As for the httptrigger functions as they are now out of process you don't use aspnet core mvc style HttpRequest and HttpReponse objects and the ActionResult return types you have HttpRequestData and HttpReponseData for in bound and outbound. These have access to the request/response body, headers, cookies etc. You can't host api controllers in a functions app. Similar functionality but completely separate technologies. Sorry if I've miss understood your query 😄

hkusulja commented 2 years ago

@WestDiscGolf Thank you for your comment. Indeed, in a new project (VS Code + dotnet new cli), I do have HostBuilder for Azure Functions isolated.

I understand that those are two separate technologies, but I am trying to move from Azure App Service Web Sites, to Azure Functions, while still working with ASP.NET code and style. So it is possible to "connect"/bind those two - Azure Functions (isolated / Worker) with ASP.NET WebAPI ?

WestDiscGolf commented 2 years ago

@hkusulja No there isn't. The in process az functions used the ASP.NET Core nomenclature with the HttpTrigger to return IActionResult type constructs but these are not available in the out of process/isolated model.

What I would suggest is that you move over an action at a time into your new az func project, register the service layer/domain layer dependencies as required and create a new HttpTrigger function for that functionality. The services are registered in the ConfigureServices method on the HostBuilder in the same way as in the ASP.NET Core project - https://docs.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide#dependency-injection

The rest of the page is a good read as well tbh. I do appriciate it is quite confusing though. I too have found some of the information unclear or missing and I believe this is due to the nature of it still being relatively new and the amazing docs team can't keep up with all the rapid changes!

Also on that page - https://docs.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide#differences-with-net-class-library-functions - gives you a comparison about what the in process "thing" was and the corresponding isolated process construct.

Hope that helps and makes sense. Again if I've miss understood then let me know. This is all based on my own experience as I don't work for MS 😄

hkusulja commented 2 years ago

Yeah, I understand those links and information mostly. The thing is that we are doing isolated, since it is a future, but still writing and having API applications. We do need lot things, like copying appsettings.json etc. etc., so we have to manually write those (which are automatic by SDK.Web project type)

WinInsider commented 2 years ago

Is there any "architecture advantage" of hosting API endpoint in "Azure Functions" (via HTTP Triggers) instead of normal "Azure Web Site" with ASP.NET MVC Core framework?

hkusulja commented 2 years ago

We have moved from Azure App Service to Azure Functions, while having ASP.NET 6 WebAPI project, due to:

brettsam commented 2 years ago

@hkusulja -- I believe all of your questions were answered here, but in summary -- the dotnet-isolated worker does not expose any true HTTP endpoint, which means that ASP.NET classes and concepts do not translate 1:1. We explicitly expose our own HttpRequestData, class, for example, and do not use the HttpRequest from ASP.NET.

Ultimately, the dotnet-isolated worker is a console application that receives all events via grpc and routes them to your functions based on the supplied attributes.

fabiocav commented 2 years ago

Closing as answered based on the information shared above, but please let us know if you have any additional questions.