Skip to content

Instantly share code, notes, and snippets.

@stevenkuhn
Created February 2, 2012 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevenkuhn/1721753 to your computer and use it in GitHub Desktop.
Save stevenkuhn/1721753 to your computer and use it in GitHub Desktop.
Triggering events in a Windows Service from an ASP.NET site using Redis
private void ProcessEvents() {
using (var client = redisManager.GetClient()) {
// incoming events are JSON, so deserialize each one to IDictionary<>.
var events = client.GetTypedClient<IDictionary<string, object>>().Lists["urn:events"];
while (true) {
// wait for next event, then convert it to an ExpandoObject;
dynamic @event = events.BlockingDequeue(null).ToExpando();
if (@event.Name == "User.ForgotPassword")
SendForgottenPasswordEmail(@event.Email);
}
}
}
kernel.Bind<IRedisClientsManager>()
// BasicRedisClientManager
// - use when the redis server and client are on the same host.
// PooledRedisClientManager
// - use when the redis server and client are on different hosts.
.To<BasicRedisClientManager>()
.InSingletonScope()
.WithConstructorArgument("readWriteHosts", new string[] { "localhost:6379" });
public class UserController : Controller {
private IRedisClientManager redisManager;
[HttpPost]
public ActionResult ForgotPassword(string email) {
using (var client = redisManager.GetClient()) {
// each event is automatically serialized to JSON
var events = client.GetTypedClient<dynamic>().Lists["urn:events"];
events.Enqueue(new
{
Name = "User.ForgotPassword",
Email = email
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment