mirror of https://github.com/abpframework/abp.git
3 changed files with 82 additions and 1 deletions
@ -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; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue