asp.net-core - 如何访问/传递来自不同范围的数据
问题描述
出于某种原因,我需要从另一个范围中设置的不同范围访问数据。请告知如何实现这一点。这是我在一个范围内设置 CallContext 值并希望从新创建的范围中获取相同值的相同代码。
public class CallContext
{
ConcurrentDictionary<string, AsyncLocal<object>> state = new ConcurrentDictionary<string, AsyncLocal<object>>();
public void SetData(string name, object data) =>
state.GetOrAdd(name, _ => new AsyncLocal<object>()).Value = data;
public object GetData(string name) =>
state.TryGetValue(name, out AsyncLocal<object> data) ? data.Value : null;
}
public interface ICustomerService
{
void Log();
}
public class CustomerService : ICustomerService
{
private CallContext _callContext;
private static IHttpContextAccessor _httpContextAccessor;
private IServiceScopeFactory _serviceScopeFactory;
public CustomerService(CallContext callContext, IHttpContextAccessor httpContextAccessor, IServiceScopeFactory serviceScopeFactory)
{
_callContext = callContext;
_httpContextAccessor = httpContextAccessor;
_serviceScopeFactory = serviceScopeFactory;
}
public void Log()
{
var context = _httpContextAccessor.HttpContext;
_callContext.SetData("ThreadId", context.TraceIdentifier);
_callContext.SetData("URL", context?.Request?.Scheme + "://" + context?.Request?.Host.Value + context?.Request?.Path.Value);
// For some reason I need to create a new scope
using (var scope = _serviceScopeFactory.CreateScope())
{
var customerPortalService = scope.ServiceProvider.GetRequiredService<ISecondCustomerService>();
customerPortalService.Log();
}
}
}
public interface ISecondCustomerService
{
void Log();
}
public class SecondCustomerService : ISecondCustomerService
{
private CallContext _callContext;
public SecondCustomerService(CallContext callContext)
{
_callContext = callContext;
}
public void Log()
{
var threadId = _callContext.GetData("ThreadId");
var url = _callContext.GetData("URL");
}
}
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddTransient<IHttpContextAccessor, HttpContextAccessor>();
services.AddScoped<CallContext>();
services.AddScoped<ICustomerService, CustomerService>();
services.AddScoped<ISecondCustomerService, SecondCustomerService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly ICustomerService customerService;
public WeatherForecastController(ICustomerService _customerService)
{
customerService = _customerService;
}
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public ActionResult Log()
{
customerService.Log();
return Ok();
}
}
在上面的代码中,我在 CustomerServe 的 CallContext 中设置 ThreadID 和 URL 值,并希望从在 CustomerService 中创建的新范围内的 SecondCustomerService 获得相同的值。
解决方案
推荐阅读
- javascript - 计算 4x4 矩阵的行列式 - (RayTracer 挑战) - Javascript
- python - 这个找不到媒体目录的 Nginx 配置有什么问题?
- php - 查询总是失败
- css - CSS 继承与 CSS 特异性
- python - 如何从函数中获取矩阵输出?
- javascript - React Router v4 未加载组件
- javascript - 如何使用 javascript 在 html 中搜索特定文本或“字符串”?(镀铬扩展)
- mysql - 在mysql中减去两次不同的日期
- javascript - 不确定如何解析数组中的字符串
- regex - 两个引号之间的 Google 表格 REEXTRACT