Reputation: 363
I try to apply DI into Azure Function by reading this blog. The injected service in Azure Function belongs to another project which uses Structuremap for DI. My problem is that I can not add Structuremap registries into Azure Function Startup. Here is an example of Startup class
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.Configure();
}
}
and I tried to write a ContainerConfigurator class (to add those registries)
public static class ContainerConfigurator
{
public static void Configure(this IServiceCollection services)
{
var container = new Container(config =>
{
config.Scan(s =>
{
s.LookForRegistries();
s.WithDefaultConventions();
s.TheCallingAssembly();
});
config.Populate(services);
});
return container;
}
}
and here is the Function class
public class FunctionX
{
private readonly IXService _xService;
public GetReports(IXService xService)
{
_xService = xService;
}
[FunctionName("GetX")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req, ILogger log)
{
log.LogInformation("Get x");
return await Task.FromResult(new OkObjectResult("OK"));
}
}
when I test the endpoint, I get an error about the issue to resolve the "IXService". I do not know exactly what I miss. Does anyone have any solution?
Upvotes: 0
Views: 293
Reputation: 5549
I think you need to explicitly register your service in startups:
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddScoped<IXService, XService>();
}
}
Even DI is supported in Azure function, but apparently it is little different from ASP.NET Core.
Upvotes: 0