This is a Redis based ITempDataProvider for ASP.NET MVC written in C# using StackExchange.Redis.
- You can either install using NuGet:
PM> Install-Package Harbour.RedisTempData
- Or build and install from source:
psake.cmd
Psake Tip:
Runpsake.cmd -docs
to get a list of the available tasks, such as build, test, public-package, etc.
In order to publish to Nuget, here are a few simple steps:
-
First you need a nuget package source repository along with an API key and a package source URI for publishing.
-
Now run the
psake publish-package
command from our default.ps1 script, and pass the nuget API key and package source URI from the previous step:.\psake.cmd publish-package -properties "@{nugetApiKey='YOUR_NUGET_API_KEY';nugetSource='NUGET_SOURCE_URI'}"
MVC has multiple ways to configure the ITempDataProvider
:
-
Instantiate per-controller or from a base controller:
public abstract class ApplicationController : Controller { private static readonly ConnectionMultiplexer multiplexer = ConnectionMultiplexer.Connect("localhost"); private readonly IDatabase redis = multiplexer.GetDatabase(0); protected ApplicationController() { TempDataProvider = new RedisTempDataProvider(redis); } } public class HomeController : ApplicationController { public ViewResult Index() { TempData["message"] = "Hello World"; return View(); } }
-
In MVC >= 4, use dependency injection with your favorite IoC/controller factory. This is the preferred method. For example, with the
Ninject
andNinject.MVC3
packages:private static void RegisterServices(IKernel kernel) { kernel.Bind<ConnectionMultiplexer>() .ToMethod(ctx => ConnectionMultiplexer.Connect("localhost")) .InSingletonScope(); kernel.Bind<IDatabase>() .ToMethod(ctx => ctx.Kernel.Get<ConnectionMultiplexer>().GetDatabase(0)) .InRequestScope(); kernel.Bind<ITempDataProvider>() .ToMethod(ctx => { var options = new RedisTempDataProviderOptions() { KeyPrefix = "MyTempData", KeySeparator = "/", // Serializer = new CustomTempDataSerializer(), // UserProvider = new CustomUserProvider() }; return new RedisTempDataProvider(options, ctx.Kernel.Get<IDatabase>()); }) .InRequestScope(); }
IMPORTANT:
Because of a bug in MVC >= 4, you must also ensure that you override
CreateTempDataProvider
in your base controller:protected override ITempDataProvider CreateTempDataProvider() { return DependencyResolver.Current.GetService<ITempDataProvider>(); }
You can configure the options such as serialization and how to identify the current user:
var options = new RedisTempDataProviderOptions()
{
KeyPrefix = "MyTempData",
KeySeparator = "/",
Serializer = new CustomTempDataSerializer(),
UserProvider = new CustomUserProvider()
};
return new RedisTempDataProvider(options, redisClient);
- Targeting only .NET >= 4.6.1. Support for previous .NET versions has been removed.
- Support StackExchange.Redis >= 2.1.30.
- Updated Nuget version from version 2.8.5 to 4.1.0
- Updated psake tooling to support newer .NET versions.
- Switch to using Lua scripts instead of MULTI/EXEC. However, the underlying data structure has not changed.
- This means you must be running Redis 2.6.0.
- Allow overriding how the
DefaultTempDataUserProvider
gets the user from theHttpContextBase
.
- Expose the default fallback cookie name:
DefaultTempDataUserProvider.DefaultFallbackCookieName
- Fix performance issue where loading was waiting on the continuation to deserialize. Instead, we want to wait only on getting the result from Redis.
- Only fall back to the
SessionID
if it's changed since a newSessionID
is generated until the session is used (meaning the TempData would always be new).
- Fall back to identifying an anonymous user to the request's
AnonymousID
if you're using theAnonymousIdentificationModule
.
- Switch the Redis library to StackExchange.Redis.
- Change default serializer to
NetDataContractTempDataSerializer
. You can very easily implement your own serializer by implementingITempDataSerializer
.
- Prevent options from being modified once configured for the provider.
- Initial release.