无法从 ViewComponent 中解析缓存服务
Unable to resolve cache service from within ViewComponent
我正在 Startup.cs 中注册缓存服务,如下所示:
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddTransient<DataFacade, DataFacade>();
...
}
在同一个项目中,我的 _Layout.cshtml header 包含一个 ViewComponent
@await Component.InvokeAsync(nameof(ViewComponents.PageHeader))
这会调用 ModelBuilder 组件并具有如下构造函数:
using MyApp.Services;
using MyApp.Website.ModelBuilder;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
namespace MyApp.Website.ViewComponents
{
[ViewComponent]
public class Header : ViewComponent
{
private readonly HeaderModelBuilder _modelBuilder;
public Header(HeaderModelBuilder modelBuilder)
{
_modelBuilder = modelBuilder;
}
...并且 ModelBuilder 服务引用了 DataFacade 服务:
using MyApp.Data.ExternalCountryData;
using MyApp.Services;
using MyApp.Website.Constants;
using MyApp.Website.Models;
namespace MyApp.Website.ModelBuilder
{
public class HeaderModelBuilder
{
private readonly DataFacade _dataFacade;
public HeaderModelBuilder(DataFacade dataFacade)
{
_dataFacade = dataFacade;
}
...其构造函数如下:
using System;
using MyApp.Data;
using MyApp.Data.ExternalSiteData;
using MyApp.Domain.Services.Data;
using Microsoft.Extensions.Caching.Memory;
namespace MyApp.Services
{
public class DataFacade
{
private MemoryCache _cache;
public DataFacade(MemoryCache memoryCache)
{
_cache = memoryCache;
}
但是,当 运行 代码时,我得到 Program.cs 抛出的以下代码:
InvalidOperationException: Unable to resolve service for type
'Microsoft.Extensions.Caching.Memory.MemoryCache' while attempting to
activate 'MyApp.Services.DataFacade'.
如果我注释掉视图组件,应用程序的其他部分可以正常访问缓存。
为什么视图组件会出现问题?我是否错误地注册了其中一项服务?
注入IMemoryCache
抽象而不是实现
public class DataFacade {
private readonly IMemoryCache _cache;
public DataFacade(IMemoryCache memoryCache) {
_cache = memoryCache;
}
//...
引用Use IMemoryCache
添加内存缓存的源码添加服务集合注册时的接口
/// <summary>
/// Adds a non distributed in memory implementation of <see cref="IMemoryCache"/> to the
/// <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection AddMemoryCache(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
services.AddOptions();
services.TryAdd(ServiceDescriptor.Singleton<IMemoryCache, MemoryCache>());
return services;
}
我正在 Startup.cs 中注册缓存服务,如下所示:
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddTransient<DataFacade, DataFacade>();
...
}
在同一个项目中,我的 _Layout.cshtml header 包含一个 ViewComponent
@await Component.InvokeAsync(nameof(ViewComponents.PageHeader))
这会调用 ModelBuilder 组件并具有如下构造函数:
using MyApp.Services;
using MyApp.Website.ModelBuilder;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
namespace MyApp.Website.ViewComponents
{
[ViewComponent]
public class Header : ViewComponent
{
private readonly HeaderModelBuilder _modelBuilder;
public Header(HeaderModelBuilder modelBuilder)
{
_modelBuilder = modelBuilder;
}
...并且 ModelBuilder 服务引用了 DataFacade 服务:
using MyApp.Data.ExternalCountryData;
using MyApp.Services;
using MyApp.Website.Constants;
using MyApp.Website.Models;
namespace MyApp.Website.ModelBuilder
{
public class HeaderModelBuilder
{
private readonly DataFacade _dataFacade;
public HeaderModelBuilder(DataFacade dataFacade)
{
_dataFacade = dataFacade;
}
...其构造函数如下:
using System;
using MyApp.Data;
using MyApp.Data.ExternalSiteData;
using MyApp.Domain.Services.Data;
using Microsoft.Extensions.Caching.Memory;
namespace MyApp.Services
{
public class DataFacade
{
private MemoryCache _cache;
public DataFacade(MemoryCache memoryCache)
{
_cache = memoryCache;
}
但是,当 运行 代码时,我得到 Program.cs 抛出的以下代码:
InvalidOperationException: Unable to resolve service for type 'Microsoft.Extensions.Caching.Memory.MemoryCache' while attempting to activate 'MyApp.Services.DataFacade'.
如果我注释掉视图组件,应用程序的其他部分可以正常访问缓存。
为什么视图组件会出现问题?我是否错误地注册了其中一项服务?
注入IMemoryCache
抽象而不是实现
public class DataFacade {
private readonly IMemoryCache _cache;
public DataFacade(IMemoryCache memoryCache) {
_cache = memoryCache;
}
//...
引用Use IMemoryCache
添加内存缓存的源码添加服务集合注册时的接口
/// <summary>
/// Adds a non distributed in memory implementation of <see cref="IMemoryCache"/> to the
/// <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection AddMemoryCache(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
services.AddOptions();
services.TryAdd(ServiceDescriptor.Singleton<IMemoryCache, MemoryCache>());
return services;
}