1

I'm trying to use my own dependency injection container but my IJobActivator/IJobActivatorEx instance isn't getting called. When I create a timer function and have constructor parameters which are only registered through my DI container (and not the MS container) my JobActivator is not called and is unable to attempt to resolve those dependencies.

My code is structured as such:

public static async Task Main(string[] args)
{
    var builder = Host
      .CreateDefaultBuilder(args)
      .ConfigureFunctionsWebApplication()
      .ConfigureAppConfiguration(builder =>
      {
          builder.AddUserSecrets<Program>();
      })
      .ConfigureServices((appBuilder, services) =>
      {
          var configuration = appBuilder.Configuration;

          var connectionString = configuration.GetConnectionString("DefaultConnection");

          services.AddSingleton<IMyDiContainer>(provider => BuildContainer(provider, connectionString));
          services.Replace(ServiceDescriptor.Singleton<IJobActivator>(svc => new FunctionJobActivator(svc.GetRequiredService<IMyDiContainer>())));
          services.Replace(ServiceDescriptor.Singleton<IJobActivator>(svc => new FunctionJobActivator(svc.GetRequiredService<IMyDiContainer>())));
      });

    var host = builder.Build();

    await host.RunAsync();
}

public class FunctionJobActivator : IJobActivator, IJobActivatorEx
{
    private readonly IMyDiContainer Container;

    public FunctionJobActivator(IMyDiContainer container)
    {
        Container = container;
    }

    public T CreateInstance<T>()
    {
        return Container.Build<T>();
    }

    public T CreateInstance<T>(IFunctionInstanceEx functionInstance)
    {
        return Container.Build<T>();
    }
}

public class Function1
{
    private readonly MySpecialSystems Systems;

    public Function1(MySpecialSystems systems)
    {
        Systems = systems;
    }

    [Function("Function1")]
    public void Run([TimerTrigger("*/5 * * * * *", RunOnStartup = true)] TimerInfo myTimer)
    {
        var test = Systems.ToQuery().Take(10).ExecuteAll();
    }
}

I've looked at how DryIoc and Autofac do this but I can't replicate how they're actually getting it to work. Neither of which have very good examples.

I've tried creating my own ServiceProviderFactory and calling .UseServiceProviderFactory on the host builder.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.