junalmeida / autofac-azurefunctions

Autofac implementation of the dependency injection factory for non isolated Azure Functions.
MIT License
20 stars 6 forks source link

Null Logger is being injected to constructor #30

Closed wpearson4 closed 1 year ago

wpearson4 commented 1 year ago

Hi, my constructor has a dependency on ILogger. The following type of ILogger is being injected {Microsoft.Extensions.Logging.Abstractions.NullLogger}. Currently nothing is logged.

junalmeida commented 1 year ago

Sorry but I don't have enough information to help you out with this. Please provide more information, like your startup code and class that is consuming the ILogger.

wpearson4 commented 1 year ago

` public class StartUp : FunctionsStartup { private IConfigurationRoot Configuration { get; set; }

    public override void Configure(IFunctionsHostBuilder builder)
    {
        builder.UseAutofacServiceProviderFactory(ConfigureContainer);
    }

    public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
    {
        var connectionString = Environment.GetEnvironmentVariable("AzureAppConfiguration");
        var config = new ConfigurationBuilder().AddAzureAppConfiguration(options =>
            options
                .Connect(connectionString)
                .Select(KeyFilter.Any, LabelFilter.Null)
                .Select(KeyFilter.Any, Environment.GetEnvironmentVariable("environment")
        )).AddEnvironmentVariables().Build();

        Configuration = config;
    }

    private IContainer ConfigureContainer(ContainerBuilder containerBuilder)
    {
        containerBuilder
       .RegisterAssemblyTypes(typeof(StartUp).Assembly)
       .InNamespace(typeof(StartUp).Namespace)
       .AsSelf() // Azure Functions core code resolves a function class by itself.
       .InstancePerTriggerRequest(); // Th
        // Register your services and dependencies here
        containerBuilder.RegisterInstance<IConfiguration>(Configuration);

        containerBuilder.RegisterType<CacheController>()
                .AsSelf()
                .SingleInstance()
                .OnActivating(x => x.Instance.RegisterClient(Configuration["connectionstring:rediscache:hot"], "rtls"));
        containerBuilder.RegisterType<DatabaseController>()
            .AsSelf()
            .SingleInstance()
            .OnActivating(x => x.Instance.RegisterClient(Configuration["connectionstring:mongodb:hot"]));
        containerBuilder.RegisterType<SerializationService>()
            .AsSelf()
            .SingleInstance();

        containerBuilder.RegisterType<BeaconIdentifierService>()
         .AsImplementedInterfaces()
         .SingleInstance();
        ConfigurationHelper.Initialize(Configuration);

        var appContainer = containerBuilder.Build();
        return appContainer;
    }
}`

Partial Class Declaration `namespace TrueSpot.LocationServices.TT521IngestionParser { public class FrameProcessor { private readonly ILogger _logger; private readonly CacheController _redisController; private readonly SerializationService _serializationService; private readonly IBeaconIdentifierService _beaconIdService;

    public FrameProcessor(ILogger logger, CacheController redisController, SerializationService serializationService, IBeaconIdentifierService beaconIdService)
    {
        _logger = logger;
        _redisController = redisController;
        _serializationService = serializationService;
        _beaconIdService = beaconIdService;
    }

    [FunctionName("Function1")]
    public async Task Run([EventHubTrigger("%parser:tt521:eventhubname%", Connection = "connectionstring:eventhub:ingest")] EventData[] events, 
       [ServiceBus("all-obd2-events", Connection = "connectionstring:servicebus:exhaust", EntityType = ServiceBusEntityType.Queue)] IAsyncCollector<ServiceBusMessage> deviceEvents)
    {`
junalmeida commented 1 year ago

What is the namespace of your Startup class?

wpearson4 commented 1 year ago

LocationServices.HealthReport

junalmeida commented 1 year ago

So according to your startup code, you are only registering to autofac services that are in the same namespace of Startup itself, but your function lives on a different namespace.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.