Skip to content

Commit

Permalink
Merge pull request #56 from bing-framework/dev_3.1
Browse files Browse the repository at this point in the history
publish: 2.2.1
  • Loading branch information
jianxuanbing authored Feb 8, 2022
2 parents 8096f49 + 1455a05 commit e4f425e
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Bing.AspNetCore.Tracing
/// AspNetCore 跟踪关联ID提供程序
/// </summary>
[Dependency(ServiceLifetime.Transient, ReplaceExisting = true)]
public class AspNetCoreCorrelationIdProvider : ICorrelationIdProvider, ITransientDependency
public class AspNetCoreCorrelationIdProvider : ICorrelationIdProvider
{
/// <summary>
/// Http上下文访问器
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public async Task InvokeAsync(HttpContext context)
TraceIdContext.Current ??= new TraceIdContext(correlationId);
try
{
// TODO: 由于可能存在某个中间件设置了Response导致当前中间件无法设置
CheckAndSetCorrelationIdOnResponse(context, _options, correlationId);
await _next(context);
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,24 @@ public sealed class CreationAuditedInitializer
/// </summary>
private readonly string _userName;

/// <summary>
/// 操作时间
/// </summary>
private readonly DateTime? _dateTime;

/// <summary>
/// 初始化一个<see cref="CreationAuditedInitializer"/>类型的实例
/// </summary>
/// <param name="entity">实体</param>
/// <param name="userId">用户标识</param>
/// <param name="userName">用户名称</param>
private CreationAuditedInitializer(object entity, string userId, string userName)
/// <param name="dateTime">操作时间</param>
private CreationAuditedInitializer(object entity, string userId, string userName, DateTime? dateTime)
{
_entity = entity;
_userId = userId;
_userName = userName;
_dateTime = dateTime;
}

/// <summary>
Expand All @@ -42,7 +49,16 @@ private CreationAuditedInitializer(object entity, string userId, string userName
/// <param name="entity">实体</param>
/// <param name="userId">用户标识</param>
/// <param name="userName">用户名称</param>
public static void Init(object entity, string userId, string userName) => new CreationAuditedInitializer(entity, userId, userName).Init();
public static void Init(object entity, string userId, string userName) => new CreationAuditedInitializer(entity, userId, userName, null).Init();

/// <summary>
/// 初始化
/// </summary>
/// <param name="entity">实体</param>
/// <param name="userId">用户标识</param>
/// <param name="userName">用户名称</param>
/// <param name="dateTime">操作时间</param>
public static void Init(object entity, string userId, string userName, DateTime? dateTime) => new CreationAuditedInitializer(entity, userId, userName,dateTime).Init();

/// <summary>
/// 初始化
Expand All @@ -62,7 +78,7 @@ public void Init()
private void InitCreationTime()
{
if (_entity is IHasCreationTime result)
result.CreationTime = DateTime.Now;
result.CreationTime = _dateTime.HasValue ? _dateTime.SafeValue() : DateTime.Now;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,24 @@ public sealed class ModificationAuditedInitializer
/// </summary>
private readonly string _userName;

/// <summary>
/// 操作时间
/// </summary>
private readonly DateTime? _dateTime;

/// <summary>
/// 初始化一个<see cref="ModificationAuditedInitializer"/>类型的实例
/// </summary>
/// <param name="entity">实体</param>
/// <param name="userId">用户标识</param>
/// <param name="userName">用户名称</param>
private ModificationAuditedInitializer(object entity, string userId, string userName)
/// <param name="dateTime">操作时间</param>
private ModificationAuditedInitializer(object entity, string userId, string userName, DateTime? dateTime)
{
_entity = entity;
_userId = userId;
_userName = userName;
_dateTime = dateTime;
}

/// <summary>
Expand All @@ -42,7 +49,16 @@ private ModificationAuditedInitializer(object entity, string userId, string user
/// <param name="entity">实体</param>
/// <param name="userId">用户标识</param>
/// <param name="userName">用户名称</param>
public static void Init(object entity, string userId, string userName) => new ModificationAuditedInitializer(entity, userId, userName).Init();
public static void Init(object entity, string userId, string userName) => new ModificationAuditedInitializer(entity, userId, userName, null).Init();

/// <summary>
/// 初始化
/// </summary>
/// <param name="entity">实体</param>
/// <param name="userId">用户标识</param>
/// <param name="userName">用户名称</param>
/// <param name="dateTime">操作时间</param>
public static void Init(object entity, string userId, string userName, DateTime? dateTime) => new ModificationAuditedInitializer(entity, userId, userName, dateTime).Init();

/// <summary>
/// 初始化
Expand All @@ -62,7 +78,7 @@ public void Init()
private void InitLastModificationTime()
{
if (_entity is IHasModificationTime result)
result.LastModificationTime = DateTime.Now;
result.LastModificationTime = _dateTime.HasValue ? _dateTime.SafeValue() : DateTime.Now;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public class DependsOnModuleAttribute : Attribute
/// 初始化一个<see cref="DependsOnModuleAttribute"/>类型的实例
/// </summary>
/// <param name="dependedModuleTypes">依赖模块类型集合</param>
public DependsOnModuleAttribute(params Type[] dependedModuleTypes) => DependedModuleTypes = dependedModuleTypes;
public DependsOnModuleAttribute(params Type[] dependedModuleTypes) => DependedModuleTypes = dependedModuleTypes ?? new Type[0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ protected virtual void AddToServices(IServiceCollection services, Type implement
{
if (implementationType.IsAbstract || implementationType.IsInterface)
return;

var lifetime = GetLifetimeOrNull(implementationType);
if (lifetime == null)
return;

var dependencyAttribute = implementationType.GetAttribute<DependencyAttribute>();
var serviceTypes = GetImplementedInterfaces(implementationType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ public class DependencyTypeFinder : FinderBase<Type>, IDependencyTypeFinder
/// </summary>
protected override Type[] FindAllItems()
{
var baseTypes = new[]
{typeof(ISingletonDependency), typeof(IScopedDependency), typeof(ITransientDependency)};
var types = _allAssemblyFinder.FindAll(true).SelectMany(assembly => assembly.GetTypes().Where(type =>
type.IsClass && !type.IsAbstract && !type.IsInterface &&
!type.HasAttribute<IgnoreDependencyAttribute>() &&
(baseTypes.Any(b => b.IsAssignableFrom(type)) || type.HasAttribute<DependencyAttribute>()))).ToArray();
var baseTypes = new[] {typeof(ISingletonDependency), typeof(IScopedDependency), typeof(ITransientDependency)};
var types = _allAssemblyFinder.FindAll(true)
.SelectMany(assembly => assembly
.GetTypes()
.Where(type => type.IsClass && !type.IsAbstract && !type.IsInterface
&& !type.HasAttribute<IgnoreDependencyAttribute>()
&& (baseTypes.Any(b => b.IsAssignableFrom(type)) || type.HasAttribute<DependencyAttribute>())))
.ToArray();
return types;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System;
using Bing.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

namespace Bing.Tracing
{
/// <summary>
/// 默认跟踪标识提供程序
/// </summary>
internal class DefaultCorrelationIdProvider : ICorrelationIdProvider, ISingletonDependency
[Dependency(ServiceLifetime.Singleton, TryAdd = true)]
public class DefaultCorrelationIdProvider : ICorrelationIdProvider
{
/// <summary>
/// 跟踪标识提供程序
Expand Down
2 changes: 1 addition & 1 deletion version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<VersionMajor>2</VersionMajor>
<VersionMinor>2</VersionMinor>
<VersionPatch>0</VersionPatch>
<VersionPatch>1</VersionPatch>
<VersionQuality>20220104-1</VersionQuality>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
<!--<VersionSuffix>preview-$(VersionQuality)</VersionSuffix>-->
Expand Down

0 comments on commit e4f425e

Please sign in to comment.