Search code examples
api.net-corecontrollerazure-functions.net-6.0

Can I call an Azure fuction from a API Controller using .NET 6?


I'm developing a project with Azure Functions. I created the Function app successfully, but my boss requires me to call the function from a controller because the frontend has to consume this function.

I have some questions about that:

  1. How should I call the function? I was searching but I did not find anything about the structure or examples where the people use a controller to call a function.

  2. Is this necessary? What advantage gives us call a function of this way?

  3. Is this useful for the frontend development team.

Thanks!

I'm expecting an explanation that can me focus in my task.


Solution

  • Can I call an Azure fuction from a API Controller using .NET 6?

    Yes, you can call Azure Function from a API Controller using .Net 6.

    You can use HttpClient for calling an Azure Function. Azure function URL is like any other URL which can be called using HttpClient and followed Microsoft-Document.

    using var httpClient = new HttpClient();
    var r = await httpClient.GetAsync("http://localhost:7116/api/Function1");
    var c = await r.Content.ReadAsStringAsync();
    

    enter image description here

    Here I used normal console you can do the same in api controller.

    Output:

    enter image description here

    Logs after execution:

    enter image description here

    Is this necessary? What advantage gives us call a function of this way?

    Yes, it helps as entire project starts at Controller API. Yes it also helps the developing teams as you will just call the controller and rest it does.