Skip to content

Commit

Permalink
fix: 修复ConfigureServicesContext.ServiceProvider在服务初始化阶段为空的问题. (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
joesdu authored Jul 4, 2024
2 parents 4b19244 + 60252e2 commit d1f4383
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 27 deletions.
10 changes: 10 additions & 0 deletions EasilyNET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApi.Test.Unit", "sample\
EndProject
Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{771BAFD8-69AB-4F2E-9FBF-0280E83953BF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "LiteDB", "LiteDB", "{9E0F32B2-D021-4321-9426-81D00458EA70}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasilyNET.LiteDB.Core", "src\EasilyNET.LiteDB.Core\EasilyNET.LiteDB.Core.csproj", "{1392E8DC-3F8A-43AD-9AC8-DE9B6A3A0542}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -143,6 +147,10 @@ Global
{771BAFD8-69AB-4F2E-9FBF-0280E83953BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{771BAFD8-69AB-4F2E-9FBF-0280E83953BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{771BAFD8-69AB-4F2E-9FBF-0280E83953BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1392E8DC-3F8A-43AD-9AC8-DE9B6A3A0542}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1392E8DC-3F8A-43AD-9AC8-DE9B6A3A0542}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1392E8DC-3F8A-43AD-9AC8-DE9B6A3A0542}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1392E8DC-3F8A-43AD-9AC8-DE9B6A3A0542}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -168,6 +176,8 @@ Global
{7E493CA2-CB0C-77E0-B442-5ED204493F13} = {8D626EA8-CB54-BC41-363A-217881BEBA6E}
{8F9C18F9-A526-DCA0-597F-90D64AE5C6C2} = {8D626EA8-CB54-BC41-363A-217881BEBA6E}
{9B426136-DC85-603B-94FB-F3C0B2E72713} = {4F9DEAE5-078F-E77A-2E4A-FEB6FFE226FF}
{9E0F32B2-D021-4321-9426-81D00458EA70} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
{1392E8DC-3F8A-43AD-9AC8-DE9B6A3A0542} = {9E0F32B2-D021-4321-9426-81D00458EA70}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BF2C0069-ED43-44A1-A66C-2CC1B62E3EA3}
Expand Down
3 changes: 2 additions & 1 deletion src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="BouncyCastle.Cryptography" Version="2.4.0" />
<PackageVersion Include="LiteDB" Version="5.0.20" />
<PackageVersion Include="MessagePack" Version="3.0.54-alpha" />
<PackageVersion Include="MongoDB.Bson" Version="2.27.0" />
<PackageVersion Include="MongoDB.Driver" Version="2.27.0" />
<PackageVersion Include="MongoDB.Driver.Core" Version="2.27.0" />
<PackageVersion Include="MongoDB.Driver.GridFS" Version="2.27.0" />
<PackageVersion Include="RabbitMQ.Client" Version="7.0.0-rc.3" />
<PackageVersion Include="RabbitMQ.Client" Version="7.0.0-rc.4" />
<PackageVersion Include="Serilog" Version="4.0.0" />
<PackageVersion Include="Spectre.Console.Json" Version="0.49.2-preview.0.10" />
<PackageVersion Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.6.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ public interface IStartupModuleRunner : IModuleApplication
/// <summary>
/// 初始化
/// </summary>
/// <param name="provider"></param>
void Initialize(IServiceProvider? provider = null);
void Initialize();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,18 @@ internal StartupModuleRunner(Type startupModuleType, IServiceCollection services
ConfigureServices();
}

private IServiceScope? ServiceScope { get; set; }

/// <inheritdoc />
public void Initialize(IServiceProvider? provider = null)
{
if (provider is not null)
{
SetServiceProvider(provider);
}
else
{
ServiceScope = Services.BuildServiceProvider().CreateScope();
SetServiceProvider(ServiceScope.ServiceProvider);
}
InitializeModules();
}

/// <inheritdoc />
public override void Dispose()
{
base.Dispose();
ServiceScope?.Dispose();
}
public void Initialize() => InitializeModules();

/// <summary>
/// 配置服务
/// </summary>
private void ConfigureServices()
{
if (ServiceProvider is null)
{
SetServiceProvider(Services.BuildServiceProvider());
}
var context = new ConfigureServicesContext(Services, ServiceProvider);
Services.AddSingleton(context);
foreach (var config in Modules)
Expand Down
1 change: 1 addition & 0 deletions src/EasilyNET.AutoDependencyInjection/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#### EasilyNET.AutoDependencyInjection

- 新增 WPF 项目支持,理论上也支持 WinForm 项目,但是没有测试,使用时请注意.(仅限于 .NET 的项目,不支持 .NET Framework)
- 经测试是支持 WinUI 3 类型的项目的,但是需要注意的是,WinUI 3 项目的启动方式和 WPF 项目不一样,需要自行调整.

##### 变化

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static IHost InitializeApplication(this IHost builder)
{
builder.Services.GetRequiredService<IObjectAccessor<IHost>>().Value = builder;
var runner = builder.Services.GetRequiredService<IStartupModuleRunner>();
runner.Initialize(builder.Services);
runner.Initialize();
return builder;
}

Expand Down
22 changes: 22 additions & 0 deletions src/EasilyNET.LiteDB.Core/EasilyNET.LiteDB.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyTitle>$(PackagePrefix).LiteDB.Core</AssemblyTitle>
<AssemblyName>$(PackagePrefix).LiteDB.Core</AssemblyName>
<PackageId>$(PackagePrefix).LiteDB.Core</PackageId>
<PackageTags>LiteDB</PackageTags>
<PackageReleaseNotes>
</PackageReleaseNotes>
<RootNamespace>EasilyNET.LiteDB.Core</RootNamespace>
</PropertyGroup>

<PropertyGroup>
<PackageProjectUrl>https://www.nuget.org/packages/EasilyNET.LiteDB.Core</PackageProjectUrl>
<Description>LiteDB 驱动的服务包,方便使用 LiteDB 数据库.</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LiteDB" />
</ItemGroup>

</Project>
53 changes: 53 additions & 0 deletions src/EasilyNET.LiteDB.Core/LiteDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using LiteDB;

namespace EasilyNET.LiteDB.Core;

/// <summary>
/// LiteDbContext
/// </summary>
public class LiteDbContext : IDisposable
{
/// <summary>
/// Database
/// </summary>
private ILiteDatabase Database { get; set; } = default!;

/// <inheritdoc />
public void Dispose()
{
if (Database is { } db)
{
db.Dispose();
}
}

/// <summary>
/// 获取 <see cref="ILiteCollection{TDoc}" />.
/// </summary>
/// <typeparam name="TDoc">实体类型</typeparam>
/// <param name="name">集合名称</param>
/// <returns></returns>
public ILiteCollection<TDoc> GetCollection<TDoc>(string name) where TDoc : class
{
ArgumentException.ThrowIfNullOrWhiteSpace(name, nameof(name));
return Database.GetCollection<TDoc>(name);
}

private static ILiteDatabase GetDatabase(string conn)
{
ArgumentException.ThrowIfNullOrWhiteSpace(conn, nameof(conn));
return new LiteDatabase(conn, new());
}

/// <summary>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="conn"></param>
/// <returns></returns>
public static T CreateInstance<T>(string conn) where T : LiteDbContext
{
var t = Activator.CreateInstance<T>();
t.Database = GetDatabase(conn);
return t;
}
}
4 changes: 4 additions & 0 deletions src/EasilyNET.LiteDB.Core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### EasilyNET.LiteDB.Core

- 提供 DbContext 类型,为业务层提供分离
- 提供业务中会使用到的一些数据类型
2 changes: 1 addition & 1 deletion test/EasilyNET.Test.Unit/EasilyNET.Test.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="7.0.0-alpha.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.0-release-24352-06" />
<PackageReference Include="MSTest.TestAdapter" Version="3.5.0-preview.24325.7" />
<PackageReference Include="MSTest.TestFramework" Version="3.5.0-preview.24325.7" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
Expand Down

0 comments on commit d1f4383

Please sign in to comment.