|
1 | 1 | using System;
|
2 | 2 | using Microsoft.Extensions.DependencyInjection;
|
| 3 | +using Microsoft.Extensions.DependencyInjection.Extensions; |
| 4 | +using Microsoft.Maui.Hosting; |
3 | 5 | using Microsoft.Maui.Hosting.Internal;
|
4 | 6 | using Xunit;
|
5 | 7 |
|
@@ -99,6 +101,126 @@ public void CloneCanOverrideIncludeService()
|
99 | 101 | Assert.Same(obj2, second.Services.GetService<TestThing>());
|
100 | 102 | }
|
101 | 103 |
|
| 104 | + [Fact] |
| 105 | + public void MauiContextSupportsKeyedServices() |
| 106 | + { |
| 107 | + var collection = new ServiceCollection(); |
| 108 | + collection.AddKeyedTransient<IFooService, FooService>("foo"); |
| 109 | + collection.AddKeyedTransient<IFooService, FooService2>("foo2"); |
| 110 | + var services = collection.BuildServiceProvider(); |
| 111 | + |
| 112 | + var context = new MauiContext(services); |
| 113 | + |
| 114 | + var foo = context.Services.GetRequiredKeyedService<IFooService>("foo"); |
| 115 | + Assert.IsType<FooService>(foo); |
| 116 | + |
| 117 | + var foo2 = context.Services.GetRequiredKeyedService<IFooService>("foo2"); |
| 118 | + Assert.IsType<FooService2>(foo2); |
| 119 | + } |
| 120 | + |
| 121 | + [Fact] |
| 122 | + public void MauiContextSupportsKeyedServicesUsingAttributes() |
| 123 | + { |
| 124 | + var collection = new ServiceCollection(); |
| 125 | + collection.AddKeyedTransient<IFooService, FooService>("foo"); |
| 126 | + collection.AddKeyedTransient<IBarService, BarService>("bar"); |
| 127 | + collection.AddTransient<IFooBarService, FooBarKeyedService>(); |
| 128 | + var services = collection.BuildServiceProvider(); |
| 129 | + |
| 130 | + var context = new MauiContext(services); |
| 131 | + |
| 132 | + var foobar = context.Services.GetRequiredService<IFooBarService>(); |
| 133 | + var keyed = Assert.IsType<FooBarKeyedService>(foobar); |
| 134 | + Assert.NotNull(keyed.Foo); |
| 135 | + Assert.NotNull(keyed.Bar); |
| 136 | + } |
| 137 | + [Fact] |
| 138 | + public void NonKeyedProviderStaysNonKeyed() |
| 139 | + { |
| 140 | + var builder = MauiApp.CreateBuilder(useDefaults: false); |
| 141 | + builder.ConfigureContainer(new KeyedOrNonKeyedProviderFactory(false)); |
| 142 | + var mauiApp = builder.Build(); |
| 143 | + |
| 144 | + var context = new MauiContext(mauiApp.Services); |
| 145 | + |
| 146 | + Assert.IsAssignableFrom<IServiceProvider>(context.Services); |
| 147 | + Assert.IsNotAssignableFrom<IKeyedServiceProvider>(context.Services); |
| 148 | + |
| 149 | + var context2 = new MauiContext(context.Services); |
| 150 | + |
| 151 | + Assert.IsAssignableFrom<IServiceProvider>(context2.Services); |
| 152 | + Assert.IsNotAssignableFrom<IKeyedServiceProvider>(context2.Services); |
| 153 | + } |
| 154 | + |
| 155 | + [Fact] |
| 156 | + public void KeyedProviderStaysKeyed() |
| 157 | + { |
| 158 | + var builder = MauiApp.CreateBuilder(useDefaults: false); |
| 159 | + builder.ConfigureContainer(new KeyedOrNonKeyedProviderFactory(true)); |
| 160 | + var mauiApp = builder.Build(); |
| 161 | + |
| 162 | + var context = new MauiContext(mauiApp.Services); |
| 163 | + |
| 164 | + Assert.IsAssignableFrom<IServiceProvider>(context.Services); |
| 165 | + Assert.IsAssignableFrom<IKeyedServiceProvider>(context.Services); |
| 166 | + |
| 167 | + var context2 = new MauiContext(context.Services); |
| 168 | + |
| 169 | + Assert.IsAssignableFrom<IServiceProvider>(context2.Services); |
| 170 | + Assert.IsAssignableFrom<IKeyedServiceProvider>(context2.Services); |
| 171 | + } |
| 172 | + |
| 173 | + private class KeyedOrNonKeyedProviderFactory : IServiceProviderFactory<ServiceCollection> |
| 174 | + { |
| 175 | + public KeyedOrNonKeyedProviderFactory(bool keyed) |
| 176 | + { |
| 177 | + Keyed = keyed; |
| 178 | + } |
| 179 | + |
| 180 | + public bool Keyed { get; } |
| 181 | + |
| 182 | + public ServiceCollection CreateBuilder(IServiceCollection services) => |
| 183 | + new() { services }; |
| 184 | + |
| 185 | + public IServiceProvider CreateServiceProvider(ServiceCollection containerBuilder) |
| 186 | + { |
| 187 | + var real = containerBuilder.BuildServiceProvider(); |
| 188 | + return Keyed ? new KeyedProvider(real) : new NonKeyedProvider(real); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + private class NonKeyedProvider : IServiceProvider |
| 193 | + { |
| 194 | + public NonKeyedProvider(ServiceProvider provider) |
| 195 | + { |
| 196 | + Provider = provider; |
| 197 | + } |
| 198 | + |
| 199 | + public ServiceProvider Provider { get; } |
| 200 | + |
| 201 | + public object GetService(Type serviceType) => |
| 202 | + Provider.GetService(serviceType); |
| 203 | + } |
| 204 | + |
| 205 | + private class KeyedProvider : IServiceProvider, IKeyedServiceProvider |
| 206 | + { |
| 207 | + public KeyedProvider(ServiceProvider provider) |
| 208 | + { |
| 209 | + Provider = provider; |
| 210 | + } |
| 211 | + |
| 212 | + public ServiceProvider Provider { get; } |
| 213 | + |
| 214 | + public object GetKeyedService(Type serviceType, object serviceKey) => |
| 215 | + Provider.GetKeyedService(serviceType, serviceKey); |
| 216 | + |
| 217 | + public object GetRequiredKeyedService(Type serviceType, object serviceKey) => |
| 218 | + Provider.GetRequiredKeyedService(serviceType, serviceKey); |
| 219 | + |
| 220 | + public object GetService(Type serviceType) => |
| 221 | + Provider.GetService(serviceType); |
| 222 | + } |
| 223 | + |
102 | 224 | class TestThing
|
103 | 225 | {
|
104 | 226 | }
|
|
0 commit comments