Skip to content

Commit 73b3798

Browse files
committed
修改服务自动注册策略为默认自动注册,手动关闭,而非之前的默认不自动注册
1 parent 0d4c898 commit 73b3798

8 files changed

Lines changed: 28 additions & 9 deletions

File tree

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public class SampleModule : AppModule
4343
}
4444
```
4545
- `[DependsOn]`:声明此模块依赖的模块,加载顺序与声明顺序相同;(可选)
46-
- `[AutoRegisterServicesInAssembly]`:有此特性的模块将会自动将所在程序集中使用ExportServices标记的导出服务注入到DI容器;(可选)
46+
- ~~`[AutoRegisterServicesInAssembly]`:有此特性的模块将会自动将所在程序集中使用ExportServices标记的导出服务注入到DI容器;(可选)~~
47+
- `1.1.6`之后默认自动注册,使用`[DisableAssemblyServicesRegister]`特性进行手动关闭
4748
- 继承`AppModule`:标准的模块只需要继承`IAppModule`即可。如果需要单独的配置,则可以单独实现`IPreConfigureServices``IConfigureServices``IPostConfigureServices``IOnPreApplicationInitialization``IOnApplicationInitialization``IOnPostApplicationInitialization``IOnApplicationShutdown`以在对应的时机进行配置,或直接继承`AppModule`并重写对应的方法;所有方法都有异步版本`IPreConfigureServicesAsync``IConfigureServicesAsync``IPostConfigureServicesAsync``IOnPreApplicationInitializationAsync``IOnApplicationInitializationAsync``IOnPostApplicationInitializationAsync``IOnApplicationShutdownAsync`,或直接继承`AsyncAppModule`
4849

4950
### 3.3 直接通过DI容器使用模块

samples/SampleModule2/SampleModule2Module.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace SampleModule2
1212
[DependsOn(
1313
typeof(SampleModule4.SampleModule4Module)
1414
)]
15-
[AutoRegisterServicesInAssembly]
1615
public class SampleModule2Module : AppModule, IOnPostApplicationInitializationAsync
1716
{
1817
public async Task OnPostApplicationInitializationAsync(ApplicationInitializationContext context)

samples/SampleModule3/SampleModule3Module.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace SampleModule3
66
{
7-
[AutoRegisterServicesInAssembly]
87
public class SampleModule3Module : AppModule
98
{
109

samples/SampleModule5/SampleModule5Module.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace SampleModule5
66
{
77
[DependsOn(typeof(SampleModule2.SampleModule2Module))]
8-
[AutoRegisterServicesInAssembly]
98
public class SampleModule5Module : IAppModule, IOnApplicationShutdown
109
{
1110
public void OnApplicationShutdown(ApplicationShutdownContext context)

src/Cuture.Extensions.Modularity/Attributes/AutoRegisterServicesInAssemblyAttribute.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,19 @@ public class AutoRegisterServicesInAssemblyAttribute : Attribute
2222
#region Public 构造函数
2323

2424
/// <inheritdoc cref="AutoRegisterServicesInAssemblyAttribute"/>
25+
[Obsolete("1.1.6之后的版本默认会进行注册,不需要再手动指定此特性,除非需要自定义 ServiceRegistrar")]
2526
public AutoRegisterServicesInAssemblyAttribute()
2627
{
2728
}
2829

2930
/// <summary>
3031
/// <inheritdoc cref="AutoRegisterServicesInAssemblyAttribute"/>
3132
/// </summary>
32-
public AutoRegisterServicesInAssemblyAttribute(Type type)
33+
public AutoRegisterServicesInAssemblyAttribute(Type serviceRegistrarType)
3334
{
34-
ServiceRegistrarType = type ?? throw new ArgumentNullException(nameof(type));
35+
ServiceRegistrarType = serviceRegistrarType ?? throw new ArgumentNullException(nameof(serviceRegistrarType));
3536

36-
type.ThrowIfNotInherit<IServiceRegistrar>();
37+
serviceRegistrarType.ThrowIfNotInherit<IServiceRegistrar>();
3738
}
3839

3940
#endregion Public 构造函数
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
namespace Cuture.Extensions.Modularity
4+
{
5+
/// <summary>
6+
/// 禁止自动注册模块所在程序集中导出的服务
7+
/// </summary>
8+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
9+
public class DisableAssemblyServicesRegisterAttribute : Attribute
10+
{
11+
#region Public 构造函数
12+
13+
/// <inheritdoc cref="DisableAssemblyServicesRegisterAttribute"/>
14+
public DisableAssemblyServicesRegisterAttribute()
15+
{
16+
}
17+
18+
#endregion Public 构造函数
19+
}
20+
}

src/Cuture.Extensions.Modularity/Internal/Extensions/TypeExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public static bool IsStandardAppModule(this Type type)
103103
[MethodImpl(MethodImplOptions.AggressiveInlining)]
104104
public static bool ShouldRegisterServicesInAssembly(this Type type)
105105
{
106-
return type.IsDefined(typeof(AutoRegisterServicesInAssemblyAttribute), true);
106+
return !type.IsDefined(typeof(DisableAssemblyServicesRegisterAttribute), true);
107107
}
108108

109109
/// <summary>

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
<!--Package Info-->
3131
<PropertyGroup>
32-
<VersionPrefix>1.1.5</VersionPrefix>
32+
<VersionPrefix>1.1.6</VersionPrefix>
3333

3434
<PackageIdPrefix>Cuture.Extensions</PackageIdPrefix>
3535
<Authors>Stratos</Authors>

0 commit comments

Comments
 (0)