asp.net-mvc - 更新 nuget 后 StackExchange.Redis.Extensions 错误
问题描述
我已更新到最新的 StackExchange.Redis.Extensions.Core 包,但出现以下错误。
StackExchange.Redis.Extensions.Core 之前的版本是 - 2.3.0,更新后的新版本是 - 3.4.0
None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'StackExchange.Redis.Extensions.Core.StackExchangeRedisCacheClient' can be invoked with the available services and parameters:
Cannot resolve parameter 'StackExchange.Redis.Extensions.Core.Configuration.RedisConfiguration configuration' of constructor 'Void .ctor(StackExchange.Redis.Extensions.Core.ISerializer, StackExchange.Redis.Extensions.Core.Configuration.RedisConfiguration)'.
Cannot resolve parameter 'System.String connectionString' of constructor 'Void .ctor(StackExchange.Redis.Extensions.Core.ISerializer, System.String, System.String)'.
Cannot resolve parameter 'System.String connectionString' of constructor 'Void .ctor(StackExchange.Redis.Extensions.Core.ISerializer, System.String, Int32, System.String)'.
Cannot resolve parameter 'StackExchange.Redis.IConnectionMultiplexer connectionMultiplexer' of constructor 'Void .ctor(StackExchange.Redis.IConnectionMultiplexer, StackExchange.Redis.Extensions.Core.ISerializer, System.String)'.
Cannot resolve parameter 'StackExchange.Redis.IConnectionMultiplexer connectionMultiplexer' of constructor 'Void .ctor(StackExchange.Redis.IConnectionMultiplexer, StackExchange.Redis.Extensions.Core.ISerializer, StackExchange.Redis.Extensions.Core.Configuration.ServerEnumerationStrategy, System.String)'.
Cannot resolve parameter 'StackExchange.Redis.IConnectionMultiplexer connectionMultiplexer' of constructor 'Void .ctor(StackExchange.Redis.IConnectionMultiplexer, StackExchange.Redis.Extensions.Core.ISerializer, Int32, System.String)'.
Cannot resolve parameter 'StackExchange.Redis.IConnectionMultiplexer connectionMultiplexer' of constructor 'Void .ctor(StackExchange.Redis.IConnectionMultiplexer, StackExchange.Redis.Extensions.Core.ISerializer, StackExchange.Redis.Extensions.Core.Configuration.ServerEnumerationStrategy, Int32, System.String)'.
我已经安装了以下 nuget 包:
Microsoft.Web.RedisSessionStateProvider
StackExchange.Redis
StackExchange.Redis.StrongName
StackExchange.Redis.Extensions.Core
StackExchange.Redis.Extensions.LegacyConfiguration
StackExchange.Redis.Extensions.Newtonsoft
我的 web.config 如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="redisCacheClient" type="StackExchange.Redis.Extensions.LegacyConfiguration.RedisCachingSectionHandler, StackExchange.Redis.Extensions.LegacyConfiguration" />
</configSections>
<redisCacheClient allowAdmin="true" ssl="True" connectTimeout="3000" database="15" password="IlQZ--------90=">
<serverEnumerationStrategy mode="Single" targetRole="PreferSlave" unreachableServerAction="IgnoreIfOtherAvailable" />
<hosts>
<add host="xxxxxx.redis.cache.windows.net" cachePort="6380" />
</hosts>
</redisCacheClient>
<connectionStrings>
<add name="RedisConnectionString" connectionString="xxxx.redis.cache.windows.net:6380,password=IlQZ--------90=,ssl=True,synctimeout=15000,abortConnect=False" />
</connectionStrings>
<appSettings>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
</system.web>
<runtime>
<assemblyBinding
xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.8.1.0" newVersion="4.8.1.0" />
</dependentAssembly>
<!-- more dependentAssembly -->
<dependentAssembly>
<assemblyIdentity name="StackExchange.Redis.Extensions.Core" publicKeyToken="d7d863643bcd13ef" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.4.0.0" newVersion="3.4.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
我的带有 autofac 的 OWIN 启动类如下:
public void Configuration(IAppBuilder app)
{
var builder = new ContainerBuilder();
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterType<NewtonsoftSerializer>()
.AsSelf()
.AsImplementedInterfaces()
.SingleInstance();
builder.RegisterType<StackExchangeRedisCacheClient>()
.AsSelf()
.AsImplementedInterfaces()
.SingleInstance();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
app.UseAutofacMiddleware(container);
app.UseAutofacMvc();
}
我在这里做错了什么?
解决方案
Autofac 无法解决您没有告诉它的任何问题。你已经告诉过它:
- 你的控制器。
NewtonsoftSerializer
StackExchangeRedisClient
但是请阅读异常 - 没有无参数构造函数,StackExchangeRedisClient
并且您没有告诉 Autofac 其他任何事情。Autofac 不只是“读取 web.config”或任何东西。你必须把它放在一起。再次读取异常并注册足够的内容,以便构造函数之一匹配。
这不是真正的代码,但给你一个想法:
// THIS ISN'T TESTED, FOR IDEA PURPOSES ONLY
builder.Register(c => {
var config = ConfigurationManager.GetSection("RedisCacheClient");
var connectionString = config.ConnectionStrings[0];
return new StackExchangeRedisClient(connectionString);
}).AsSelf()
.AsImplementedInterfaces()
.SingleInstance();
重点是,您必须阅读配置,或在容器中注册一些内容,否则将满足创建 Redis 客户端所需的参数。
推荐阅读
- bash - 需要在新行中替换值
- angular - NgbTypeahead 在 ngbBootstrap10.0.0 中不起作用
- python - 什么时候最好将变量传递给方法与在 python 中读取属性
- c - C中的快速排序分配
- docker - 多个 docker 镜像之间的工作项分布
- java - NetSuite 自动显示列表后无法单击下拉列表选项
- python - 对嵌入层的 mask_zero=True 使用带有 class_id 变量的召回指标
- visual-studio - 纱线不存在 - Visual Code Studio 中的终端与 Mac 中的终端不同
- hadoop - 如何计算区块链/IPFS 和 Hadoop/HDFS 的可用性?
- python - 如何在寡妇上使用来自 github 的 Python 程序