Skip to content

Commit ca23a6a

Browse files
authored
#18 Change syntax to allow for a MultiTenant container. (#27)
* Deprecate UseLogger * Example with v3 * Fix build process * Fix documentation
1 parent 640bcee commit ca23a6a

File tree

8 files changed

+64
-25
lines changed

8 files changed

+64
-25
lines changed

.github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727

2828
- uses: actions/setup-dotnet@v2
2929
with:
30-
dotnet-version: '6.0.x'
30+
dotnet-version: '3.1.x'
3131

3232
- name: "Get Commit Date"
3333
uses: actions/github-script@0.3.0

Autofac.Extensions.DependencyInjection.AzureFunctions/ConfigurationExtensions.cs

+23-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,23 @@ public static class ConfigurationExtensions
2323
/// <param name="hostBuilder">An instance of <see cref="IFunctionsHostBuilder"/>.</param>
2424
/// <param name="configurationAction">Action on a <see cref="ContainerBuilder"/> that adds component registrations to the conatiner.</param>
2525
/// <returns>The IFunctionsHostBuilder.</returns>
26-
public static IFunctionsHostBuilder UseAutofacServiceProviderFactory(this IFunctionsHostBuilder hostBuilder, Action<ContainerBuilder> configurationAction = null)
26+
public static IFunctionsHostBuilder UseAutofacServiceProviderFactory(this IFunctionsHostBuilder hostBuilder, Action<ContainerBuilder> configurationAction = null) =>
27+
UseAutofacServiceProviderFactory(hostBuilder, (builder) =>
28+
{
29+
configurationAction?.Invoke(builder);
30+
var container = builder.Build();
31+
32+
return container;
33+
});
34+
35+
36+
/// <summary>
37+
/// Attatch the <see cref="AutofacServiceProvider"/> to the <see cref="IFunctionsHostBuilder"/> of the current function host.
38+
/// </summary>
39+
/// <param name="hostBuilder">An instance of <see cref="IFunctionsHostBuilder"/>.</param>
40+
/// <param name="configurationAction">Func on a <see cref="ContainerBuilder"/> that adds component registrations to the conatiner. You may return either a regular AutofacContainer or a MultiTenantContainer.</param>
41+
/// <returns>The IFunctionsHostBuilder.</returns>
42+
public static IFunctionsHostBuilder UseAutofacServiceProviderFactory(this IFunctionsHostBuilder hostBuilder, Func<ContainerBuilder, IContainer> configurationAction)
2743
{
2844
// Adding as a Singleton service will make sure post-registered
2945
// services like TelemetryClient will be in the scope.
@@ -34,9 +50,6 @@ public static IFunctionsHostBuilder UseAutofacServiceProviderFactory(this IFunct
3450
containerBuilder.Populate(hostBuilder.Services);
3551
containerBuilder.RegisterModule<LoggerModule>();
3652

37-
// Call the user code to configure the container
38-
configurationAction?.Invoke(containerBuilder);
39-
4053
IEnvironmentType = hostBuilder.Services.Where(s => s.ServiceType.Namespace == "Microsoft.Azure.WebJobs.Script").FirstOrDefault()?.ServiceType.Assembly.GetExportedTypes().Where(x => x.Name == "IEnvironment").FirstOrDefault();
4154
if (IEnvironmentType != null)
4255
{
@@ -58,7 +71,12 @@ public static IFunctionsHostBuilder UseAutofacServiceProviderFactory(this IFunct
5871
.ExternallyOwned()
5972
.InstancePerTriggerRequest();
6073

61-
var container = containerBuilder.Build();
74+
// Call the user code to configure the container
75+
var container = configurationAction?.Invoke(containerBuilder);
76+
77+
if (container == null)
78+
throw new InvalidOperationException($"Container cannot be null. Call `builder.Build()` and return either the main container or a multi-tenant container.");
79+
6280
return new AutofacContainer(container);
6381
});
6482

Autofac.Extensions.DependencyInjection.AzureFunctions/LoggerExtensions.cs

+2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ public static class LoggerExtensions
1717
/// <param name="hostBuilder">An instance of <see cref="IFunctionsHostBuilder"/>.</param>
1818
/// <param name="configure">The <see cref="ILoggingBuilder"/> configuration delegate.</param>
1919
/// <returns>The IFunctionsHostBuilder.</returns>
20+
[Obsolete("This may replace Azure Function host logger settings and disable telemetry. Prefer using host.json `logging` settings or `services.Configure<LoggerFilterOptions>()`.")]
2021
public static IFunctionsHostBuilder UseLogger(this IFunctionsHostBuilder hostBuilder, Action<ILoggingBuilder, IConfiguration> configure)
2122
{
2223
var configuration = hostBuilder.GetContext().Configuration;
2324

2425
hostBuilder.Services.AddLogging((config) => configure?.Invoke(config, configuration));
26+
2527
return hostBuilder;
2628
}
2729
}

README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class Startup : FunctionsStartup
4747
builder.UseAppSettings();
4848
}
4949

50-
private void ConfigureContainer(ContainerBuilder builder)
50+
private IContainer ConfigureContainer(ContainerBuilder builder)
5151
{
5252
builder
5353
.Register(activator =>
@@ -86,6 +86,17 @@ public class Startup : FunctionsStartup
8686
.AsImplementedInterfaces()
8787
.InstancePerTriggerRequest();
8888

89+
90+
91+
var appContainer = builder.Build();
92+
93+
// If you need a Multi-Tenant Container, use this code instead of plain appContainer;
94+
95+
// var multiTenant = new MultitenantContainer(tenantIdentificationStrategy, appContainer);
96+
// return multiTenant
97+
98+
return appContainer;
99+
89100
}
90101
}
91102

SampleAutofacFunction/Functions/Function3.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Microsoft.ApplicationInsights;
12
using Microsoft.Azure.WebJobs;
23
using Microsoft.Extensions.Logging;
34
using SampleAutofacFunction.Services;
@@ -11,12 +12,14 @@ public class Function3 : IDisposable
1112
{
1213
private readonly IService1 _service1;
1314
private readonly ILogger _logger;
15+
private readonly TelemetryClient telemetry;
1416
private readonly Guid _id = Guid.NewGuid();
1517

16-
public Function3(IService1 service1, ILogger logger)
18+
public Function3(IService1 service1, ILogger logger, TelemetryClient telemetry)
1719
{
1820
_service1 = service1;
1921
_logger = logger;
22+
this.telemetry = telemetry;
2023
_logger.LogWarning($"Creating {this}");
2124
}
2225

@@ -31,6 +34,7 @@ public async Task Run([TimerTrigger("0 */5 * * * *", RunOnStartup = true)] Timer
3134
await Task.Delay(2000);
3235
Activity.Current.AddTag("Test", "Hello World");
3336
_logger.LogInformation($"C# Timer trigger function processed.");
37+
telemetry.TrackEvent("C# Timer trigger function processed.");
3438
}
3539

3640
public override string ToString()

SampleAutofacFunction/SampleAutofacFunction.csproj

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net6.0</TargetFramework>
4-
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
3+
<TargetFramework>netcoreapp3.1</TargetFramework>
4+
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
55
</PropertyGroup>
66
<ItemGroup>
7-
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.20.0" />
8-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
9-
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="4.0.1" />
10-
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.0.12" />
7+
<PackageReference Include="Autofac.Multitenant" Version="6.0.0" />
8+
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.21.0" />
119
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.0.1" />
12-
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage.Queues" Version="5.0.1" />
13-
<PackageReference Include="Microsoft.Azure.WebJobs.Logging.ApplicationInsights" Version="3.0.33" />
14-
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.0" />
10+
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.1.0" />
1511
</ItemGroup>
1612
<ItemGroup>
1713
<ProjectReference Include="..\Autofac.Extensions.DependencyInjection.AzureFunctions\Autofac.Extensions.DependencyInjection.AzureFunctions.csproj" />

SampleAutofacFunction/Startup.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ class Startup : FunctionsStartup
1515
public override void Configure(IFunctionsHostBuilder builder)
1616
{
1717
builder
18-
// You can call UseLogger to change how logging is done on your app by code.
19-
.UseLogger(ConfigureLogger)
2018
// This is the required call in order to use autofac in your azure functions app
2119
.UseAutofacServiceProviderFactory(ConfigureContainer);
2220
}
@@ -34,7 +32,7 @@ public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder bu
3432
builder.UseAppSettings(true);
3533
}
3634

37-
private void ConfigureContainer(ContainerBuilder builder)
35+
private IContainer ConfigureContainer(ContainerBuilder builder)
3836
{
3937
builder
4038
.Register(activator =>
@@ -71,6 +69,14 @@ private void ConfigureContainer(ContainerBuilder builder)
7169
.AsImplementedInterfaces()
7270
.InstancePerTriggerRequest();
7371

72+
var appContainer = builder.Build();
73+
74+
// If you need a Multi-Tenant Container, use this code instead of plain appContainer;
75+
76+
// var multiTenant = new MultitenantContainer(tenantIdentificationStrategy, appContainer);
77+
// return multiTenant
78+
79+
return appContainer;
7480
}
7581
}
7682
}
+7-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
2-
"IsEncrypted": false,
3-
"Values": {
4-
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
5-
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
6-
}
2+
"IsEncrypted": false,
3+
"Values": {
4+
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
5+
6+
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
7+
"APPINSIGHTS_INSTRUMENTATIONKEY": "local-dev"
8+
}
79
}

0 commit comments

Comments
 (0)