Permalink
Browse files

Exposed GetHttpContext extension method from SystemWeb assembly.

  • Loading branch information...
1 parent 7f4969c commit 6ea4b20e872d8116113791c15a52dbda68d27b07 @davidfowl davidfowl committed Dec 14, 2012
@@ -13,13 +13,15 @@ public partial class ServerRequest
{
private readonly IDictionary<string, object> _environment;
+ public static readonly string OwinEnvironmentKey = "owin.environment";
+
public ServerRequest(IDictionary<string, object> environment)
{
_environment = environment;
Items = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase)
{
- { "owin.environment" , _environment }
+ { OwinEnvironmentKey , _environment }
};
}
@@ -48,6 +48,7 @@
<Link>Properties\CommonVersionInfo.cs</Link>
</Compile>
<Compile Include="Infrastructure\BuildManagerAssemblyLocator.cs" />
+ <Compile Include="RequestExtensions.cs" />
<Compile Include="RouteExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+using System.Web;
+using Microsoft.AspNet.SignalR.Owin;
+
+namespace Microsoft.AspNet.SignalR
+{
+ public static class RequestExtensions
+ {
+ public static HttpContextBase GetHttpContext(this IRequest request)
+ {
+ if (request == null)
+ {
+ throw new ArgumentNullException("request");
+ }
+
+ var env = request.Items.Get<IDictionary<string, object>>(ServerRequest.OwinEnvironmentKey);
+
+ if (env == null)
+ {
+ // Owin environment not detected
+ return null;
+ }
+
+ // Try to grab the HttpContextBase from the environment
+ return env.Get<HttpContextBase>(typeof(HttpContextBase).FullName);
+ }
+
+ private static T Get<T>(this IDictionary<string, object> values, string key)
+ {
+ object value;
+ return values.TryGetValue(key, out value) ? (T)value : default(T);
+ }
+ }
+}
@@ -55,6 +55,9 @@
<Compile Include="..\Microsoft.AspNet.SignalR.SystemWeb\Infrastructure\BuildManagerAssemblyLocator.cs">
<Link>Infrastructure\BuildManagerAssemblyLocator.cs</Link>
</Compile>
+ <Compile Include="..\Microsoft.AspNet.SignalR.SystemWeb\RequestExtensions.cs">
+ <Link>RequestExtensions.cs</Link>
+ </Compile>
<Compile Include="..\Microsoft.AspNet.SignalR.SystemWeb\RouteExtensions.cs">
<Link>RouteExtensions.cs</Link>
</Compile>

0 comments on commit 6ea4b20

Please sign in to comment.