|
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
3 | 3 |
|
4 | 4 | using System; |
| 5 | +using System.Collections.Generic; |
5 | 6 | using System.Text; |
| 7 | +using Autofac; |
| 8 | +using Autofac.Extensions.DependencyInjection; |
6 | 9 | using Microsoft.Extensions.DependencyInjection; |
7 | 10 | using Xunit; |
8 | 11 |
|
@@ -76,5 +79,44 @@ public void ConfigureServices(IServiceCollection services) |
76 | 79 | services.AddSingleton<string>("foo"); |
77 | 80 | } |
78 | 81 | } |
| 82 | + |
| 83 | + [Fact] |
| 84 | + public void HostBuilder_ServiceFactory_Autofac() |
| 85 | + { |
| 86 | + // Arrange |
| 87 | + var builder = new WebAssemblyHostBuilder(); |
| 88 | + builder.ConfigureServices((c, s) => s.AddSingleton<IServiceProviderFactory<IServiceCollection>>(new MyServiceProviderFactory())); |
| 89 | + |
| 90 | + // Act |
| 91 | + var host = builder.Build(); |
| 92 | + |
| 93 | + // Assert |
| 94 | + Assert.IsType<AutofacServiceProvider>(host.Services); |
| 95 | + } |
| 96 | + |
| 97 | + private class MyServiceProviderFactory : IServiceProviderFactory<IServiceCollection> |
| 98 | + { |
| 99 | + public IServiceCollection CreateBuilder(IServiceCollection services) |
| 100 | + { |
| 101 | + return new MyServiceCollection(services); |
| 102 | + } |
| 103 | + |
| 104 | + public IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection) |
| 105 | + { |
| 106 | + Assert.IsType<MyServiceCollection>(serviceCollection); |
| 107 | + var containerBuilder = new ContainerBuilder(); |
| 108 | + containerBuilder.Populate(serviceCollection); |
| 109 | + var container = containerBuilder.Build(); |
| 110 | + return new AutofacServiceProvider(container); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private class MyServiceCollection : List<ServiceDescriptor>, IServiceCollection |
| 115 | + { |
| 116 | + public MyServiceCollection(IEnumerable<ServiceDescriptor> collection) : base(collection) |
| 117 | + { |
| 118 | + } |
| 119 | + } |
| 120 | + |
79 | 121 | } |
80 | 122 | } |
0 commit comments