Ошибка StackExchange.Redis.Extensions после обновления nuget - PullRequest
0 голосов
/ 23 июня 2018

Я обновился до последней версии пакета 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>

И мой класс запуска OWIN с autofac выглядит следующим образом:

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();
}

Что я здесь не так делаю?

Ответы [ 2 ]

0 голосов
/ 05 октября 2018

Проблема в том, что у Autofac нет необходимой информации для создания объекта, поэтому я согласен с @ travis-illig.Я решил, что сначала нужно зарегистрировать необходимые типы:

builder.RegisterType<JilSerializer>()
       .As<ISerializer>()
       .SingleInstance();

RedisConfiguration redisConfiguration = RedisUtil.GetConfig();
builder.RegisterInstance(redisConfiguration);

builder.RegisterType<StackExchangeRedisCacheClient>()
       .As<ICacheClient>()
       .SingleInstance();

Примечание. У меня есть класс Util, который создает Redisconfiguration.

0 голосов
/ 25 июня 2018

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.

...