baseline: 小军 WMS 二开代码原始快照(含 SynOABusiness.cs Debugger.Break)
This commit is contained in:
commit
c2b0e7be0d
6522 files changed
+3530643
No files matched your search
@@ -0,0 +1,23 @@
|
||||
using Opsoft.NetCore.O3App.Domain;
|
||||
using Opsoft.NetCore.O3App.Domain.Models;
|
||||
using System.Threading;
|
||||
|
||||
namespace Opsoft.NetCore.O3App.Infrastructure
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class TraceContextAccessor : ITraceContextAccessor
|
||||
{
|
||||
private static AsyncLocal<TraceContext> traceContext = new AsyncLocal<TraceContext>();
|
||||
private volatile int transactionSequence;
|
||||
|
||||
/// <inheritdoc />
|
||||
public TraceContext TraceContext
|
||||
{
|
||||
get => traceContext.Value;
|
||||
set => traceContext.Value = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public long TransactionSequence => Interlocked.Increment(ref transactionSequence);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using Opsoft.NetCore.O3App.Domain;
|
||||
using Opsoft.NetCore.O3App.Domain.Models;
|
||||
|
||||
namespace Opsoft.NetCore.O3App.Infrastructure
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class TraceContextFactory : ITraceContextFactory
|
||||
{
|
||||
private readonly ITraceContextAccessor traceContextAccessor;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="T:TraceId.TraceContextFactory" /> class.
|
||||
/// </summary>
|
||||
public TraceContextFactory()
|
||||
: this(null)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="TraceContextFactory"/> class.
|
||||
/// </summary>
|
||||
/// <param name="traceContextAccessor">The <see cref="ITraceContextAccessor"/> through which the <see cref="TraceContext"/> will be set.</param>
|
||||
public TraceContextFactory(ITraceContextAccessor traceContextAccessor)
|
||||
{
|
||||
this.traceContextAccessor = traceContextAccessor;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public TraceContext Create(string traceId, string header)
|
||||
{
|
||||
var traceContext = new TraceContext(traceId, header);
|
||||
|
||||
if (traceContextAccessor != null)
|
||||
{
|
||||
traceContextAccessor.TraceContext = traceContext;
|
||||
}
|
||||
|
||||
return traceContext;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Dispose()
|
||||
{
|
||||
if (traceContextAccessor != null)
|
||||
{
|
||||
traceContextAccessor.TraceContext = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Opsoft.NetCore.O3App.Domain;
|
||||
using Opsoft.NetCore.O3App.Shared;
|
||||
using System;
|
||||
|
||||
namespace Opsoft.NetCore.O3App.Infrastructure
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension methods for the TraceIdMiddleware.
|
||||
/// </summary>
|
||||
public static class TraceIdExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Enables Trace IDs for the request.
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseTraceId(this IApplicationBuilder app)
|
||||
{
|
||||
if (app == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(app));
|
||||
}
|
||||
|
||||
return app.UseTraceId(new TraceIdOptions());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enables Trace IDs for the request.
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
/// <param name="header">The header field name to use for the Trace ID.</param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseTraceId(this IApplicationBuilder app, string header)
|
||||
{
|
||||
if (app == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(app));
|
||||
}
|
||||
|
||||
return app.UseTraceId(new TraceIdOptions
|
||||
{
|
||||
Header = header
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Enables Trace IDs for the request.
|
||||
/// </summary>
|
||||
/// <param name="app"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public static IApplicationBuilder UseTraceId(this IApplicationBuilder app, TraceIdOptions options)
|
||||
{
|
||||
if (app == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(app));
|
||||
}
|
||||
|
||||
if (options == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(options));
|
||||
}
|
||||
|
||||
if (app.ApplicationServices.GetService(typeof(ITraceContextFactory)) == null)
|
||||
{
|
||||
throw new InvalidOperationException("Unable to find the required services. You must call the AddTraceId method in ConfigureServices in the application startup code.");
|
||||
}
|
||||
|
||||
return app.UseMiddleware<TraceIdMiddleware>(Microsoft.Extensions.Options.Options.Create(options));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using Opsoft.NetCore.O3App.Domain;
|
||||
using Opsoft.NetCore.O3App.Domain.Models;
|
||||
using Opsoft.NetCore.O3App.Shared;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Opsoft.NetCore.O3App.Infrastructure
|
||||
{
|
||||
/// <summary>
|
||||
/// Middleware which attempts to reads / creates a Trace ID that can then be used in logs and
|
||||
/// passed to upstream requests.
|
||||
/// </summary>
|
||||
public class TraceIdMiddleware
|
||||
{
|
||||
private readonly RequestDelegate next;
|
||||
private readonly TraceIdOptions options;
|
||||
private readonly ITraceContextAccessor traceContextAccessor;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of the TraceIdMiddleware.
|
||||
/// </summary>
|
||||
/// <param name="next">The next middleware in the pipeline.</param>
|
||||
/// <param name="options">The configuration options.</param>
|
||||
public TraceIdMiddleware(RequestDelegate next, IOptions<TraceIdOptions> options, ITraceContextAccessor traceContextAccessor)
|
||||
{
|
||||
this.next = next ?? throw new ArgumentNullException(nameof(next));
|
||||
this.options = options.Value ?? throw new ArgumentNullException(nameof(options));
|
||||
this.traceContextAccessor = traceContextAccessor ?? throw new ArgumentNullException(nameof(traceContextAccessor));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Processes a request to synchronise TraceIdentifier and Trace ID headers. Also creates a
|
||||
/// <see cref="TraceContext"/> for the current request and disposes of it when the request is completing.
|
||||
/// </summary>
|
||||
/// <param name="context">The <see cref="HttpContext"/> for the current request.</param>
|
||||
/// <param name="traceContextFactory">The <see cref="ITraceContextFactory"/> which can create a <see cref="TraceContext"/>.</param>
|
||||
public async Task Invoke(HttpContext context, ITraceContextFactory traceContextFactory)
|
||||
{
|
||||
var traceId = GetTraceId(context);
|
||||
|
||||
if (options.UpdateTraceIdentifier)
|
||||
context.TraceIdentifier = traceId;
|
||||
|
||||
traceContextFactory.Create(traceId, options.Header);
|
||||
|
||||
if (options.IncludeInResponse)
|
||||
{
|
||||
// apply the Trace ID to the response header for client side tracking
|
||||
context.Response.OnStarting(() =>
|
||||
{
|
||||
if (!context.Response.Headers.ContainsKey(options.Header))
|
||||
{
|
||||
context.Response.Headers.Add(options.Header, traceId);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
}
|
||||
|
||||
await next(context);
|
||||
|
||||
traceContextFactory.Dispose();
|
||||
}
|
||||
|
||||
private StringValues GetTraceId(HttpContext context)
|
||||
{
|
||||
var traceIdFoundInRequestHeader = context.Request.Headers.TryGetValue(options.Header, out var traceId);
|
||||
|
||||
if (RequiresGenerationOfTraceId(traceIdFoundInRequestHeader, traceId))
|
||||
traceId = GenerateTraceId(context.TraceIdentifier);
|
||||
|
||||
return traceId;
|
||||
}
|
||||
|
||||
private static bool RequiresGenerationOfTraceId(bool idInHeader, StringValues idFromHeader) =>
|
||||
!idInHeader || StringValues.IsNullOrEmpty(idFromHeader);
|
||||
|
||||
private StringValues GenerateTraceId(string traceIdentifier)
|
||||
{
|
||||
return new TransactionId(
|
||||
TraceUtility.GenerateAgentId(),
|
||||
DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
|
||||
traceContextAccessor.TransactionSequence).ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Opsoft.NetCore.O3App.Domain;
|
||||
|
||||
namespace Opsoft.NetCore.O3App.Infrastructure
|
||||
{
|
||||
/// <summary>
|
||||
/// Extensions on the <see cref="IServiceCollection"/>.
|
||||
/// </summary>
|
||||
public static class TraceIdServiceExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds required services to support the Trace ID functionality.
|
||||
/// </summary>
|
||||
/// <param name="serviceCollection"></param>
|
||||
public static IServiceCollection AddTraceId(this IServiceCollection serviceCollection)
|
||||
{
|
||||
serviceCollection.TryAddSingleton<ITraceContextAccessor, TraceContextAccessor>();
|
||||
serviceCollection.TryAddTransient<ITraceContextFactory, TraceContextFactory>();
|
||||
|
||||
return serviceCollection;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user