I have a web app and a background service that processes messages from Redis. However, I’m unsure as to whether or not the web application’s RedisMqServer should be configured as a singleton (I’m using Ninject as my IoC container). Each request that comes is will need to send messages to the background service (one-way), but I’m not sure it it should be instantiated per-request or per-application.
I was thinking that the container would be configured like this:
var clientManager = new PooledRedisClientManager();
var mqHost = new RedisMqHost(clientManager);
Bind<IMessageProducer>()
.ToMethod(_ => mqHost.MessageFactory.CreateMessageProducer())
.InRequestScope();
Or maybe the RedisMqHost/RedisMqServer isn’t necessary when the messages are one-way? Therefore, reducing the configuration to:
var clientManager = new PooledRedisClientManager();
Bind<IMessageProducer>()
.ToMethod(_ => new RedisMessageProducer(clientManager))
.InRequestScope();
You don’t actually need to register the
IMessageServiceif your services don’t need access to the host directly. But if you do end up using it, then Yes it should be registered as a singleton.The only thing that needs to be registered is
IMessageFactory. In this case RequestScope is the same as Ninject’s default TransientScope since if it’s being used, it’ll only ever be resolved once per request, in your Service class.The
IMessageFactoryis used in the base Service to lazily load aIMessageProducerso you can publish a message in your services with:Note: You’re using
RedisMqHostin code which process all messages on a single background thread. Changing to useRedisMqServerwill create a background thread for each message type, allowing processing of different messages to happen in parallel.