[英]ASP.NET Core 2.1 - Memory cache - value set in middleware cannot be accessed by controller
我在Asp.Net Core 2.1 Webapi项目中使用IMemoryCache临时存储从RabbitMq通道接收的一些数据。 我在定制的中间件中有一个频道监听器。 但是,当我尝试从控制器访问缓存中的数据时,其中没有数据。 我正在使用Autofac作为我的DI框架。
如果将值设置为控制器中的内存缓存,则可以从其他类中获取该值,例如与此控制器关联的服务(也在Autofac DI中注册)。
我的实现如下:
services.AddMemoryCache();
然后使用中间件作为
app.UseMiddleware<RabbitMqConsumerMiddleware>();
app.UseCors("default")
.UseAuthentication()
.UseMvc();`
我的Autofac注册如下:
这是我的Autofac注册
public IServiceProvider ConfigureServices(IServiceCollection services)
{
RabbitMqConfiguration rabbitMqConfiguration = new RabbitMqConfiguration();
Configuration.GetSection("RabbitMq").Bind(rabbitMqConfiguration);
services.AddSingleton(rabbitMqConfiguration);
services.AddMvcCore()
.AddAuthorization()
.AddJsonFormatters()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization()
.AddControllersAsServices()
.AddApiExplorer();
services.AddRabbitMq(rabbitMqConfiguration);
services.AddMemoryCache();
ContainerBuilder builder = new ContainerBuilder();
builder.Populate(services);
IContainer container = builder.Build();
return new AutofacServiceProvider(container);
}
在中间件中,我有
public RabbitMqConsumerMiddleware(RequestDelegate next, IMemoryCache memoryCache, RabbitMqConfiguration configuration, IModel messageBodyReceiverChannel) { _next = next; _memoryCache = memoryCache; _configuration = configuration; _messageBodyReceiverChannel = messageBodyReceiverChannel; } public async Task Invoke(HttpContext context) { bool isMessageBodyReceiverConsumerExist = _memoryCache.TryGetValue("message-body-receiver-consumer", out EventingBasicConsumer messageBodyReceiverConsumer); if (!isMessageBodyReceiverConsumerExist) { var messageBodyReceiverConsumer = new EventingBasicConsumer(_messageBodyReceiverChannel); messageBodyReceiverConsumer.Received += (ch, ea) => { MessageBody messageBody = JsonConvert.DeserializeObject<MessageBody>(Encoding.Default.GetString(ea.Body)); if (_memoryCache.TryGetValue("Message Body", out List<MessageBody> cacheMessageBodies)) { cacheMessageBodies.Add(messageBody); } else { _memoryCache.Set("Message Body", new List<MessageBody> { messageBody }); } }; _memoryCache.Set("message-body-receiver-consumer", messageBodyReceiverConsumer); } _messageBodyReceiverChannel.BasicConsume(_configuration.MessageReceiverQueue, false, messageBodyReceiverConsumer); await _next(context); }
messageBody
来自RabbitMq的位置
在控制器中我有
[Authorize] [Route("[controller]")] [ApiController] public class MessageBodyController : ControllerBase { private readonly IApplicationService<Dto.MessageBody> _messageBodyPresenter; private readonly IMemoryCache _memoryCache; public MessageBodyController(IApplicationService<Dto.MessageBody> messageBodyPresenter, IMemoryCache memoryCache) { _messageBodyPresenter = messageBodyPresenter; _memoryCache = memoryCache; } [HttpGet] [Route("Stop")] public IActionResult Stop() { ((MessageBodyPresenter)_messageBodyPresenter).Stop(); return Ok(); } }
但是_memoryCache
始终为空。
我在中间件中放置了一个断点,并确认已将数据设置为缓存。
我是否以错误的方式使用内存缓存? 谢谢
不知道您的完整配置,我想一个问题可能是您在Mvc中间件之后注册了新的中间件。
您的注册应如下所示:
app.UseMiddleware<RabbitMqConsumerMiddleware>();
app.UseMvc();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.