|
4 | 4 | using System;
|
5 | 5 | using System.Collections.Generic;
|
6 | 6 | using System.Text;
|
7 |
| -using Autofac; |
8 |
| -using Autofac.Extensions.DependencyInjection; |
9 | 7 | using Microsoft.Extensions.DependencyInjection;
|
| 8 | +using Microsoft.JSInterop; |
10 | 9 | using Xunit;
|
11 | 10 |
|
12 | 11 | namespace Microsoft.AspNetCore.Components.Hosting
|
@@ -81,42 +80,69 @@ public void ConfigureServices(IServiceCollection services)
|
81 | 80 | }
|
82 | 81 |
|
83 | 82 | [Fact]
|
84 |
| - public void HostBuilder_ServiceFactory_Autofac() |
| 83 | + public void HostBuilder_CanCustomizeServiceFactory() |
85 | 84 | {
|
86 | 85 | // Arrange
|
87 | 86 | var builder = new WebAssemblyHostBuilder();
|
88 |
| - builder.ConfigureServices((c, s) => s.AddSingleton<IServiceProviderFactory<IServiceCollection>>(new MyServiceProviderFactory())); |
| 87 | + builder.ConfigureServices((c, s) => |
| 88 | + { |
| 89 | + s.AddSingleton<IServiceProviderFactory<IServiceCollection>>( |
| 90 | + new TestServiceProviderFactory()); |
| 91 | + }); |
89 | 92 |
|
90 | 93 | // Act
|
91 | 94 | var host = builder.Build();
|
92 | 95 |
|
93 | 96 | // Assert
|
94 |
| - Assert.IsType<AutofacServiceProvider>(host.Services); |
| 97 | + Assert.IsType<TestServiceProvider>(host.Services); |
95 | 98 | }
|
96 | 99 |
|
97 |
| - private class MyServiceProviderFactory : IServiceProviderFactory<IServiceCollection> |
| 100 | + private class TestServiceProvider : IServiceProvider |
98 | 101 | {
|
99 |
| - public IServiceCollection CreateBuilder(IServiceCollection services) |
| 102 | + private readonly IServiceProvider _underlyingProvider; |
| 103 | + |
| 104 | + public TestServiceProvider(IServiceProvider underlyingProvider) |
100 | 105 | {
|
101 |
| - return new MyServiceCollection(services); |
| 106 | + _underlyingProvider = underlyingProvider; |
102 | 107 | }
|
103 | 108 |
|
104 |
| - public IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection) |
| 109 | + public object GetService(Type serviceType) |
105 | 110 | {
|
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 | + if (serviceType == typeof(IWebAssemblyHost)) |
| 112 | + { |
| 113 | + // Since the test will make assertions about the resulting IWebAssemblyHost, |
| 114 | + // show that custom DI containers have the power to substitute themselves |
| 115 | + // as the IServiceProvider |
| 116 | + return new WebAssemblyHost( |
| 117 | + this, _underlyingProvider.GetRequiredService<IJSRuntime>()); |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + return _underlyingProvider.GetService(serviceType); |
| 122 | + } |
111 | 123 | }
|
112 | 124 | }
|
113 | 125 |
|
114 |
| - private class MyServiceCollection : List<ServiceDescriptor>, IServiceCollection |
| 126 | + private class TestServiceProviderFactory : IServiceProviderFactory<IServiceCollection> |
115 | 127 | {
|
116 |
| - public MyServiceCollection(IEnumerable<ServiceDescriptor> collection) : base(collection) |
| 128 | + public IServiceCollection CreateBuilder(IServiceCollection services) |
117 | 129 | {
|
| 130 | + return new TestServiceCollection(services); |
118 | 131 | }
|
119 |
| - } |
120 | 132 |
|
| 133 | + public IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection) |
| 134 | + { |
| 135 | + Assert.IsType<TestServiceCollection>(serviceCollection); |
| 136 | + return new TestServiceProvider(serviceCollection.BuildServiceProvider()); |
| 137 | + } |
| 138 | + |
| 139 | + class TestServiceCollection : List<ServiceDescriptor>, IServiceCollection |
| 140 | + { |
| 141 | + public TestServiceCollection(IEnumerable<ServiceDescriptor> collection) |
| 142 | + : base(collection) |
| 143 | + { |
| 144 | + } |
| 145 | + } |
| 146 | + } |
121 | 147 | }
|
122 | 148 | }
|
0 commit comments