Reputation: 866
I have set up DI for an Azure function but it will not resolve when I run the function. The code I have is:
StartUp:
[assembly: FunctionsStartup(typeof(OmegaConnector.StartUp))]
namespace OmegaConnector
{
public class StartUp : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
builder.Services.AddLogging();
builder.Services.AddVehicleSearchCosmosDataProvider();
builder.Services.AddScoped<IProcessSearchData, SearchProcessor>(); <- This one
}
}
IProcessSearchData:
public interface IProcessSearchData
{
Task<bool> ProcessData(string campaign);
}
SearchProcessor:
public class SearchProcessor : IProcessSearchData
{
public async Task<bool> ProcessData(string campaign)
{
return true;
}
}
Function:
public OmegaConnectorFunction(ILogger<OmegaConnectorFunction> logger, IProcessSearchData searchProcessor)
{
I get the error:
Executed 'CatchCampaign' (Failed, Id=daef3371-fa4d-4d1f-abad-7ad343537872)
[27/05/2020 12:17:27] Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type 'OmegaConnector.Interfaces.IProcessSearchData' while attempting to activate 'OmegaConnector.OmegaConnectorFunction'.
Sorry if this is too simple but I just can't see what I have wrong here. I think I have this set up correctly but I obviously don't. Can anyone see what I need to do?
Upvotes: 1
Views: 137
Reputation: 2719
See here: https://learn.microsoft.com/en-us/azure/azure-functions/functions-dotnet-class-library
The Functions 3.x packages are built with .NET Core 3.1 in mind.
Try keeping these versions in sync so there are no dependency compatibility problems.
Upvotes: 1
Reputation: 2095
public class OmegaConnectorFunction { private readonly IProcessSearchData _searchProcessor; public OmegaConnectorFunction(IProcessSearchData searchProcessor) { _searchProcessor = searchProcessor; } [FunctionName("OmegaConnectorFunction")] public async Task<IActionResult> Run([HttpTrigger] HttpRequest request, ILogger log) // ILogger is automatically imported { var campaign = await request.Content.ReadAsAsync<string>(); _searchProcessor.ProcessData(campaign); return new OkResult(); } }
Upvotes: 0
Reputation: 56
From what I understood of the documentation provided by Microsoft the issue may be that the service needs to be injected into the class that contains the function.
I'm unsure if this is what you've done from the code examples you've provided. An example of this is:
public class OmegaConnectorFunction
{
private readonly ILogger _logger;
private readonly IProcessSearchData _searchProcessor;
public OmegaConnectorFunction(ILogger<OmegaConnectorFunction> logger, IProcessSearchData searchProcessor)
{
_logger = logger;
_searchProcessor = searchProcessor;
}
[FunctionName("OmegaConnectorFunction")]
public async Task<IActionResult> Run([HttpTrigger] HttpRequest request)
{
var campaign = await request.Content.ReadAsAsync<string>();
_searchProcessor.ProcessData(campaign);
return new OkResult();
}
}
Upvotes: 1