From ceccb8c721ebd68eb88b7596a0ad3a7b73530be7 Mon Sep 17 00:00:00 2001 From: root <8907060@qq.com> Date: Mon, 18 Sep 2023 16:08:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=BB=98=E8=AE=A4=E7=A7=9F?= =?UTF-8?q?=E6=88=B7=E8=A7=A3=E6=9E=90=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/Build.csproj | 34 +-- build/version.props | 2 +- src/Util.AspNetCore/07-Util.AspNetCore.csproj | 2 +- src/Util.AspNetCore/Helpers/Web.cs | 24 +- .../03-Util.Data.Dapper.PostgreSql.csproj | 2 +- ...Data.EntityFrameworkCore.PostgreSql.csproj | 2 +- ....Data.EntityFrameworkCore.SqlServer.csproj | 2 +- ...til.Data.EntityFrameworkCore.Sqlite.csproj | 2 +- .../01-Util.Data.EntityFrameworkCore.csproj | 4 +- src/Util.Domain/Auditing/IAudited.cs | 2 +- .../01-Util.Images.ImageSharp.csproj | 2 +- .../01-Util.Localization.csproj | 2 +- .../04-Util.Microservices.HealthChecks.csproj | 2 +- .../02-Util.Templates.Razor.csproj | 2 +- .../IDomainTenantResolver.cs | 8 +- src/Util.Tenants/AppBuilderExtensions.cs | 5 +- .../Resolvers/DefaultTenantResolver.cs | 4 + .../Resolvers/DomainTenantResolver.cs | 90 ++----- .../Resolvers/DomainTenantResolverHelper.cs | 101 ++++++++ .../Resolvers/ITenantDomainStore.cs | 13 + .../Internal/DomainTenantResolver.cs | 44 ++++ .../Resolvers/NullTenantDomainStore.cs | 31 +++ src/Util.Tenants/TenantOptions.cs | 3 +- src/Util.Tenants/TenantResolverCollection.cs | 39 ++- src/Util.Ui.NgZorro/03-Util.Ui.NgZorro.csproj | 2 +- src/Util.Ui/01-Util.Ui.csproj | 2 +- src/Util.Ui/Razor/GenerateHtmlFilter.cs | 230 +++++++++--------- .../Util.Aop.AspectCore.Tests.csproj | 4 +- ...ameworkCore.MySql.Tests.Integration.csproj | 4 +- ...rkCore.PostgreSql.Tests.Integration.csproj | 4 +- ...orkCore.SqlServer.Tests.Integration.csproj | 4 +- .../Util.Application.Tests.csproj | 4 +- ...tion.WebApi.MySql.Tests.Integration.csproj | 6 +- ...WebApi.PostgreSql.Tests.Integration.csproj | 6 +- ....WebApi.SqlServer.Tests.Integration.csproj | 6 +- ...pplication.WebApi.Tests.Integration.csproj | 6 +- .../Util.AspNetCore.Tests.Integration.csproj | 6 +- ...ching.EasyCaching.Tests.Integration.csproj | 4 +- .../Util.Core.Tests.Integration.csproj | 4 +- test/Util.Core.Tests/Util.Core.Tests.csproj | 4 +- ...Data.Dapper.MySql.Tests.Integration.csproj | 6 +- ...Dapper.PostgreSql.Tests.Integration.csproj | 6 +- ....Dapper.SqlServer.Tests.Integration.csproj | 6 +- ...ameworkCore.MySql.Tests.Integration.csproj | 4 +- ...meworkCore.Oracle.Tests.Integration.csproj | 4 +- ...rkCore.PostgreSql.Tests.Integration.csproj | 4 +- ...orkCore.SqlServer.Tests.Integration.csproj | 6 +- ...meworkCore.Sqlite.Tests.Integration.csproj | 4 +- .../Util.Data.Sql.Tests.csproj | 4 +- test/Util.Data.Tests/Util.Data.Tests.csproj | 4 +- .../Util.Domain.Tests.csproj | 4 +- ...il.Events.MediatR.Tests.Integration.csproj | 4 +- .../Util.Events.Tests.Integration.csproj | 4 +- ...FileStorage.Minio.Tests.Integration.csproj | 6 +- ....Generators.Razor.Tests.Integration.csproj | 4 +- .../Contexts/GeneratorContextBuilderTest.cs | 5 +- .../Util.Generators.Tests.csproj | 4 +- ...til.Images.Avatar.Tests.Integration.csproj | 4 +- ...Images.ImageSharp.Tests.Integration.csproj | 4 +- ...Util.Localization.Tests.Integration.csproj | 6 +- ...log.Exceptionless.Tests.Integration.csproj | 4 +- ...l.Logging.Serilog.Tests.Integration.csproj | 4 +- .../Util.Logging.Tests.csproj | 4 +- ...icroservices.Dapr.Tests.Integration.csproj | 6 +- .../Util.Microservices.Dapr.Tests.csproj | 4 +- ...croservices.Polly.Tests.Integration.csproj | 4 +- ...Util.ObjectMapping.AutoMapper.Tests.csproj | 4 +- ...heduling.Hangfire.Tests.Integration.csproj | 6 +- ...Scheduling.Quartz.Tests.Integration.csproj | 6 +- .../Util.Security.Tests.csproj | 4 +- ...plates.Handlebars.Tests.Integration.csproj | 4 +- ...l.Templates.Razor.Tests.Integration.csproj | 4 +- .../Resolvers/DomainTenantResolverTest.cs | 25 +- .../Util.Tenants.Tests.Integration/Startup.cs | 11 +- .../Util.Tenants.Tests.Integration.csproj | 6 +- .../Resolvers/DomainTenantResolverTest.cs | 36 ++- .../Resolvers/TenantResolverCollectionTest.cs | 48 ++++ .../Util.Tenants.Tests.csproj | 4 +- .../OperationLogConfiguration.cs | 2 +- .../Util.Ui.NgAlain.Tests.csproj | 4 +- .../Util.Ui.NgZorro.Tests.csproj | 4 +- test/Util.Ui.Tests/Util.Ui.Tests.csproj | 4 +- .../Util.Validation.Tests.csproj | 4 +- 83 files changed, 657 insertions(+), 356 deletions(-) rename src/{Util.Tenants/Resolvers => Util.Tenants.Abstractions}/IDomainTenantResolver.cs (71%) create mode 100644 src/Util.Tenants/Resolvers/DomainTenantResolverHelper.cs create mode 100644 src/Util.Tenants/Resolvers/ITenantDomainStore.cs create mode 100644 src/Util.Tenants/Resolvers/Internal/DomainTenantResolver.cs create mode 100644 src/Util.Tenants/Resolvers/NullTenantDomainStore.cs diff --git a/build/Build.csproj b/build/Build.csproj index 4a0f0bea7..a2fbee17d 100644 --- a/build/Build.csproj +++ b/build/Build.csproj @@ -1,17 +1,17 @@ - - - - $(NetTargetFramework) - - - - - - - - - - - - - + + + + $(NetTargetFramework) + + + + + + + + + + + + + diff --git a/build/version.props b/build/version.props index c95ef3448..3349511d8 100644 --- a/build/version.props +++ b/build/version.props @@ -2,7 +2,7 @@ 7 1 - 22 + 27 $(VersionMajor).$(VersionMinor).$(VersionPatch) diff --git a/src/Util.AspNetCore/07-Util.AspNetCore.csproj b/src/Util.AspNetCore/07-Util.AspNetCore.csproj index 82e94e308..5cc5756a7 100644 --- a/src/Util.AspNetCore/07-Util.AspNetCore.csproj +++ b/src/Util.AspNetCore/07-Util.AspNetCore.csproj @@ -31,7 +31,7 @@ - + diff --git a/src/Util.AspNetCore/Helpers/Web.cs b/src/Util.AspNetCore/Helpers/Web.cs index 78cf379f2..8a5d9ae90 100644 --- a/src/Util.AspNetCore/Helpers/Web.cs +++ b/src/Util.AspNetCore/Helpers/Web.cs @@ -1,4 +1,5 @@ -using System.Security.Claims; +using System.Collections.Specialized; +using System.Security.Claims; using Util.Http; using Util.Security.Authentication; @@ -274,6 +275,27 @@ public static string UrlDecode( string url, Encoding encoding ) { #endregion + #region ParseQueryString(将查询字符串转换为名值对集合) + + /// + /// 将查询字符串转换为名值对集合 + /// + /// 查询字符串 + public static NameValueCollection ParseQueryString( string query ) { + return HttpUtility.ParseQueryString( query ); + } + + /// + /// 将查询字符串转换为名值对集合 + /// + /// 查询字符串 + /// 字符编码 + public static NameValueCollection ParseQueryString( string query, Encoding encoding ) { + return HttpUtility.ParseQueryString( query, encoding ); + } + + #endregion + #region DownloadAsync(下载) /// diff --git a/src/Util.Data.Dapper.PostgreSql/03-Util.Data.Dapper.PostgreSql.csproj b/src/Util.Data.Dapper.PostgreSql/03-Util.Data.Dapper.PostgreSql.csproj index dd4806d18..6fb3b5af1 100644 --- a/src/Util.Data.Dapper.PostgreSql/03-Util.Data.Dapper.PostgreSql.csproj +++ b/src/Util.Data.Dapper.PostgreSql/03-Util.Data.Dapper.PostgreSql.csproj @@ -27,7 +27,7 @@ - + diff --git a/src/Util.Data.EntityFrameworkCore.PostgreSql/03-Util.Data.EntityFrameworkCore.PostgreSql.csproj b/src/Util.Data.EntityFrameworkCore.PostgreSql/03-Util.Data.EntityFrameworkCore.PostgreSql.csproj index 54d0d6b28..606743f7f 100644 --- a/src/Util.Data.EntityFrameworkCore.PostgreSql/03-Util.Data.EntityFrameworkCore.PostgreSql.csproj +++ b/src/Util.Data.EntityFrameworkCore.PostgreSql/03-Util.Data.EntityFrameworkCore.PostgreSql.csproj @@ -27,7 +27,7 @@ - + diff --git a/src/Util.Data.EntityFrameworkCore.SqlServer/02-Util.Data.EntityFrameworkCore.SqlServer.csproj b/src/Util.Data.EntityFrameworkCore.SqlServer/02-Util.Data.EntityFrameworkCore.SqlServer.csproj index 853fff330..87fe849e9 100644 --- a/src/Util.Data.EntityFrameworkCore.SqlServer/02-Util.Data.EntityFrameworkCore.SqlServer.csproj +++ b/src/Util.Data.EntityFrameworkCore.SqlServer/02-Util.Data.EntityFrameworkCore.SqlServer.csproj @@ -27,7 +27,7 @@ - + diff --git a/src/Util.Data.EntityFrameworkCore.Sqlite/05-Util.Data.EntityFrameworkCore.Sqlite.csproj b/src/Util.Data.EntityFrameworkCore.Sqlite/05-Util.Data.EntityFrameworkCore.Sqlite.csproj index ff582c2a6..20888572c 100644 --- a/src/Util.Data.EntityFrameworkCore.Sqlite/05-Util.Data.EntityFrameworkCore.Sqlite.csproj +++ b/src/Util.Data.EntityFrameworkCore.Sqlite/05-Util.Data.EntityFrameworkCore.Sqlite.csproj @@ -27,7 +27,7 @@ - + diff --git a/src/Util.Data.EntityFrameworkCore/01-Util.Data.EntityFrameworkCore.csproj b/src/Util.Data.EntityFrameworkCore/01-Util.Data.EntityFrameworkCore.csproj index 15b803066..1fbeeb615 100644 --- a/src/Util.Data.EntityFrameworkCore/01-Util.Data.EntityFrameworkCore.csproj +++ b/src/Util.Data.EntityFrameworkCore/01-Util.Data.EntityFrameworkCore.csproj @@ -27,8 +27,8 @@ - - + + diff --git a/src/Util.Domain/Auditing/IAudited.cs b/src/Util.Domain/Auditing/IAudited.cs index 3dd606a34..9e0688e65 100644 --- a/src/Util.Domain/Auditing/IAudited.cs +++ b/src/Util.Domain/Auditing/IAudited.cs @@ -9,6 +9,6 @@ public interface IAudited : ICreationAudited, IModificationAudited { /// /// 操作审计 /// -/// 操作人编号类型 +/// 操作人标识类型 public interface IAudited : ICreationAudited, IModificationAudited { } \ No newline at end of file diff --git a/src/Util.Images.ImageSharp/01-Util.Images.ImageSharp.csproj b/src/Util.Images.ImageSharp/01-Util.Images.ImageSharp.csproj index 21c30141b..c8cb7e6b4 100644 --- a/src/Util.Images.ImageSharp/01-Util.Images.ImageSharp.csproj +++ b/src/Util.Images.ImageSharp/01-Util.Images.ImageSharp.csproj @@ -32,7 +32,7 @@ - + diff --git a/src/Util.Localization/01-Util.Localization.csproj b/src/Util.Localization/01-Util.Localization.csproj index c27f7b005..7c267f4ba 100644 --- a/src/Util.Localization/01-Util.Localization.csproj +++ b/src/Util.Localization/01-Util.Localization.csproj @@ -27,7 +27,7 @@ - + diff --git a/src/Util.Microservices.HealthChecks/04-Util.Microservices.HealthChecks.csproj b/src/Util.Microservices.HealthChecks/04-Util.Microservices.HealthChecks.csproj index 3f189f66a..496255b8c 100644 --- a/src/Util.Microservices.HealthChecks/04-Util.Microservices.HealthChecks.csproj +++ b/src/Util.Microservices.HealthChecks/04-Util.Microservices.HealthChecks.csproj @@ -29,7 +29,7 @@ - + diff --git a/src/Util.Templates.Razor/02-Util.Templates.Razor.csproj b/src/Util.Templates.Razor/02-Util.Templates.Razor.csproj index 47ecdc330..98abf6ad7 100644 --- a/src/Util.Templates.Razor/02-Util.Templates.Razor.csproj +++ b/src/Util.Templates.Razor/02-Util.Templates.Razor.csproj @@ -37,7 +37,7 @@ - + diff --git a/src/Util.Tenants/Resolvers/IDomainTenantResolver.cs b/src/Util.Tenants.Abstractions/IDomainTenantResolver.cs similarity index 71% rename from src/Util.Tenants/Resolvers/IDomainTenantResolver.cs rename to src/Util.Tenants.Abstractions/IDomainTenantResolver.cs index 47c11a064..701934b69 100644 --- a/src/Util.Tenants/Resolvers/IDomainTenantResolver.cs +++ b/src/Util.Tenants.Abstractions/IDomainTenantResolver.cs @@ -1,9 +1,7 @@ -using Util.Dependency; - -namespace Util.Tenants.Resolvers; +namespace Util.Tenants; /// -/// 基于域名的租户解析器 +/// 域名租户解析器 /// public interface IDomainTenantResolver : ITransientDependency { /// @@ -11,4 +9,4 @@ public interface IDomainTenantResolver : ITransientDependency { /// /// 域名,范例: a.test.com Task ResolveTenantIdAsync( string host ); -} \ No newline at end of file +} \ No newline at end of file diff --git a/src/Util.Tenants/AppBuilderExtensions.cs b/src/Util.Tenants/AppBuilderExtensions.cs index 8fc8229a3..7567a421a 100644 --- a/src/Util.Tenants/AppBuilderExtensions.cs +++ b/src/Util.Tenants/AppBuilderExtensions.cs @@ -22,14 +22,13 @@ public static IAppBuilder AddTenant( this IAppBuilder builder ) { /// 租户配置操作 public static IAppBuilder AddTenant( this IAppBuilder builder, Action setupAction ) { builder.CheckNull( nameof( builder ) ); - setupAction ??= tenantOptions => tenantOptions.IsEnabled = true; var options = new TenantOptions { IsEnabled = true }; - setupAction.Invoke( options ); + setupAction?.Invoke( options ); builder.Host.ConfigureServices( ( context, services ) => { services.TryAddSingleton(); - services.Configure( setupAction ); + services.TryAddSingleton>( new OptionsWrapper( options ) ); } ); return builder; } diff --git a/src/Util.Tenants/Resolvers/DefaultTenantResolver.cs b/src/Util.Tenants/Resolvers/DefaultTenantResolver.cs index 5a25c96f7..d521f07bf 100644 --- a/src/Util.Tenants/Resolvers/DefaultTenantResolver.cs +++ b/src/Util.Tenants/Resolvers/DefaultTenantResolver.cs @@ -21,11 +21,15 @@ public DefaultTenantResolver( IOptions options ) { /// public async Task ResolveAsync( HttpContext context ) { + if ( context == null ) + return null; if ( _options.IsEnabled == false ) return null; var session = context.RequestServices.GetService(); if ( session is { IsAuthenticated: true } ) return session.TenantId; + if ( _options.Resolvers == null ) + return null; foreach ( var resolver in _options.Resolvers.OrderByDescending( t => t.Priority ) ) { var result = await resolver.ResolveAsync( context ); if ( result.IsEmpty() == false ) diff --git a/src/Util.Tenants/Resolvers/DomainTenantResolver.cs b/src/Util.Tenants/Resolvers/DomainTenantResolver.cs index fd8ef74f3..9cdc0d472 100644 --- a/src/Util.Tenants/Resolvers/DomainTenantResolver.cs +++ b/src/Util.Tenants/Resolvers/DomainTenantResolver.cs @@ -4,15 +4,6 @@ /// 基于域名的租户解析器 /// public class DomainTenantResolver : TenantResolverBase { - /// - /// 域名格式字符串 - /// - private readonly string _domainFormat; - /// - /// 域名映射配置 - /// - private readonly IDictionary _domainMap; - /// /// 初始化基于域名的租户解析器 /// @@ -24,7 +15,7 @@ public DomainTenantResolver() { /// /// 域名格式字符串,范例:{0}.a.com public DomainTenantResolver( string domainFormat ) { - _domainFormat = domainFormat; + DomainFormat = domainFormat; } /// @@ -32,84 +23,37 @@ public DomainTenantResolver( string domainFormat ) { /// /// 域名映射配置,键为域名,值为租户标识 public DomainTenantResolver( IDictionary domainMap ) { - _domainMap = domainMap; + DomainMap = domainMap; } + /// + /// 域名格式字符串 + /// + public string DomainFormat { get; } + + /// + /// 域名映射配置 + /// + public IDictionary DomainMap { get; } + /// /// 解析租户标识 /// protected override async Task Resolve( HttpContext context ) { - var host = GetDomain( context.Request.Host.Value ); + var host = DomainTenantResolverHelper.RemoveDomainPrefix( context.Request.Host.Value ); if ( host.IsEmpty() ) return null; - var result = await Resolve( context,host ); + var result = await Resolve( context, host ); GetLog( context ).LogTrace( $"执行域名租户解析器,host={host},tenantId={result}" ); return result; } - /// - /// 获取域名 - /// - private string GetDomain( string domain ) { - return domain.RemoveStart( "http://" ).RemoveStart( "https://" ); - } - /// /// 解析租户标识 /// private async Task Resolve( HttpContext context, string host ) { - if ( _domainMap != null ) - return ResolveByMap( host ); - if ( _domainFormat.IsEmpty() == false ) - return ResolveByFormat( host ); - var resolver = context.RequestServices.GetService(); - return resolver == null ? ResolveBySplit( host ) : await resolver.ResolveTenantIdAsync( host ); - } - - /// - /// 通过域名映射配置解析 - /// - private string ResolveByMap( string host ) { - foreach ( var item in _domainMap ) { - if ( item.Key == host ) - return item.Value; - } - return null; - } - - /// - /// 通过域名格式字符串解析 - /// - private string ResolveByFormat( string host ) { - var formats = GetDomainFormats(); - foreach ( var format in formats ) { - if ( format.IsEmpty() ) - continue; - var result = Util.Helpers.String.Extract( host, GetDomain( format ) ).FirstOrDefault().Value; - if( result.IsEmpty() == false ) - return result; - } - return null; - } - - /// - /// 获取域名格式列表 - /// - private IEnumerable GetDomainFormats() { - if ( _domainFormat.IndexOf( ",", StringComparison.Ordinal ) >= 0 ) - return _domainFormat.Split( ',' ); - if ( _domainFormat.IndexOf( ";", StringComparison.Ordinal ) >= 0 ) - return _domainFormat.Split( ';' ); - return new[] { _domainFormat }; - } - - /// - /// 通过拆分解析 - /// - private string ResolveBySplit( string host ) { - var items = host.Split( '.' ); - if ( items.Length > 2 ) - return items[0]; - return null; + var store = context.RequestServices.GetService(); + var map = await DomainTenantResolverHelper.CombineMapAsync( DomainMap,store ); + return DomainTenantResolverHelper.ResolveTenantId( host, map, DomainFormat ); } } \ No newline at end of file diff --git a/src/Util.Tenants/Resolvers/DomainTenantResolverHelper.cs b/src/Util.Tenants/Resolvers/DomainTenantResolverHelper.cs new file mode 100644 index 000000000..f1b2c9485 --- /dev/null +++ b/src/Util.Tenants/Resolvers/DomainTenantResolverHelper.cs @@ -0,0 +1,101 @@ +namespace Util.Tenants.Resolvers; + +/// +/// 租户域名解析器辅助操作 +/// +public class DomainTenantResolverHelper { + /// + /// 解析租户标识 + /// + /// 域名 + /// 域名映射配置 + /// 域名格式字符串 + public static string ResolveTenantId( string domain, IDictionary domainMap, string domainFormat ) { + if ( domain.IsEmpty() ) + return null; + var host = RemoveDomainPrefix( domain ); + if ( host.IsEmpty() ) + return null; + if ( domainMap is { Count: > 0 } ) { + var result = ResolveByMap( host, domainMap ); + if ( result.IsEmpty() == false ) + return result; + } + domainFormat = domainFormat.RemoveStart( "," ).RemoveEnd( "," ); + return domainFormat.IsEmpty() ? ResolveBySplit( host ): ResolveByFormat( host, domainFormat ); + } + + /// + /// 移除域名前缀 + /// + public static string RemoveDomainPrefix( string domain ) { + return domain.RemoveStart( "http://" ).RemoveStart( "https://" ); + } + + /// + /// 通过域名映射配置解析 + /// + private static string ResolveByMap( string host, IDictionary domainMap ) { + foreach ( var item in domainMap ) { + if ( item.Key == host ) + return item.Value; + } + return null; + } + + /// + /// 通过域名格式字符串解析 + /// + private static string ResolveByFormat( string host, string domainFormat ) { + var formats = GetDomainFormats( domainFormat ); + foreach ( var format in formats ) { + if ( format.IsEmpty() ) + continue; + var result = Util.Helpers.String.Extract( host, RemoveDomainPrefix( format ) ).FirstOrDefault().Value; + if ( result.IsEmpty() == false ) + return result; + } + return null; + } + + /// + /// 获取域名格式列表 + /// + private static IEnumerable GetDomainFormats( string domainFormat ) { + if ( domainFormat.IndexOf( ",", StringComparison.Ordinal ) >= 0 ) + return domainFormat.Split( ',' ); + if ( domainFormat.IndexOf( ";", StringComparison.Ordinal ) >= 0 ) + return domainFormat.Split( ';' ); + return new[] { domainFormat }; + } + + /// + /// 通过拆分解析 + /// + private static string ResolveBySplit( string host ) { + var items = host.Split( '.' ); + if ( items.Length > 2 ) + return items[0]; + return null; + } + + /// + /// 合并域名映射字典和租户域名存储器中的域名映射项 + /// + /// 域名映射字典 + /// 租户域名存储器 + public static async Task> CombineMapAsync( IDictionary domainMap, ITenantDomainStore store) { + if ( store == null ) + return domainMap; + var map = await store.GetAsync(); + if ( map == null || map.Count == 0 ) + return domainMap; + domainMap ??= new Dictionary(); + foreach ( var item in map ) { + if ( domainMap.ContainsKey( item.Key ) == false ) { + domainMap.Add( item.Key, item.Value ); + } + } + return domainMap; + } +} \ No newline at end of file diff --git a/src/Util.Tenants/Resolvers/ITenantDomainStore.cs b/src/Util.Tenants/Resolvers/ITenantDomainStore.cs new file mode 100644 index 000000000..16c9038dd --- /dev/null +++ b/src/Util.Tenants/Resolvers/ITenantDomainStore.cs @@ -0,0 +1,13 @@ +using Util.Dependency; + +namespace Util.Tenants.Resolvers; + +/// +/// 租户域名存储器 +/// +public interface ITenantDomainStore : ITransientDependency { + /// + /// 获取租户域名映射,键为域名,值为租户标识 + /// + Task> GetAsync(); +} \ No newline at end of file diff --git a/src/Util.Tenants/Resolvers/Internal/DomainTenantResolver.cs b/src/Util.Tenants/Resolvers/Internal/DomainTenantResolver.cs new file mode 100644 index 000000000..631a8ff0b --- /dev/null +++ b/src/Util.Tenants/Resolvers/Internal/DomainTenantResolver.cs @@ -0,0 +1,44 @@ +namespace Util.Tenants.Resolvers.Internal; + +/// +/// 域名租户解析器 +/// +internal class DomainTenantResolver : IDomainTenantResolver { + /// + /// 租户域名存储器 + /// + private readonly ITenantDomainStore _store; + /// + /// 租户配置 + /// + private readonly TenantOptions _options; + + /// + /// 初始化域名租户解析器 + /// + /// 租户域名存储器 + /// 租户配置 + public DomainTenantResolver( ITenantDomainStore store, IOptions options ) { + _store = store ?? throw new ArgumentNullException( nameof( store ) ); + _options = options?.Value ?? new TenantOptions(); + } + + /// + public async Task ResolveTenantIdAsync( string host ) { + if ( host.IsEmpty() ) + return null; + var domainMap = new Dictionary(); + var domainFormat = string.Empty; + var resolvers = _options.Resolvers.GetResolvers(); + foreach ( var resolver in resolvers ) { + if ( resolver.DomainMap != null ) { + foreach ( var item in resolver.DomainMap ) { + domainMap.Add( item.Key, item.Value ); + } + } + domainFormat += $",{resolver.DomainFormat}"; + } + var map = await DomainTenantResolverHelper.CombineMapAsync( domainMap, _store ); + return DomainTenantResolverHelper.ResolveTenantId( host, map, domainFormat ); + } +} \ No newline at end of file diff --git a/src/Util.Tenants/Resolvers/NullTenantDomainStore.cs b/src/Util.Tenants/Resolvers/NullTenantDomainStore.cs new file mode 100644 index 000000000..b75ff31de --- /dev/null +++ b/src/Util.Tenants/Resolvers/NullTenantDomainStore.cs @@ -0,0 +1,31 @@ +using Util.Dependency; + +namespace Util.Tenants.Resolvers; + +/// +/// 空租户域名存储器 +/// +[Ioc(-9)] +public class NullTenantDomainStore : ITenantDomainStore { + /// + /// 结果 + /// + private readonly IDictionary _result; + + /// + /// 初始化空租户域名存储器 + /// + public NullTenantDomainStore() { + _result = new Dictionary(); + } + + /// + /// 空租户域名存储器 + /// + public static readonly ITenantDomainStore Instance = new NullTenantDomainStore(); + + /// + public Task> GetAsync() { + return Task.FromResult(_result); + } +} \ No newline at end of file diff --git a/src/Util.Tenants/TenantOptions.cs b/src/Util.Tenants/TenantOptions.cs index 9f365b178..fd9880786 100644 --- a/src/Util.Tenants/TenantOptions.cs +++ b/src/Util.Tenants/TenantOptions.cs @@ -14,8 +14,7 @@ public TenantOptions() { Resolvers = new TenantResolverCollection { new HeaderTenantResolver{ Priority = 9 }, new QueryStringTenantResolver{ Priority = 7 }, - new CookieTenantResolver{ Priority = 5 }, - new DomainTenantResolver{ Priority = 3 } + new CookieTenantResolver{ Priority = 5 } }; } diff --git a/src/Util.Tenants/TenantResolverCollection.cs b/src/Util.Tenants/TenantResolverCollection.cs index 7eb08d349..4fe379f58 100644 --- a/src/Util.Tenants/TenantResolverCollection.cs +++ b/src/Util.Tenants/TenantResolverCollection.cs @@ -34,7 +34,42 @@ public IEnumerator GetEnumerator() { /// 获取租户解析器列表 /// public List GetResolvers() { - return _resolvers.Select( t => t.Value ).ToList(); + return _resolvers + .Select( t => t.Value ) + .OrderByDescending( t => t.Priority ) + .ToList(); + } + + /// + /// 获取租户解析器列表 + /// + public List GetResolvers() where TResolver : ITenantResolver { + return _resolvers + .Where( t => t.Value.GetType() == typeof( TResolver ) ) + .Select( t => (TResolver)t.Value ) + .OrderByDescending( t => t.Priority ) + .ToList(); + } + + /// + /// 获取租户解析器 + /// + /// 租户解析器类型 + public TResolver GetResolver() where TResolver: ITenantResolver { + var key = typeof( TResolver ).FullName; + return GetResolver( key ); + } + + /// + /// 获取租户解析器 + /// + /// 租户解析器类型 + /// 租户解析器键名 + public TResolver GetResolver( string key ) where TResolver : ITenantResolver { + var result = _resolvers.FirstOrDefault( t => t.Key == key ); + if ( result.Value == null ) + return default; + return (TResolver)result.Value; } /// @@ -42,6 +77,8 @@ public List GetResolvers() { /// /// 租户解析器 public void Add( ITenantResolver resolver ) { + if ( resolver == null ) + return; var key = resolver.GetType().FullName; Add( key, resolver ); } diff --git a/src/Util.Ui.NgZorro/03-Util.Ui.NgZorro.csproj b/src/Util.Ui.NgZorro/03-Util.Ui.NgZorro.csproj index 274269134..239ce6e3a 100644 --- a/src/Util.Ui.NgZorro/03-Util.Ui.NgZorro.csproj +++ b/src/Util.Ui.NgZorro/03-Util.Ui.NgZorro.csproj @@ -27,7 +27,7 @@ - + diff --git a/src/Util.Ui/01-Util.Ui.csproj b/src/Util.Ui/01-Util.Ui.csproj index 4007ca4ef..2a21078fc 100644 --- a/src/Util.Ui/01-Util.Ui.csproj +++ b/src/Util.Ui/01-Util.Ui.csproj @@ -28,7 +28,7 @@ - + diff --git a/src/Util.Ui/Razor/GenerateHtmlFilter.cs b/src/Util.Ui/Razor/GenerateHtmlFilter.cs index b70228206..c13911fdb 100644 --- a/src/Util.Ui/Razor/GenerateHtmlFilter.cs +++ b/src/Util.Ui/Razor/GenerateHtmlFilter.cs @@ -1,116 +1,116 @@ -using Microsoft.AspNetCore.Mvc.Rendering; - -namespace Util.Ui.Razor; - -/// -/// 生成Html静态页面过滤器 -/// -public class GenerateHtmlFilter : IAsyncPageFilter { - /// - /// 执行 - /// - public async Task OnPageHandlerExecutionAsync( PageHandlerExecutingContext context, PageHandlerExecutionDelegate next ) { - await next(); - } - - /// - /// 选择操作 - /// - public async Task OnPageHandlerSelectionAsync( PageHandlerSelectedContext context ) { - if ( IsGenerateHtml( context ) != true ) - return; - if ( context.ActionDescriptor.ViewEnginePath == "/Error" ) - return; - var path = CreatePath( context ); - if( string.IsNullOrWhiteSpace( path ) ) - return; - var html = await GetHtml( context ); - await WriteFile( path, html ); - } - - /// - /// 是否生成html - /// - private bool IsGenerateHtml( PageHandlerSelectedContext context ) { - var options = context.HttpContext.RequestServices.GetService>(); - if ( options == null ) - return false; - return options.Value.IsGenerateHtml; - } - - /// - /// 创建Html文件路径 - /// - private string CreatePath( PageHandlerSelectedContext context ) { - var attribute = context.ActionDescriptor.ModelTypeInfo.GetCustomAttribute(); - if( attribute == null ) - return GetPath( context.ActionDescriptor.ViewEnginePath ); - if( attribute.Ignore ) - return string.Empty; - if( string.IsNullOrWhiteSpace( attribute.Path ) ) - return string.Empty; - return attribute.Path; - } - - /// - /// 获取Html文件路径 - /// - /// 路径 - /// 基路径,默认值:/ClientApp/src/app - /// html文件后缀,默认值:component.html - public static string GetPath( string path, string basePath = "/ClientApp/src/app", string htmlSuffix = "component.html" ) { - if( string.IsNullOrWhiteSpace( path ) ) - return string.Empty; - path = path.Kebaberize().ToLower().Trim( '\\' ).Trim( '/' ); - if( path.Contains( "/" ) == false ) - return Util.Helpers.Url.JoinPath( basePath, $"{path}.{htmlSuffix}" ); - var lastIndex = path.LastIndexOf( "/", StringComparison.Ordinal ); - return Util.Helpers.Url.JoinPath( basePath, path.Substring( 0, lastIndex ), "html", $"{path.Substring( lastIndex + 1 )}.{htmlSuffix}" ); - } - - /// - /// 获取页面Html - /// - private async Task GetHtml( PageHandlerSelectedContext context ) { - var serviceProvider = context.HttpContext.RequestServices; - var engine = serviceProvider.GetService(); - var activator = serviceProvider.GetService(); - dynamic model = System.Convert.ChangeType( context.HandlerInstance, context.HandlerInstance.GetType() ); - var actionContext = new ActionContext( context.HttpContext, model.RouteData, context.ActionDescriptor ); - var page = FindPage( engine, context.ActionDescriptor.RelativePath ); - await using var stringWriter = new StringWriter(); - var view = new RazorView( engine, activator, new List(), page, HtmlEncoder.Default, new DiagnosticListener( "ViewRenderService" ) ); - var viewContext = new ViewContext( actionContext, view, model?.ViewData, model?.TempData, stringWriter, new HtmlHelperOptions() ) { - ExecutingFilePath = context.ActionDescriptor.RelativePath - }; - var razorPage = (Page)page; - razorPage.PageContext = model.PageContext; - razorPage.ViewContext = viewContext; - activator.Activate( razorPage, viewContext ); - await page.ExecuteAsync(); - return stringWriter.ToString(); - } - - /// - /// 查找Razor页面 - /// - private IRazorPage FindPage( IRazorViewEngine razorViewEngine, string pageName ) { - var result = razorViewEngine.GetPage( null, pageName ); - return result.Page; - } - - /// - /// 写入文件 - /// - private async Task WriteFile( string path, string html ) { - if( string.IsNullOrWhiteSpace( html ) ) - return; - path = Util.Helpers.Web.GetPhysicalPath( path ); - var directory = Path.GetDirectoryName( path ); - if( string.IsNullOrWhiteSpace( directory ) ) - return; - if( Directory.Exists( directory ) == false ) - Directory.CreateDirectory( directory ); - await File.WriteAllTextAsync( path, html ); - } +using Microsoft.AspNetCore.Mvc.Rendering; + +namespace Util.Ui.Razor; + +/// +/// 生成Html静态页面过滤器 +/// +public class GenerateHtmlFilter : IAsyncPageFilter { + /// + /// 执行 + /// + public async Task OnPageHandlerExecutionAsync( PageHandlerExecutingContext context, PageHandlerExecutionDelegate next ) { + await next(); + } + + /// + /// 选择操作 + /// + public async Task OnPageHandlerSelectionAsync( PageHandlerSelectedContext context ) { + if ( IsGenerateHtml( context ) != true ) + return; + if ( context.ActionDescriptor.ViewEnginePath == "/Error" ) + return; + var path = CreatePath( context ); + if( string.IsNullOrWhiteSpace( path ) ) + return; + var html = await GetHtml( context ); + await WriteFile( path, html ); + } + + /// + /// 是否生成html + /// + private bool IsGenerateHtml( PageHandlerSelectedContext context ) { + var options = context.HttpContext.RequestServices.GetService>(); + if ( options == null ) + return false; + return options.Value.IsGenerateHtml; + } + + /// + /// 创建Html文件路径 + /// + private string CreatePath( PageHandlerSelectedContext context ) { + var attribute = context.ActionDescriptor.ModelTypeInfo.GetCustomAttribute(); + if( attribute == null ) + return GetPath( context.ActionDescriptor.ViewEnginePath ); + if( attribute.Ignore ) + return string.Empty; + if( string.IsNullOrWhiteSpace( attribute.Path ) ) + return string.Empty; + return attribute.Path; + } + + /// + /// 获取Html文件路径 + /// + /// 路径 + /// 基路径,默认值:/ClientApp/src/app + /// html文件后缀,默认值:component.html + public static string GetPath( string path, string basePath = "/ClientApp/src/app", string htmlSuffix = "component.html" ) { + if( string.IsNullOrWhiteSpace( path ) ) + return string.Empty; + path = path.Kebaberize().ToLower().Trim( '\\' ).Trim( '/' ); + if( path.Contains( "/" ) == false ) + return Util.Helpers.Url.JoinPath( basePath, $"{path}.{htmlSuffix}" ); + var lastIndex = path.LastIndexOf( "/", StringComparison.Ordinal ); + return Util.Helpers.Url.JoinPath( basePath, path.Substring( 0, lastIndex ), "html", $"{path.Substring( lastIndex + 1 )}.{htmlSuffix}" ); + } + + /// + /// 获取页面Html + /// + private async Task GetHtml( PageHandlerSelectedContext context ) { + var serviceProvider = context.HttpContext.RequestServices; + var engine = serviceProvider.GetService(); + var activator = serviceProvider.GetService(); + dynamic model = System.Convert.ChangeType( context.HandlerInstance, context.HandlerInstance.GetType() ); + var actionContext = new ActionContext( context.HttpContext, model.RouteData, context.ActionDescriptor ); + var page = FindPage( engine, context.ActionDescriptor.RelativePath ); + await using var stringWriter = new StringWriter(); + var view = new RazorView( engine, activator, new List(), page, HtmlEncoder.Default, new DiagnosticListener( "ViewRenderService" ) ); + var viewContext = new ViewContext( actionContext, view, model?.ViewData, model?.TempData, stringWriter, new HtmlHelperOptions() ) { + ExecutingFilePath = context.ActionDescriptor.RelativePath + }; + var razorPage = (Page)page; + razorPage.PageContext = model.PageContext; + razorPage.ViewContext = viewContext; + activator.Activate( razorPage, viewContext ); + await page.ExecuteAsync(); + return stringWriter.ToString(); + } + + /// + /// 查找Razor页面 + /// + private IRazorPage FindPage( IRazorViewEngine razorViewEngine, string pageName ) { + var result = razorViewEngine.GetPage( null, pageName ); + return result.Page; + } + + /// + /// 写入文件 + /// + private async Task WriteFile( string path, string html ) { + if( string.IsNullOrWhiteSpace( html ) ) + return; + path = Util.Helpers.Web.GetPhysicalPath( path ); + var directory = Path.GetDirectoryName( path ); + if( string.IsNullOrWhiteSpace( directory ) ) + return; + if( Directory.Exists( directory ) == false ) + Directory.CreateDirectory( directory ); + await File.WriteAllTextAsync( path, html ); + } } \ No newline at end of file diff --git a/test/Util.Aop.AspectCore.Tests/Util.Aop.AspectCore.Tests.csproj b/test/Util.Aop.AspectCore.Tests/Util.Aop.AspectCore.Tests.csproj index b5b46b090..7ff72cb9a 100644 --- a/test/Util.Aop.AspectCore.Tests/Util.Aop.AspectCore.Tests.csproj +++ b/test/Util.Aop.AspectCore.Tests/Util.Aop.AspectCore.Tests.csproj @@ -8,10 +8,10 @@ - + - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Application.EntityFrameworkCore.MySql.Tests.Integration/Util.Application.EntityFrameworkCore.MySql.Tests.Integration.csproj b/test/Util.Application.EntityFrameworkCore.MySql.Tests.Integration/Util.Application.EntityFrameworkCore.MySql.Tests.Integration.csproj index 9adc9e169..f0c20bcd1 100644 --- a/test/Util.Application.EntityFrameworkCore.MySql.Tests.Integration/Util.Application.EntityFrameworkCore.MySql.Tests.Integration.csproj +++ b/test/Util.Application.EntityFrameworkCore.MySql.Tests.Integration/Util.Application.EntityFrameworkCore.MySql.Tests.Integration.csproj @@ -21,8 +21,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Application.EntityFrameworkCore.PostgreSql.Tests.Integration/Util.Application.EntityFrameworkCore.PostgreSql.Tests.Integration.csproj b/test/Util.Application.EntityFrameworkCore.PostgreSql.Tests.Integration/Util.Application.EntityFrameworkCore.PostgreSql.Tests.Integration.csproj index 5b1dbf784..e59ebce1e 100644 --- a/test/Util.Application.EntityFrameworkCore.PostgreSql.Tests.Integration/Util.Application.EntityFrameworkCore.PostgreSql.Tests.Integration.csproj +++ b/test/Util.Application.EntityFrameworkCore.PostgreSql.Tests.Integration/Util.Application.EntityFrameworkCore.PostgreSql.Tests.Integration.csproj @@ -21,8 +21,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Application.EntityFrameworkCore.SqlServer.Tests.Integration/Util.Application.EntityFrameworkCore.SqlServer.Tests.Integration.csproj b/test/Util.Application.EntityFrameworkCore.SqlServer.Tests.Integration/Util.Application.EntityFrameworkCore.SqlServer.Tests.Integration.csproj index ef7ac05ec..baa01a95a 100644 --- a/test/Util.Application.EntityFrameworkCore.SqlServer.Tests.Integration/Util.Application.EntityFrameworkCore.SqlServer.Tests.Integration.csproj +++ b/test/Util.Application.EntityFrameworkCore.SqlServer.Tests.Integration/Util.Application.EntityFrameworkCore.SqlServer.Tests.Integration.csproj @@ -21,8 +21,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Application.Tests/Util.Application.Tests.csproj b/test/Util.Application.Tests/Util.Application.Tests.csproj index 5b4400b99..84fab9391 100644 --- a/test/Util.Application.Tests/Util.Application.Tests.csproj +++ b/test/Util.Application.Tests/Util.Application.Tests.csproj @@ -16,8 +16,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Application.WebApi.MySql.Tests.Integration/Util.Application.WebApi.MySql.Tests.Integration.csproj b/test/Util.Application.WebApi.MySql.Tests.Integration/Util.Application.WebApi.MySql.Tests.Integration.csproj index 830a93103..512da11f4 100644 --- a/test/Util.Application.WebApi.MySql.Tests.Integration/Util.Application.WebApi.MySql.Tests.Integration.csproj +++ b/test/Util.Application.WebApi.MySql.Tests.Integration/Util.Application.WebApi.MySql.Tests.Integration.csproj @@ -12,10 +12,10 @@ - + - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Application.WebApi.PostgreSql.Tests.Integration/Util.Application.WebApi.PostgreSql.Tests.Integration.csproj b/test/Util.Application.WebApi.PostgreSql.Tests.Integration/Util.Application.WebApi.PostgreSql.Tests.Integration.csproj index 9c27966b2..b1fb521c9 100644 --- a/test/Util.Application.WebApi.PostgreSql.Tests.Integration/Util.Application.WebApi.PostgreSql.Tests.Integration.csproj +++ b/test/Util.Application.WebApi.PostgreSql.Tests.Integration/Util.Application.WebApi.PostgreSql.Tests.Integration.csproj @@ -12,10 +12,10 @@ - + - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Application.WebApi.SqlServer.Tests.Integration/Util.Application.WebApi.SqlServer.Tests.Integration.csproj b/test/Util.Application.WebApi.SqlServer.Tests.Integration/Util.Application.WebApi.SqlServer.Tests.Integration.csproj index 57d96acd1..b6e4a1a7a 100644 --- a/test/Util.Application.WebApi.SqlServer.Tests.Integration/Util.Application.WebApi.SqlServer.Tests.Integration.csproj +++ b/test/Util.Application.WebApi.SqlServer.Tests.Integration/Util.Application.WebApi.SqlServer.Tests.Integration.csproj @@ -12,10 +12,10 @@ - + - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Application.WebApi.Tests.Integration/Util.Application.WebApi.Tests.Integration.csproj b/test/Util.Application.WebApi.Tests.Integration/Util.Application.WebApi.Tests.Integration.csproj index c5b78a775..8105e4e88 100644 --- a/test/Util.Application.WebApi.Tests.Integration/Util.Application.WebApi.Tests.Integration.csproj +++ b/test/Util.Application.WebApi.Tests.Integration/Util.Application.WebApi.Tests.Integration.csproj @@ -8,14 +8,14 @@ - + - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.AspNetCore.Tests.Integration/Util.AspNetCore.Tests.Integration.csproj b/test/Util.AspNetCore.Tests.Integration/Util.AspNetCore.Tests.Integration.csproj index 224726ac0..7fb16acab 100644 --- a/test/Util.AspNetCore.Tests.Integration/Util.AspNetCore.Tests.Integration.csproj +++ b/test/Util.AspNetCore.Tests.Integration/Util.AspNetCore.Tests.Integration.csproj @@ -8,13 +8,13 @@ - + - + - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Caching.EasyCaching.Tests.Integration/Util.Caching.EasyCaching.Tests.Integration.csproj b/test/Util.Caching.EasyCaching.Tests.Integration/Util.Caching.EasyCaching.Tests.Integration.csproj index 4b6d7b9c4..62affde8c 100644 --- a/test/Util.Caching.EasyCaching.Tests.Integration/Util.Caching.EasyCaching.Tests.Integration.csproj +++ b/test/Util.Caching.EasyCaching.Tests.Integration/Util.Caching.EasyCaching.Tests.Integration.csproj @@ -12,8 +12,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Core.Tests.Integration/Util.Core.Tests.Integration.csproj b/test/Util.Core.Tests.Integration/Util.Core.Tests.Integration.csproj index 1f84da100..7719142a4 100644 --- a/test/Util.Core.Tests.Integration/Util.Core.Tests.Integration.csproj +++ b/test/Util.Core.Tests.Integration/Util.Core.Tests.Integration.csproj @@ -21,8 +21,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Core.Tests/Util.Core.Tests.csproj b/test/Util.Core.Tests/Util.Core.Tests.csproj index a2bd18dbb..76365bed7 100644 --- a/test/Util.Core.Tests/Util.Core.Tests.csproj +++ b/test/Util.Core.Tests/Util.Core.Tests.csproj @@ -9,8 +9,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Data.Dapper.MySql.Tests.Integration/Util.Data.Dapper.MySql.Tests.Integration.csproj b/test/Util.Data.Dapper.MySql.Tests.Integration/Util.Data.Dapper.MySql.Tests.Integration.csproj index 969b5fd87..d51041840 100644 --- a/test/Util.Data.Dapper.MySql.Tests.Integration/Util.Data.Dapper.MySql.Tests.Integration.csproj +++ b/test/Util.Data.Dapper.MySql.Tests.Integration/Util.Data.Dapper.MySql.Tests.Integration.csproj @@ -19,13 +19,13 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Data.Dapper.PostgreSql.Tests.Integration/Util.Data.Dapper.PostgreSql.Tests.Integration.csproj b/test/Util.Data.Dapper.PostgreSql.Tests.Integration/Util.Data.Dapper.PostgreSql.Tests.Integration.csproj index 10ab1ad7b..f3097ad0e 100644 --- a/test/Util.Data.Dapper.PostgreSql.Tests.Integration/Util.Data.Dapper.PostgreSql.Tests.Integration.csproj +++ b/test/Util.Data.Dapper.PostgreSql.Tests.Integration/Util.Data.Dapper.PostgreSql.Tests.Integration.csproj @@ -19,13 +19,13 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Data.Dapper.SqlServer.Tests.Integration/Util.Data.Dapper.SqlServer.Tests.Integration.csproj b/test/Util.Data.Dapper.SqlServer.Tests.Integration/Util.Data.Dapper.SqlServer.Tests.Integration.csproj index f8f6c9488..05f51f012 100644 --- a/test/Util.Data.Dapper.SqlServer.Tests.Integration/Util.Data.Dapper.SqlServer.Tests.Integration.csproj +++ b/test/Util.Data.Dapper.SqlServer.Tests.Integration/Util.Data.Dapper.SqlServer.Tests.Integration.csproj @@ -19,13 +19,13 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Data.EntityFrameworkCore.MySql.Tests.Integration/Util.Data.EntityFrameworkCore.MySql.Tests.Integration.csproj b/test/Util.Data.EntityFrameworkCore.MySql.Tests.Integration/Util.Data.EntityFrameworkCore.MySql.Tests.Integration.csproj index 3fffc8faa..3ffddc344 100644 --- a/test/Util.Data.EntityFrameworkCore.MySql.Tests.Integration/Util.Data.EntityFrameworkCore.MySql.Tests.Integration.csproj +++ b/test/Util.Data.EntityFrameworkCore.MySql.Tests.Integration/Util.Data.EntityFrameworkCore.MySql.Tests.Integration.csproj @@ -21,8 +21,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Data.EntityFrameworkCore.Oracle.Tests.Integration/Util.Data.EntityFrameworkCore.Oracle.Tests.Integration.csproj b/test/Util.Data.EntityFrameworkCore.Oracle.Tests.Integration/Util.Data.EntityFrameworkCore.Oracle.Tests.Integration.csproj index 083f0ed6c..07dc2e4c2 100644 --- a/test/Util.Data.EntityFrameworkCore.Oracle.Tests.Integration/Util.Data.EntityFrameworkCore.Oracle.Tests.Integration.csproj +++ b/test/Util.Data.EntityFrameworkCore.Oracle.Tests.Integration/Util.Data.EntityFrameworkCore.Oracle.Tests.Integration.csproj @@ -22,8 +22,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Data.EntityFrameworkCore.PostgreSql.Tests.Integration/Util.Data.EntityFrameworkCore.PostgreSql.Tests.Integration.csproj b/test/Util.Data.EntityFrameworkCore.PostgreSql.Tests.Integration/Util.Data.EntityFrameworkCore.PostgreSql.Tests.Integration.csproj index e990d5700..9c0e2cdca 100644 --- a/test/Util.Data.EntityFrameworkCore.PostgreSql.Tests.Integration/Util.Data.EntityFrameworkCore.PostgreSql.Tests.Integration.csproj +++ b/test/Util.Data.EntityFrameworkCore.PostgreSql.Tests.Integration/Util.Data.EntityFrameworkCore.PostgreSql.Tests.Integration.csproj @@ -21,8 +21,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Data.EntityFrameworkCore.SqlServer.Tests.Integration/Util.Data.EntityFrameworkCore.SqlServer.Tests.Integration.csproj b/test/Util.Data.EntityFrameworkCore.SqlServer.Tests.Integration/Util.Data.EntityFrameworkCore.SqlServer.Tests.Integration.csproj index 9371adfb9..a523994f3 100644 --- a/test/Util.Data.EntityFrameworkCore.SqlServer.Tests.Integration/Util.Data.EntityFrameworkCore.SqlServer.Tests.Integration.csproj +++ b/test/Util.Data.EntityFrameworkCore.SqlServer.Tests.Integration/Util.Data.EntityFrameworkCore.SqlServer.Tests.Integration.csproj @@ -30,13 +30,13 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Data.EntityFrameworkCore.Sqlite.Tests.Integration/Util.Data.EntityFrameworkCore.Sqlite.Tests.Integration.csproj b/test/Util.Data.EntityFrameworkCore.Sqlite.Tests.Integration/Util.Data.EntityFrameworkCore.Sqlite.Tests.Integration.csproj index 1c9762950..085687d50 100644 --- a/test/Util.Data.EntityFrameworkCore.Sqlite.Tests.Integration/Util.Data.EntityFrameworkCore.Sqlite.Tests.Integration.csproj +++ b/test/Util.Data.EntityFrameworkCore.Sqlite.Tests.Integration/Util.Data.EntityFrameworkCore.Sqlite.Tests.Integration.csproj @@ -21,8 +21,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Data.Sql.Tests/Util.Data.Sql.Tests.csproj b/test/Util.Data.Sql.Tests/Util.Data.Sql.Tests.csproj index 992ade611..1d5fa3165 100644 --- a/test/Util.Data.Sql.Tests/Util.Data.Sql.Tests.csproj +++ b/test/Util.Data.Sql.Tests/Util.Data.Sql.Tests.csproj @@ -7,8 +7,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Data.Tests/Util.Data.Tests.csproj b/test/Util.Data.Tests/Util.Data.Tests.csproj index 6cd950687..3af312568 100644 --- a/test/Util.Data.Tests/Util.Data.Tests.csproj +++ b/test/Util.Data.Tests/Util.Data.Tests.csproj @@ -7,8 +7,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Domain.Tests/Util.Domain.Tests.csproj b/test/Util.Domain.Tests/Util.Domain.Tests.csproj index 644a1f0a4..5946e57ea 100644 --- a/test/Util.Domain.Tests/Util.Domain.Tests.csproj +++ b/test/Util.Domain.Tests/Util.Domain.Tests.csproj @@ -8,8 +8,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Events.MediatR.Tests.Integration/Util.Events.MediatR.Tests.Integration.csproj b/test/Util.Events.MediatR.Tests.Integration/Util.Events.MediatR.Tests.Integration.csproj index 05f6b32ce..ddf323afb 100644 --- a/test/Util.Events.MediatR.Tests.Integration/Util.Events.MediatR.Tests.Integration.csproj +++ b/test/Util.Events.MediatR.Tests.Integration/Util.Events.MediatR.Tests.Integration.csproj @@ -12,8 +12,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Events.Tests.Integration/Util.Events.Tests.Integration.csproj b/test/Util.Events.Tests.Integration/Util.Events.Tests.Integration.csproj index 6ceed1271..e606ce290 100644 --- a/test/Util.Events.Tests.Integration/Util.Events.Tests.Integration.csproj +++ b/test/Util.Events.Tests.Integration/Util.Events.Tests.Integration.csproj @@ -12,8 +12,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.FileStorage.Minio.Tests.Integration/Util.FileStorage.Minio.Tests.Integration.csproj b/test/Util.FileStorage.Minio.Tests.Integration/Util.FileStorage.Minio.Tests.Integration.csproj index 86af25507..531de47a6 100644 --- a/test/Util.FileStorage.Minio.Tests.Integration/Util.FileStorage.Minio.Tests.Integration.csproj +++ b/test/Util.FileStorage.Minio.Tests.Integration/Util.FileStorage.Minio.Tests.Integration.csproj @@ -8,13 +8,13 @@ - + - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Generators.Razor.Tests.Integration/Util.Generators.Razor.Tests.Integration.csproj b/test/Util.Generators.Razor.Tests.Integration/Util.Generators.Razor.Tests.Integration.csproj index 5e01df39e..5cfd6ee01 100644 --- a/test/Util.Generators.Razor.Tests.Integration/Util.Generators.Razor.Tests.Integration.csproj +++ b/test/Util.Generators.Razor.Tests.Integration/Util.Generators.Razor.Tests.Integration.csproj @@ -18,8 +18,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Generators.Tests/Contexts/GeneratorContextBuilderTest.cs b/test/Util.Generators.Tests/Contexts/GeneratorContextBuilderTest.cs index 3f4051b34..6c5f914d9 100644 --- a/test/Util.Generators.Tests/Contexts/GeneratorContextBuilderTest.cs +++ b/test/Util.Generators.Tests/Contexts/GeneratorContextBuilderTest.cs @@ -1,5 +1,6 @@ using System; using System.Data; +using System.Threading.Tasks; using Util.Data; using Util.Data.Dapper.Metadata; using Util.Generators.Configuration; @@ -38,8 +39,8 @@ public void TestGeneratorContext_Path_1() { /// 测试生成器上下文 - 绝对路径 /// [Fact] - public void TestGeneratorContext_Path_2() { - _context = new GeneratorContextBuilder( NullGeneratorLogger.Instance, new MockGeneratorOptionsBuilder2(), new MockMetadataServiceFactory(),new TypeConverterFactory() ).BuildAsync().Result; + public async Task TestGeneratorContext_Path_2() { + _context = await new GeneratorContextBuilder( NullGeneratorLogger.Instance, new MockGeneratorOptionsBuilder2(), new MockMetadataServiceFactory(),new TypeConverterFactory() ).BuildAsync(); Assert.Equal( @"c:\Templates", _context.TemplateRootPath ); Assert.Equal( @"d:\Output", _context.OutputRootPath ); } diff --git a/test/Util.Generators.Tests/Util.Generators.Tests.csproj b/test/Util.Generators.Tests/Util.Generators.Tests.csproj index b2a05329d..6931de343 100644 --- a/test/Util.Generators.Tests/Util.Generators.Tests.csproj +++ b/test/Util.Generators.Tests/Util.Generators.Tests.csproj @@ -20,8 +20,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Images.Avatar.Tests.Integration/Util.Images.Avatar.Tests.Integration.csproj b/test/Util.Images.Avatar.Tests.Integration/Util.Images.Avatar.Tests.Integration.csproj index daf2a320b..a754fc8ae 100644 --- a/test/Util.Images.Avatar.Tests.Integration/Util.Images.Avatar.Tests.Integration.csproj +++ b/test/Util.Images.Avatar.Tests.Integration/Util.Images.Avatar.Tests.Integration.csproj @@ -12,8 +12,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Images.ImageSharp.Tests.Integration/Util.Images.ImageSharp.Tests.Integration.csproj b/test/Util.Images.ImageSharp.Tests.Integration/Util.Images.ImageSharp.Tests.Integration.csproj index 809d7b611..ee31bacc1 100644 --- a/test/Util.Images.ImageSharp.Tests.Integration/Util.Images.ImageSharp.Tests.Integration.csproj +++ b/test/Util.Images.ImageSharp.Tests.Integration/Util.Images.ImageSharp.Tests.Integration.csproj @@ -11,8 +11,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Localization.Tests.Integration/Util.Localization.Tests.Integration.csproj b/test/Util.Localization.Tests.Integration/Util.Localization.Tests.Integration.csproj index 6000f9ab3..252ebee3a 100644 --- a/test/Util.Localization.Tests.Integration/Util.Localization.Tests.Integration.csproj +++ b/test/Util.Localization.Tests.Integration/Util.Localization.Tests.Integration.csproj @@ -11,10 +11,10 @@ - + - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Logging.Serilog.Exceptionless.Tests.Integration/Util.Logging.Serilog.Exceptionless.Tests.Integration.csproj b/test/Util.Logging.Serilog.Exceptionless.Tests.Integration/Util.Logging.Serilog.Exceptionless.Tests.Integration.csproj index aeb21b559..c8a465796 100644 --- a/test/Util.Logging.Serilog.Exceptionless.Tests.Integration/Util.Logging.Serilog.Exceptionless.Tests.Integration.csproj +++ b/test/Util.Logging.Serilog.Exceptionless.Tests.Integration/Util.Logging.Serilog.Exceptionless.Tests.Integration.csproj @@ -12,8 +12,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Logging.Serilog.Tests.Integration/Util.Logging.Serilog.Tests.Integration.csproj b/test/Util.Logging.Serilog.Tests.Integration/Util.Logging.Serilog.Tests.Integration.csproj index 24ad3fefb..8a84ceb3c 100644 --- a/test/Util.Logging.Serilog.Tests.Integration/Util.Logging.Serilog.Tests.Integration.csproj +++ b/test/Util.Logging.Serilog.Tests.Integration/Util.Logging.Serilog.Tests.Integration.csproj @@ -12,8 +12,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Logging.Tests/Util.Logging.Tests.csproj b/test/Util.Logging.Tests/Util.Logging.Tests.csproj index e05082c5c..9910cc237 100644 --- a/test/Util.Logging.Tests/Util.Logging.Tests.csproj +++ b/test/Util.Logging.Tests/Util.Logging.Tests.csproj @@ -8,8 +8,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Microservices.Dapr.Tests.Integration/Util.Microservices.Dapr.Tests.Integration.csproj b/test/Util.Microservices.Dapr.Tests.Integration/Util.Microservices.Dapr.Tests.Integration.csproj index 1a1b09ba4..696142f3a 100644 --- a/test/Util.Microservices.Dapr.Tests.Integration/Util.Microservices.Dapr.Tests.Integration.csproj +++ b/test/Util.Microservices.Dapr.Tests.Integration/Util.Microservices.Dapr.Tests.Integration.csproj @@ -25,14 +25,14 @@ - + - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Microservices.Dapr.Tests/Util.Microservices.Dapr.Tests.csproj b/test/Util.Microservices.Dapr.Tests/Util.Microservices.Dapr.Tests.csproj index aecc4c508..2dd9bf3e2 100644 --- a/test/Util.Microservices.Dapr.Tests/Util.Microservices.Dapr.Tests.csproj +++ b/test/Util.Microservices.Dapr.Tests/Util.Microservices.Dapr.Tests.csproj @@ -9,8 +9,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Microservices.Polly.Tests.Integration/Util.Microservices.Polly.Tests.Integration.csproj b/test/Util.Microservices.Polly.Tests.Integration/Util.Microservices.Polly.Tests.Integration.csproj index 20263074a..4e091835c 100644 --- a/test/Util.Microservices.Polly.Tests.Integration/Util.Microservices.Polly.Tests.Integration.csproj +++ b/test/Util.Microservices.Polly.Tests.Integration/Util.Microservices.Polly.Tests.Integration.csproj @@ -11,8 +11,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.ObjectMapping.AutoMapper.Tests/Util.ObjectMapping.AutoMapper.Tests.csproj b/test/Util.ObjectMapping.AutoMapper.Tests/Util.ObjectMapping.AutoMapper.Tests.csproj index 2a0cdf8be..84ba6fe68 100644 --- a/test/Util.ObjectMapping.AutoMapper.Tests/Util.ObjectMapping.AutoMapper.Tests.csproj +++ b/test/Util.ObjectMapping.AutoMapper.Tests/Util.ObjectMapping.AutoMapper.Tests.csproj @@ -10,8 +10,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Scheduling.Hangfire.Tests.Integration/Util.Scheduling.Hangfire.Tests.Integration.csproj b/test/Util.Scheduling.Hangfire.Tests.Integration/Util.Scheduling.Hangfire.Tests.Integration.csproj index a4770e3a7..4953779e3 100644 --- a/test/Util.Scheduling.Hangfire.Tests.Integration/Util.Scheduling.Hangfire.Tests.Integration.csproj +++ b/test/Util.Scheduling.Hangfire.Tests.Integration/Util.Scheduling.Hangfire.Tests.Integration.csproj @@ -10,13 +10,13 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Scheduling.Quartz.Tests.Integration/Util.Scheduling.Quartz.Tests.Integration.csproj b/test/Util.Scheduling.Quartz.Tests.Integration/Util.Scheduling.Quartz.Tests.Integration.csproj index 318a904a4..09490c4c9 100644 --- a/test/Util.Scheduling.Quartz.Tests.Integration/Util.Scheduling.Quartz.Tests.Integration.csproj +++ b/test/Util.Scheduling.Quartz.Tests.Integration/Util.Scheduling.Quartz.Tests.Integration.csproj @@ -24,13 +24,13 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Security.Tests/Util.Security.Tests.csproj b/test/Util.Security.Tests/Util.Security.Tests.csproj index 19d8ef116..6cfa435e9 100644 --- a/test/Util.Security.Tests/Util.Security.Tests.csproj +++ b/test/Util.Security.Tests/Util.Security.Tests.csproj @@ -7,8 +7,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Templates.Handlebars.Tests.Integration/Util.Templates.Handlebars.Tests.Integration.csproj b/test/Util.Templates.Handlebars.Tests.Integration/Util.Templates.Handlebars.Tests.Integration.csproj index 124f84872..51b7749b7 100644 --- a/test/Util.Templates.Handlebars.Tests.Integration/Util.Templates.Handlebars.Tests.Integration.csproj +++ b/test/Util.Templates.Handlebars.Tests.Integration/Util.Templates.Handlebars.Tests.Integration.csproj @@ -10,8 +10,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Templates.Razor.Tests.Integration/Util.Templates.Razor.Tests.Integration.csproj b/test/Util.Templates.Razor.Tests.Integration/Util.Templates.Razor.Tests.Integration.csproj index 97e157340..1ad0d664c 100644 --- a/test/Util.Templates.Razor.Tests.Integration/Util.Templates.Razor.Tests.Integration.csproj +++ b/test/Util.Templates.Razor.Tests.Integration/Util.Templates.Razor.Tests.Integration.csproj @@ -10,8 +10,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Tenants.Tests.Integration/Resolvers/DomainTenantResolverTest.cs b/test/Util.Tenants.Tests.Integration/Resolvers/DomainTenantResolverTest.cs index 985410d63..3e695a557 100644 --- a/test/Util.Tenants.Tests.Integration/Resolvers/DomainTenantResolverTest.cs +++ b/test/Util.Tenants.Tests.Integration/Resolvers/DomainTenantResolverTest.cs @@ -8,12 +8,17 @@ public class DomainTenantResolverTest { /// Http客户端 /// private readonly IHttpClient _client; + /// + /// 域名租户解析器 + /// + private readonly IDomainTenantResolver _resolver; /// /// 测试初始化 /// - public DomainTenantResolverTest( IHttpClient client ) { + public DomainTenantResolverTest( IHttpClient client, IDomainTenantResolver resolver ) { _client = client; + _resolver = resolver; } /// @@ -25,4 +30,22 @@ public async Task TestResolveAsync() { .GetResultAsync(); Assert.Equal( "a", result ); } + + /// + /// 测试解析租户标识 - 从域名格式解析 + /// + [Fact] + public async Task TestResolveTenantIdAsync_1() { + var result = await _resolver.ResolveTenantIdAsync( "http://a1.a.test.com" ); + Assert.Equal( "a1", result ); + } + + /// + /// 测试解析租户标识 - 从域名映射解析 + /// + [Fact] + public async Task TestResolveTenantIdAsync_2() { + var result = await _resolver.ResolveTenantIdAsync( "http://b.test.com" ); + Assert.Equal( "b1", result ); + } } \ No newline at end of file diff --git a/test/Util.Tenants.Tests.Integration/Startup.cs b/test/Util.Tenants.Tests.Integration/Startup.cs index 87542c0bb..e0eda4c52 100644 --- a/test/Util.Tenants.Tests.Integration/Startup.cs +++ b/test/Util.Tenants.Tests.Integration/Startup.cs @@ -1,4 +1,6 @@ +using System.Collections.Generic; using Util.Helpers; +using Util.Tenants.Resolvers; namespace Util.Tenants.Tests; @@ -24,7 +26,14 @@ public void ConfigureHost( IHostBuilder hostBuilder ) { } ) .AsBuild() .AddAop() - .AddTenant() + .AddTenant( t => { + var map = new Dictionary { { "b.test.com", "b1" } }; + t.Resolvers.Add( new DomainTenantResolver( map ) { Priority = 90 } ); + t.Resolvers.Add( "a", new DomainTenantResolver( "{0}.a.test.com" ) { Priority = 80 } ); + map = new Dictionary { { "c.test.com", "c1" } }; + t.Resolvers.Add( "b", new DomainTenantResolver( map ) { Priority = 70 } ); + t.Resolvers.Add( "c", new DomainTenantResolver( "{0}.b.test.com" ) { Priority = 100 } ); + } ) .AddUtil(); } diff --git a/test/Util.Tenants.Tests.Integration/Util.Tenants.Tests.Integration.csproj b/test/Util.Tenants.Tests.Integration/Util.Tenants.Tests.Integration.csproj index 6320033a0..2bf202e6c 100644 --- a/test/Util.Tenants.Tests.Integration/Util.Tenants.Tests.Integration.csproj +++ b/test/Util.Tenants.Tests.Integration/Util.Tenants.Tests.Integration.csproj @@ -12,10 +12,10 @@ - + - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Tenants.Tests/Resolvers/DomainTenantResolverTest.cs b/test/Util.Tenants.Tests/Resolvers/DomainTenantResolverTest.cs index 512454c70..c7185d4f3 100644 --- a/test/Util.Tenants.Tests/Resolvers/DomainTenantResolverTest.cs +++ b/test/Util.Tenants.Tests/Resolvers/DomainTenantResolverTest.cs @@ -171,16 +171,44 @@ public async Task TestResolveAsync_8() { } /// - /// 测试解析租户标识 - IDomainTenantResolver 接口解析 + /// 测试解析租户标识 - 租户域名存储器 /// [Fact] public async Task TestResolveAsync_9() { //设置主机名 _mockHttpContext.Setup( t => t.Request.Host ).Returns( new HostString( "https://a.test.com" ) ); - //设置IDomainTenantResolver - var mockResolver = new Mock(); - mockResolver.Setup( t => t.ResolveTenantIdAsync( "a.test.com" ) ).ReturnsAsync( "b" ); + //设置租户域名存储器 + var mockResolver = new Mock(); + mockResolver.Setup( t => t.GetAsync() ).ReturnsAsync( new Dictionary{{ "a.test.com", "b" } } ); + var container = Ioc.CreateContainer(); + container.GetServices().AddTransient( _ => mockResolver.Object ); + _mockHttpContext.Setup( t => t.RequestServices ).Returns( container.GetServiceProvider() ); + + //执行 + var result = await _resolver.ResolveAsync( _mockHttpContext.Object ); + + //验证 + Assert.Equal( "b", result ); + } + + /// + /// 测试解析租户标识 - 租户域名存储器 - 合并设置 + /// + [Fact] + public async Task TestResolveAsync_10() { + //传入域名格式 + var map = new Dictionary { + {"a.test.com","b"} + }; + _resolver = new DomainTenantResolver( map ); + + //设置主机名 + _mockHttpContext.Setup( t => t.Request.Host ).Returns( new HostString( "https://a.test.com" ) ); + + //设置租户域名存储器 + var mockResolver = new Mock(); + mockResolver.Setup( t => t.GetAsync() ).ReturnsAsync( new Dictionary { { "c.test.com", "c" } } ); var container = Ioc.CreateContainer(); container.GetServices().AddTransient( _ => mockResolver.Object ); _mockHttpContext.Setup( t => t.RequestServices ).Returns( container.GetServiceProvider() ); diff --git a/test/Util.Tenants.Tests/Resolvers/TenantResolverCollectionTest.cs b/test/Util.Tenants.Tests/Resolvers/TenantResolverCollectionTest.cs index 4dfc62138..d5a885a6e 100644 --- a/test/Util.Tenants.Tests/Resolvers/TenantResolverCollectionTest.cs +++ b/test/Util.Tenants.Tests/Resolvers/TenantResolverCollectionTest.cs @@ -83,4 +83,52 @@ public void TestRemoveTenantResolver_2() { Assert.Single( result ); Assert.Equal( typeof( Test2TenantResolver ), result[0].GetType() ); } + + /// + /// 测试获取租户解析器 - 按类型获取 + /// + [Fact] + public void TestGetResolver_1() { + _resolvers.Add( new TestTenantResolver() ); + _resolvers.Add( new Test2TenantResolver() ); + var result = _resolvers.GetResolver(); + Assert.Equal( typeof( Test2TenantResolver ), result.GetType() ); + } + + /// + /// 测试获取租户解析器 - 按键获取 + /// + [Fact] + public void TestGetResolver_2() { + _resolvers.Add( "a", new TestTenantResolver() ); + _resolvers.Add( "b", new Test2TenantResolver() ); + var result = _resolvers.GetResolver( "b" ); + Assert.Equal( typeof( Test2TenantResolver ), result.GetType() ); + } + + /// + /// 测试获取租户解析器列表 - 获取全部 + /// + [Fact] + public void TestGetResolvers_1() { + _resolvers.Add( "a", new TestTenantResolver() ); + _resolvers.Add( new Test2TenantResolver() ); + _resolvers.Add( "b", new Test2TenantResolver() ); + var result = _resolvers.GetResolvers(); + Assert.Equal( 3, result.Count ); + } + + /// + /// 测试获取租户解析器列表 - 按类型获取 + /// + [Fact] + public void TestGetResolvers_2() { + _resolvers.Add( "a", new TestTenantResolver() ); + _resolvers.Add( new Test2TenantResolver() ); + _resolvers.Add( "b", new Test2TenantResolver() ); + var result = _resolvers.GetResolvers(); + Assert.Equal( 2, result.Count ); + Assert.Equal( typeof( Test2TenantResolver ), result[0].GetType() ); + Assert.Equal( typeof( Test2TenantResolver ), result[1].GetType() ); + } } \ No newline at end of file diff --git a/test/Util.Tenants.Tests/Util.Tenants.Tests.csproj b/test/Util.Tenants.Tests/Util.Tenants.Tests.csproj index 3a5d06b5b..2b63e53f9 100644 --- a/test/Util.Tenants.Tests/Util.Tenants.Tests.csproj +++ b/test/Util.Tenants.Tests/Util.Tenants.Tests.csproj @@ -8,8 +8,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.TestShare.SqlServer/EntityTypeConfigurations/OperationLogConfiguration.cs b/test/Util.TestShare.SqlServer/EntityTypeConfigurations/OperationLogConfiguration.cs index 9e3f57a62..7c18950e6 100644 --- a/test/Util.TestShare.SqlServer/EntityTypeConfigurations/OperationLogConfiguration.cs +++ b/test/Util.TestShare.SqlServer/EntityTypeConfigurations/OperationLogConfiguration.cs @@ -21,7 +21,7 @@ public void Configure( EntityTypeBuilder builder ) { /// 配置表 /// private void ConfigTable( EntityTypeBuilder builder ) { - builder.ToTable( "OperationLog", "Systems" ).HasComment( "操作日志" ); + builder.ToTable( "OperationLog", "Systems",t => t.HasComment( "操作日志" ) ); } /// diff --git a/test/Util.Ui.NgAlain.Tests/Util.Ui.NgAlain.Tests.csproj b/test/Util.Ui.NgAlain.Tests/Util.Ui.NgAlain.Tests.csproj index 207b0e05e..14371da65 100644 --- a/test/Util.Ui.NgAlain.Tests/Util.Ui.NgAlain.Tests.csproj +++ b/test/Util.Ui.NgAlain.Tests/Util.Ui.NgAlain.Tests.csproj @@ -10,8 +10,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Ui.NgZorro.Tests/Util.Ui.NgZorro.Tests.csproj b/test/Util.Ui.NgZorro.Tests/Util.Ui.NgZorro.Tests.csproj index d7a724ac0..e4908ad9d 100644 --- a/test/Util.Ui.NgZorro.Tests/Util.Ui.NgZorro.Tests.csproj +++ b/test/Util.Ui.NgZorro.Tests/Util.Ui.NgZorro.Tests.csproj @@ -10,8 +10,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Ui.Tests/Util.Ui.Tests.csproj b/test/Util.Ui.Tests/Util.Ui.Tests.csproj index a25b8ec26..ec46980ce 100644 --- a/test/Util.Ui.Tests/Util.Ui.Tests.csproj +++ b/test/Util.Ui.Tests/Util.Ui.Tests.csproj @@ -7,8 +7,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/Util.Validation.Tests/Util.Validation.Tests.csproj b/test/Util.Validation.Tests/Util.Validation.Tests.csproj index 7a859c828..a948dd8f9 100644 --- a/test/Util.Validation.Tests/Util.Validation.Tests.csproj +++ b/test/Util.Validation.Tests/Util.Validation.Tests.csproj @@ -10,8 +10,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all