What is the Best Practice to configure StackExchange.Redis with Autofac? -


in stackexchange.redis docs recommended create 1 , reuse connection redis.

azure redis best practices recommends using following pattern:

private static lazy<connectionmultiplexer> lazyconnection = new lazy<connectionmultiplexer>(() => {     return connectionmultiplexer.connect("cachename.redis.cache.windows.net,ssl=true,abortconnect=false,password=password"); });  public static connectionmultiplexer connection {         {         return lazyconnection.value;     } } 

but how should working autofac want configuration set in web/app config files?

i have rediscacheprovider:

private readonly connectionmultiplexer _connection;  public rediscacheprovider(string config) {     _connection = connectionmultiplexer.connect(config); } 

and in autofac config:

builder.registertype<rediscacheprovider>().as<icacheprovider>().withparameter("config", "localhost"); 

my thinking is, should change rediscacheprovider take in connectionmultiplexer passed in via static variable?

update: solution far:

my rediscacheprovider (injecting interface here allows me mock connection in unit tests):

private readonly iconnectionmultiplexer _connection;  public rediscacheprovider(iconnectionmultiplexer connection) {    _connection = connection; } 

redisconnection class hold static property , read config config file:

public class redisconnection {     private static readonly lazy<connectionmultiplexer> lazyconnection =          new lazy<connectionmultiplexer>(             () => connectionmultiplexer.connect(configurationmanager.appsettings["rediscache"]));      public static connectionmultiplexer connection     {                 {             return lazyconnection.value;         }     } } 

registration in autofac module:

builder.registertype<rediscacheprovider>().as<icacheprovider>()                     .withparameter(new typedparameter(                                        typeof(iconnectionmultiplexer),                                         redisconnection.connection)); 

autofac supports implicit relationship types , lazy<> evaluation supported out of box. after register rediscacheprovider in example, is

builder .registertype<rediscacheprovider>() .as<icacheprovider>() .withparameter("config", "localhost"); 

you can resolve below:

container.resolve<lazy<icacheprovider>>() 

but not forget default autofac lifetime scope instanceperdependency(transient). new instance of rediscacheprovider everytime resolve or whenever provided other component dependency. fix need specify lifetime scope explicitly. instance make singleton need change registration below:

builder .registertype<rediscacheprovider>() .as<icacheprovider>() .withparameter("config", "localhost") .singleinstance(); 

another assumption here rediscacheprovider component redis connection used. if not case should better let autofac manage redis connection's life scope (which better idea anyway) , connection dependency in rediscacheprovider. is:

public rediscacheprovider(iconnectionmultiplexer connection) {     this.connection = connection; }  ....  builder .register(cx => connectionmultiplexer.connect("localhost")) .as<iconnectionmultiplexer>() .singleinstace();  builder .registertype<rediscacheprovider>() .as<icacheprovider>(); 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -