Unity - 解析 IDistributedCache

问题描述 投票:0回答:1

我正在尝试将 Redis 分布式缓存实现到 Net Framework 4.8 应用程序中。我很不幸不得不降级在 Net Core 6 中运行良好的解决方案,而且我不是 Unity 专家。

我正在这样注册缓存:

Application_Start() -> UnityConfig.RegisterComponents()

ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("connectionString");
container.RegisterInstance(redis);

container.RegisterType<IDistributedCache<FeatureCollection>, GeoDistributedCache>();

当我访问使用注入缓存的端点时,它将响应以下错误消息: “没有可用于类型 Microsoft.Extensions.Caching.Distributed.IDistributedCache 的公共构造函数”

然后我可以追溯到我注入的类:

using Microsoft.Extensions.Caching.Distributed;

namespace Namespace.Caching
{   
    public class GeoDistributedCache : DistributedCache<FeatureCollection>
    {
        public GeoDistributedCache(IDistributedCache cache)
        {
            // other code here...
        }
    }
}

我哪里错了?我应该如何连接 Unity 容器才能使其工作?

unity-container .net-4.8 stackexchange.redis
1个回答
0
投票

我认为你应该做到以下几点:

using Microsoft.Extensions.Caching.Distributed;

namespace Namespace.Caching
{   
    public class GeoDistributedCache
    {
        private IDistributedCache _cache;

        public GeoDistributedCache(IDistributedCache cache)
        {
            _cache = cache;
        }
}

然后安装你的redis缓存包:

dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis
dotnet add package StackExchange.Redis

注册您的 RedisCache:

builder.Services.AddStackExchangeRedisCache(options => {
    options.Configuration = builder.Configuration.GetConnectionString("Redis");
    options.InstanceName = "RedisName";
});
© www.soinside.com 2019 - 2024. All rights reserved.