Gra*_*zar 13 .net-core callcontext
我试图将.net核心转移到使用CallContext.LogicalGet/SetData的现有.net应用程序.
当Web请求到达应用程序时,我在CallContext中保存了CorrelationId,每当我需要稍后在轨道上记录某些内容时,我可以轻松地从CallContext中收集它,而无需在任何地方传输它.
因为.net核心不再支持CallContext,因为它是System.Messaging的一部分.修改了哪些选项?
我看到的一个版本是可以使用AsyncLocal(AsyncLocal的语义与逻辑调用上下文有什么不同?)但看起来好像我必须传输这个变量,而这个变量超出了目的,它不是方便.
kzu*_*kzu 13
您可以使用 AsyncLocal 字典来准确模拟原始 CallContext 的 API 和行为。有关完整的实现示例,请参见http://www.cazzulino.com/callcontext-netstandard-netcore.html。
当我们将库从.Net Framework切换到.Net Standard并不得不替换System.Runtime.Remoting.Messaging CallContext.LogicalGetData和时出现了此问题CallContext.LogicalSetData。
我按照本指南替换了方法:
http://www.cazzulino.com/callcontext-netstandard-netcore.html
/// <summary>
/// Provides a way to set contextual data that flows with the call and
/// async context of a test or invocation.
/// </summary>
public static class CallContext
{
static ConcurrentDictionary<string, AsyncLocal<object>> state = new ConcurrentDictionary<string, AsyncLocal<object>>();
/// <summary>
/// Stores a given object and associates it with the specified name.
/// </summary>
/// <param name="name">The name with which to associate the new item in the call context.</param>
/// <param name="data">The object to store in the call context.</param>
public static void SetData(string name, object data) =>
state.GetOrAdd(name, _ => new AsyncLocal<object>()).Value = data;
/// <summary>
/// Retrieves an object with the specified name from the <see cref="CallContext"/>.
/// </summary>
/// <param name="name">The name of the item in the call context.</param>
/// <returns>The object in the call context associated with the specified name, or <see langword="null"/> if not found.</returns>
public static object GetData(string name) =>
state.TryGetValue(name, out AsyncLocal<object> data) ? data.Value : null;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3839 次 |
| 最近记录: |