I am trying to call an API inside an Azure HTTP Trigger. I have a .NET Core API that I need to call from an Azure Function. The API is working without an issue. When I am trying to implement the Trigger I am getting this error. Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type 'WebApplication1.Controllers.ContactsController' while attempting to activate 'FunctionApp1.Function1'.
.NET core API and Azure function are in the same solution but as different projects.
This is the implementation of trigger
namespace FunctionApp1
{
public class Function1
{
private readonly ContactsController _contacts;
public Function1(ContactsController _contacts)
{
this._contacts = _contacts;
}
[FunctionName("Function1")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
ILogger log)
{
var response = await _contacts.GetContacts();
return new OkObjectResult(response);
}
}
}
Let me know what is wrong with this. Thank you.
I tried the suggestion
namespace FunctionApp1
{
public class Function1
{
private readonly IHttpClientFactory _httpClientFactory;
public Function1(IHttpClientFactory httpClientFactory)
{
this._httpClientFactory = httpClientFactory;
}
[FunctionName("Function1")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
ILogger log)
{
var client = _httpClientFactory.CreateClient();
var response = await client.GetAsync("https://localhost:7209/api/Contacts");
return new OkObjectResult(response.ToString());
}
}
}
But now I am ending up with some response but not the response I want
{
"version": "1.1",
"content": {
"headers": [
{
"key": "Content-Type",
"value": [
"application/json; charset=utf-8"
]
}
]
},
"statusCode": 200,
"reasonPhrase": "OK",
"headers": [
{
"key": "Date",
"value": [
"Mon, 19 Dec 2022 18:59:13 GMT"
]
},
{
"key": "Server",
"value": [
"Kestrel"
]
},
{
"key": "Transfer-Encoding",
"value": [
"chunked"
]
}
],
"trailingHeaders": [],
"requestMessage": {
"version": "1.1",
"versionPolicy": 0,
"content": null,
"method": {
"method": "GET"
},
"requestUri": "https://localhost:7209/api/Contacts",
"headers": [],
"properties": {},
"options": {}
},
"isSuccessStatusCode": true
}