|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | +using Shouldly; |
| 5 | +using Volo.Abp.Modularity; |
| 6 | +using Volo.Abp.Testing; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace Volo.Abp.Autofac; |
| 10 | + |
| 11 | +public class AutofacRegistration_Tests : AbpIntegratedTest<AutofacRegistration_Tests.TestModule> |
| 12 | +{ |
| 13 | + protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
| 14 | + { |
| 15 | + options.UseAutofac(); |
| 16 | + } |
| 17 | + |
| 18 | + [Fact] |
| 19 | + public void Should_Resolve_AnyKey_Keyed_Service_With_Any_Key() |
| 20 | + { |
| 21 | + // AnyKey registration should be resolvable with any key value. |
| 22 | + var serviceWithKeyA = GetRequiredKeyedService<IAnyKeyService>("keyA"); |
| 23 | + var serviceWithKeyB = GetRequiredKeyedService<IAnyKeyService>("keyB"); |
| 24 | + var serviceWithKeyC = GetRequiredKeyedService<IAnyKeyService>(42); |
| 25 | + |
| 26 | + serviceWithKeyA.ShouldNotBeNull(); |
| 27 | + serviceWithKeyB.ShouldNotBeNull(); |
| 28 | + serviceWithKeyC.ShouldNotBeNull(); |
| 29 | + |
| 30 | + serviceWithKeyA.ShouldBeOfType<AnyKeyServiceImpl>(); |
| 31 | + serviceWithKeyB.ShouldBeOfType<AnyKeyServiceImpl>(); |
| 32 | + serviceWithKeyC.ShouldBeOfType<AnyKeyServiceImpl>(); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public void Should_Pass_Correct_Key_To_Keyed_Factory() |
| 37 | + { |
| 38 | + var serviceA = GetRequiredKeyedService<IKeyedFactoryService>("alpha"); |
| 39 | + var serviceB = GetRequiredKeyedService<IKeyedFactoryService>("beta"); |
| 40 | + |
| 41 | + serviceA.Key.ShouldBe("alpha"); |
| 42 | + serviceB.Key.ShouldBe("beta"); |
| 43 | + } |
| 44 | + |
| 45 | + [Fact] |
| 46 | + public void Should_Not_Dispose_Instance_Registration_When_Scope_Disposed() |
| 47 | + { |
| 48 | + // Resolve the pre-registered singleton instance. |
| 49 | + var instance = GetRequiredKeyedService<IDisposableInstance>("instance"); |
| 50 | + instance.ShouldNotBeNull(); |
| 51 | + instance.IsDisposed.ShouldBeFalse(); |
| 52 | + |
| 53 | + // The same instance should be returned from a child scope. |
| 54 | + using (var scope = ServiceProvider.CreateScope()) |
| 55 | + { |
| 56 | + var scopedInstance = scope.ServiceProvider.GetRequiredKeyedService<IDisposableInstance>("instance"); |
| 57 | + scopedInstance.ShouldBeSameAs(instance); |
| 58 | + } |
| 59 | + |
| 60 | + // After the scope is disposed, the singleton instance should still be alive. |
| 61 | + instance.IsDisposed.ShouldBeFalse(); |
| 62 | + |
| 63 | + // It should also be the same static instance registered in the module. |
| 64 | + instance.ShouldBeSameAs(TestModule.DisposableInstanceForTest); |
| 65 | + } |
| 66 | + |
| 67 | + [Fact] |
| 68 | + public void Should_Resolve_Standard_Keyed_Services() |
| 69 | + { |
| 70 | + var big = GetRequiredKeyedService<ITypedCache>("big"); |
| 71 | + var small = GetRequiredKeyedService<ITypedCache>("small"); |
| 72 | + |
| 73 | + big.ShouldBeOfType<BigTypedCache>(); |
| 74 | + small.ShouldBeOfType<SmallTypedCache>(); |
| 75 | + |
| 76 | + big.Get("test").ShouldBe("big:test"); |
| 77 | + small.Get("test").ShouldBe("small:test"); |
| 78 | + } |
| 79 | + |
| 80 | + [DependsOn(typeof(AbpAutofacModule))] |
| 81 | + public class TestModule : AbpModule |
| 82 | + { |
| 83 | + public static DisposableInstance DisposableInstanceForTest { get; } = new(); |
| 84 | + |
| 85 | + public override void ConfigureServices(ServiceConfigurationContext context) |
| 86 | + { |
| 87 | + // AnyKey registration: this service can be resolved with any key. |
| 88 | + context.Services.AddKeyedTransient<IAnyKeyService, AnyKeyServiceImpl>( |
| 89 | + Microsoft.Extensions.DependencyInjection.KeyedService.AnyKey); |
| 90 | + |
| 91 | + // Keyed factory registration: the factory receives the actual key used for resolution. |
| 92 | + context.Services.Add(ServiceDescriptor.KeyedTransient<IKeyedFactoryService>( |
| 93 | + Microsoft.Extensions.DependencyInjection.KeyedService.AnyKey, |
| 94 | + (sp, key) => new KeyedFactoryServiceImpl(key))); |
| 95 | + |
| 96 | + // Instance registration with keyed service (ExternallyOwned should prevent Autofac from disposing it). |
| 97 | + context.Services.AddKeyedSingleton<IDisposableInstance>("instance", DisposableInstanceForTest); |
| 98 | + |
| 99 | + // Standard keyed type registrations. |
| 100 | + context.Services.AddKeyedTransient<ITypedCache, BigTypedCache>("big"); |
| 101 | + context.Services.AddKeyedTransient<ITypedCache, SmallTypedCache>("small"); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public interface IAnyKeyService |
| 106 | + { |
| 107 | + } |
| 108 | + |
| 109 | + public class AnyKeyServiceImpl : IAnyKeyService |
| 110 | + { |
| 111 | + } |
| 112 | + |
| 113 | + public interface IKeyedFactoryService |
| 114 | + { |
| 115 | + object Key { get; } |
| 116 | + } |
| 117 | + |
| 118 | + public class KeyedFactoryServiceImpl : IKeyedFactoryService |
| 119 | + { |
| 120 | + public object Key { get; } |
| 121 | + |
| 122 | + public KeyedFactoryServiceImpl(object key) |
| 123 | + { |
| 124 | + Key = key; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public interface IDisposableInstance |
| 129 | + { |
| 130 | + bool IsDisposed { get; } |
| 131 | + } |
| 132 | + |
| 133 | + public class DisposableInstance : IDisposableInstance, IDisposable |
| 134 | + { |
| 135 | + public bool IsDisposed { get; private set; } |
| 136 | + |
| 137 | + public void Dispose() |
| 138 | + { |
| 139 | + IsDisposed = true; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public interface ITypedCache |
| 144 | + { |
| 145 | + string Get(string key); |
| 146 | + } |
| 147 | + |
| 148 | + public class BigTypedCache : ITypedCache |
| 149 | + { |
| 150 | + public string Get(string key) => $"big:{key}"; |
| 151 | + } |
| 152 | + |
| 153 | + public class SmallTypedCache : ITypedCache |
| 154 | + { |
| 155 | + public string Get(string key) => $"small:{key}"; |
| 156 | + } |
| 157 | +} |
0 commit comments