public void Configuration(Owin.IAppBuilder app) { // using Owin; you can use UseRequestScopeContext extension method. // enabled timing is according to Pipeline. // so I recommend enable as far in advance as possible. app.UseRequestScopeContext(); app.UseErrorPage(); app.Run(async _ => { // get global context like HttpContext.Current. var context = OwinRequestScopeContext.Current; // Environment is raw Owin Environment as IDictionary<string, object>. var __ = context.Environment; // optional:If you want to change Microsoft.Owin.OwinContext, you can wrap. new Microsoft.Owin.OwinContext(context.Environment); // Timestamp is request started(correctly called RequestScopeContextMiddleware timing). var ___ = context.Timestamp; // Items is IDictionary<string, object> like HttpContext#Items. // Items is threadsafe(as ConcurrentDictionary) by default. var ____ = context.Items; // DisposeOnPipelineCompleted can register dispose when request finished(correctly RequestScopeContextMiddleware underling Middlewares finished) // return value is cancelToken. If call token.Dispose() then canceled register. var cancelToken = context.DisposeOnPipelineCompleted(new TraceDisposable()); // OwinRequestScopeContext over async/await also ConfigureAwait(false) context.Items["test"] = "foo"; await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false); var _____ = OwinRequestScopeContext.Current.Items["test"]; // foo await Task.Run(() => { // OwinRequestScopeContext over new thread/threadpool. var ______ = OwinRequestScopeContext.Current.Items["test"]; // foo }); _.Response.ContentType = "text/plain"; await _.Response.WriteAsync("Hello OwinRequestScopeContext! => "); await _.Response.WriteAsync(OwinRequestScopeContext.Current.Items["test"] as string); // render foo }); }
public void Configuration(Owin.IAppBuilder app) { // enable RedisSession Middleware. // RedisSession app.UseRedisSession(new RedisSessionOptions(new RedisSettings("127.0.0.1"))); app.Run(async context => // request begin, Get all values from Redis server. { // take session from owin environment(IDictionary<string, object>) var session = context.Environment.AsRedisSession(); // TryGet(or Get) take from local storage. DateTime lastAccess; int accessCount = 1; if (session.TryGet<DateTime>("LastAccess", out lastAccess) && session.TryGet<int>("Counter", out accessCount)) { accessCount++; await context.Response.WriteAsync("AccessCount " + accessCount); await context.Response.WriteAsync(", LastAccess from RedisSession => " + lastAccess.ToString()); } else { await context.Response.WriteAsync("First Access"); } // Set(or Remove) set to local storage and enqueue operation. session.Set("Counter", accessCount); session.Set("LastAccess", DateTime.Now); context.Response.ContentType = "text/plain"; }); // request end, queued set(or delete) values to Redis server. // context save pattern, can take everywhere. //app.UseRequestScopeContext(); //app.UseRedisSession(new RedisSessionOptions(new RedisSettings("127.0.0.1"))); //app.Run(async context => //{ // Store(); // int v = -1; // context.Environment.AsRedisSession().TryGet<int>("test", out v); // context.Response.ContentType = "text/plain"; // await context.Response.WriteAsync(v.ToString()); //}); }