From ca23a6af6ecfaae64e9133a175d221b8048d2d0d Mon Sep 17 00:00:00 2001 From: Marcos Junior Date: Fri, 14 Oct 2022 09:30:29 -0500 Subject: [PATCH] #18 Change syntax to allow for a MultiTenant container. (#27) * Deprecate UseLogger * Example with v3 * Fix build process * Fix documentation --- .github/workflows/main.yml | 2 +- .../ConfigurationExtensions.cs | 28 +++++++++++++++---- .../LoggerExtensions.cs | 2 ++ README.md | 13 ++++++++- SampleAutofacFunction/Functions/Function3.cs | 6 +++- .../SampleAutofacFunction.csproj | 14 ++++------ SampleAutofacFunction/Startup.cs | 12 ++++++-- SampleAutofacFunction/local.settings.json | 12 ++++---- 8 files changed, 64 insertions(+), 25 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 55239df..fac6648 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -27,7 +27,7 @@ jobs: - uses: actions/setup-dotnet@v2 with: - dotnet-version: '6.0.x' + dotnet-version: '3.1.x' - name: "Get Commit Date" uses: actions/github-script@0.3.0 diff --git a/Autofac.Extensions.DependencyInjection.AzureFunctions/ConfigurationExtensions.cs b/Autofac.Extensions.DependencyInjection.AzureFunctions/ConfigurationExtensions.cs index 37630c3..dc26657 100644 --- a/Autofac.Extensions.DependencyInjection.AzureFunctions/ConfigurationExtensions.cs +++ b/Autofac.Extensions.DependencyInjection.AzureFunctions/ConfigurationExtensions.cs @@ -23,7 +23,23 @@ public static class ConfigurationExtensions /// An instance of . /// Action on a that adds component registrations to the conatiner. /// The IFunctionsHostBuilder. - public static IFunctionsHostBuilder UseAutofacServiceProviderFactory(this IFunctionsHostBuilder hostBuilder, Action configurationAction = null) + public static IFunctionsHostBuilder UseAutofacServiceProviderFactory(this IFunctionsHostBuilder hostBuilder, Action configurationAction = null) => + UseAutofacServiceProviderFactory(hostBuilder, (builder) => + { + configurationAction?.Invoke(builder); + var container = builder.Build(); + + return container; + }); + + + /// + /// Attatch the to the of the current function host. + /// + /// An instance of . + /// Func on a that adds component registrations to the conatiner. You may return either a regular AutofacContainer or a MultiTenantContainer. + /// The IFunctionsHostBuilder. + public static IFunctionsHostBuilder UseAutofacServiceProviderFactory(this IFunctionsHostBuilder hostBuilder, Func configurationAction) { // Adding as a Singleton service will make sure post-registered // services like TelemetryClient will be in the scope. @@ -34,9 +50,6 @@ public static IFunctionsHostBuilder UseAutofacServiceProviderFactory(this IFunct containerBuilder.Populate(hostBuilder.Services); containerBuilder.RegisterModule(); - // Call the user code to configure the container - configurationAction?.Invoke(containerBuilder); - IEnvironmentType = hostBuilder.Services.Where(s => s.ServiceType.Namespace == "Microsoft.Azure.WebJobs.Script").FirstOrDefault()?.ServiceType.Assembly.GetExportedTypes().Where(x => x.Name == "IEnvironment").FirstOrDefault(); if (IEnvironmentType != null) { @@ -58,7 +71,12 @@ public static IFunctionsHostBuilder UseAutofacServiceProviderFactory(this IFunct .ExternallyOwned() .InstancePerTriggerRequest(); - var container = containerBuilder.Build(); + // Call the user code to configure the container + var container = configurationAction?.Invoke(containerBuilder); + + if (container == null) + throw new InvalidOperationException($"Container cannot be null. Call `builder.Build()` and return either the main container or a multi-tenant container."); + return new AutofacContainer(container); }); diff --git a/Autofac.Extensions.DependencyInjection.AzureFunctions/LoggerExtensions.cs b/Autofac.Extensions.DependencyInjection.AzureFunctions/LoggerExtensions.cs index 8d7fc2b..6ede98f 100644 --- a/Autofac.Extensions.DependencyInjection.AzureFunctions/LoggerExtensions.cs +++ b/Autofac.Extensions.DependencyInjection.AzureFunctions/LoggerExtensions.cs @@ -17,11 +17,13 @@ public static class LoggerExtensions /// An instance of . /// The configuration delegate. /// The IFunctionsHostBuilder. + [Obsolete("This may replace Azure Function host logger settings and disable telemetry. Prefer using host.json `logging` settings or `services.Configure()`.")] public static IFunctionsHostBuilder UseLogger(this IFunctionsHostBuilder hostBuilder, Action configure) { var configuration = hostBuilder.GetContext().Configuration; hostBuilder.Services.AddLogging((config) => configure?.Invoke(config, configuration)); + return hostBuilder; } } diff --git a/README.md b/README.md index b56b644..7c9241c 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ public class Startup : FunctionsStartup builder.UseAppSettings(); } - private void ConfigureContainer(ContainerBuilder builder) + private IContainer ConfigureContainer(ContainerBuilder builder) { builder .Register(activator => @@ -86,6 +86,17 @@ public class Startup : FunctionsStartup .AsImplementedInterfaces() .InstancePerTriggerRequest(); + + + var appContainer = builder.Build(); + + // If you need a Multi-Tenant Container, use this code instead of plain appContainer; + + // var multiTenant = new MultitenantContainer(tenantIdentificationStrategy, appContainer); + // return multiTenant + + return appContainer; + } } diff --git a/SampleAutofacFunction/Functions/Function3.cs b/SampleAutofacFunction/Functions/Function3.cs index 14ad31f..23e7649 100644 --- a/SampleAutofacFunction/Functions/Function3.cs +++ b/SampleAutofacFunction/Functions/Function3.cs @@ -1,3 +1,4 @@ +using Microsoft.ApplicationInsights; using Microsoft.Azure.WebJobs; using Microsoft.Extensions.Logging; using SampleAutofacFunction.Services; @@ -11,12 +12,14 @@ public class Function3 : IDisposable { private readonly IService1 _service1; private readonly ILogger _logger; + private readonly TelemetryClient telemetry; private readonly Guid _id = Guid.NewGuid(); - public Function3(IService1 service1, ILogger logger) + public Function3(IService1 service1, ILogger logger, TelemetryClient telemetry) { _service1 = service1; _logger = logger; + this.telemetry = telemetry; _logger.LogWarning($"Creating {this}"); } @@ -31,6 +34,7 @@ public async Task Run([TimerTrigger("0 */5 * * * *", RunOnStartup = true)] Timer await Task.Delay(2000); Activity.Current.AddTag("Test", "Hello World"); _logger.LogInformation($"C# Timer trigger function processed."); + telemetry.TrackEvent("C# Timer trigger function processed."); } public override string ToString() diff --git a/SampleAutofacFunction/SampleAutofacFunction.csproj b/SampleAutofacFunction/SampleAutofacFunction.csproj index 3824b54..6c22929 100644 --- a/SampleAutofacFunction/SampleAutofacFunction.csproj +++ b/SampleAutofacFunction/SampleAutofacFunction.csproj @@ -1,17 +1,13 @@  - net6.0 - v4 + netcoreapp3.1 + v3 - - - - + + - - - + diff --git a/SampleAutofacFunction/Startup.cs b/SampleAutofacFunction/Startup.cs index 9f9ada7..cdde8fe 100644 --- a/SampleAutofacFunction/Startup.cs +++ b/SampleAutofacFunction/Startup.cs @@ -15,8 +15,6 @@ class Startup : FunctionsStartup public override void Configure(IFunctionsHostBuilder builder) { builder - // You can call UseLogger to change how logging is done on your app by code. - .UseLogger(ConfigureLogger) // This is the required call in order to use autofac in your azure functions app .UseAutofacServiceProviderFactory(ConfigureContainer); } @@ -34,7 +32,7 @@ public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder bu builder.UseAppSettings(true); } - private void ConfigureContainer(ContainerBuilder builder) + private IContainer ConfigureContainer(ContainerBuilder builder) { builder .Register(activator => @@ -71,6 +69,14 @@ private void ConfigureContainer(ContainerBuilder builder) .AsImplementedInterfaces() .InstancePerTriggerRequest(); + var appContainer = builder.Build(); + + // If you need a Multi-Tenant Container, use this code instead of plain appContainer; + + // var multiTenant = new MultitenantContainer(tenantIdentificationStrategy, appContainer); + // return multiTenant + + return appContainer; } } } diff --git a/SampleAutofacFunction/local.settings.json b/SampleAutofacFunction/local.settings.json index 4fce9ff..05bf9cb 100644 --- a/SampleAutofacFunction/local.settings.json +++ b/SampleAutofacFunction/local.settings.json @@ -1,7 +1,9 @@ { - "IsEncrypted": false, - "Values": { - "AzureWebJobsStorage": "UseDevelopmentStorage=true", - "FUNCTIONS_WORKER_RUNTIME": "dotnet" - } + "IsEncrypted": false, + "Values": { + "AzureWebJobsStorage": "UseDevelopmentStorage=true", + + "FUNCTIONS_WORKER_RUNTIME": "dotnet", + "APPINSIGHTS_INSTRUMENTATIONKEY": "local-dev" + } } \ No newline at end of file