diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/AbpAspNetCoreComponentsWebAssemblyModule.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/AbpAspNetCoreComponentsWebAssemblyModule.cs index 707952f7c4..6664ec773a 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/AbpAspNetCoreComponentsWebAssemblyModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/AbpAspNetCoreComponentsWebAssemblyModule.cs @@ -1,4 +1,6 @@ -using Microsoft.Extensions.DependencyInjection; +using Microsoft.AspNetCore.Components; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Logging; using Volo.Abp.AspNetCore.Components.WebAssembly.ExceptionHandling; using Volo.Abp.AspNetCore.Mvc.Client; @@ -16,6 +18,8 @@ namespace Volo.Abp.AspNetCore.Components.WebAssembly { public override void PreConfigureServices(ServiceConfigurationContext context) { + context.Services.AddConventionalRegistrar(new AbpWebAssemblyConventionalRegistrar()); + PreConfigure(options => { options.ProxyClientBuildActions.Add((_, builder) => @@ -27,6 +31,8 @@ namespace Volo.Abp.AspNetCore.Components.WebAssembly public override void ConfigureServices(ServiceConfigurationContext context) { + context.Services.Replace(ServiceDescriptor.Transient()); + context.Services .GetHostBuilder().Logging .AddProvider(new AbpExceptionHandlingLoggerProvider(context.Services)); diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/AbpComponentActivator.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/AbpComponentActivator.cs new file mode 100644 index 0000000000..9d9666ad3c --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/AbpComponentActivator.cs @@ -0,0 +1,32 @@ +using System; +using Microsoft.AspNetCore.Components; + +namespace Volo.Abp.AspNetCore.Components.WebAssembly +{ + public class ServiceProviderComponentActivator : IComponentActivator + { + public IServiceProvider ServiceProvider { get; } + + public ServiceProviderComponentActivator(IServiceProvider serviceProvider) + { + ServiceProvider = serviceProvider; + } + + public IComponent CreateInstance(Type componentType) + { + var instance = ServiceProvider.GetService(componentType); + + if (instance == null) + { + instance = Activator.CreateInstance(componentType); + } + + if (!(instance is IComponent component)) + { + throw new ArgumentException($"The type {componentType.FullName} does not implement {nameof(IComponent)}.", nameof(componentType)); + } + + return component; + } + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/AbpWebAssemblyConventionalRegistrar.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/AbpWebAssemblyConventionalRegistrar.cs new file mode 100644 index 0000000000..91afb0eadf --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/AbpWebAssemblyConventionalRegistrar.cs @@ -0,0 +1,43 @@ +using System; +using Microsoft.AspNetCore.Components; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.AspNetCore.Components.WebAssembly +{ + public class AbpWebAssemblyConventionalRegistrar : ConventionalRegistrarBase + { + public override void AddType(IServiceCollection services, Type type) + { + if (IsConventionalRegistrationDisabled(type)) + { + return; + } + + if (!IsComponent(type)) + { + return; + } + + var serviceTypes = ExposedServiceExplorer.GetExposedServices(type); + + TriggerServiceExposing(services, type, serviceTypes); + + foreach (var serviceType in serviceTypes) + { + services.Add( + ServiceDescriptor.Describe( + serviceType, + type, + ServiceLifetime.Transient + ) + ); + } + } + + private static bool IsComponent(Type type) + { + return typeof(ComponentBase).IsAssignableFrom(type); + } + } +}