@@ -9,6 +9,7 @@ open System.Text
99open System
1010open System.IO
1111open System.Text .RegularExpressions
12+ open Microsoft.AspNetCore .Http .Features
1213
1314// setup something that reminds us of what Suave can work with
1415// this is an overly simplified model of Suave in order to show how OptionT can be used
@@ -87,10 +88,15 @@ module RequestErrors=
8788 let FORBIDDEN s = setStatusAndContent ( int HttpStatusCode.Forbidden) s
8889 let NOT_FOUND s = setStatusAndContent ( int HttpStatusCode.NotFound) s
8990 let UNAUTHORIZED s = setStatusAndContent ( int HttpStatusCode.Unauthorized) s
91+ let private tryGetSession ( ctx : Context ) =
92+ match ctx.request.HttpContext.Features.Get< ISessionFeature>() with
93+ | null -> None
94+ | feature when isNull feature.Session -> None
95+ | feature -> Some feature.Session
9096module Filters =
9197 let response ( method : string ) = OptionT << fun ( x : Context ) -> async.Return ( if ( method = x.request.Method) then Some x else None)
9298 let hasFormContentType = OptionT << fun ( x : Context ) -> async.Return ( if x.request.HasFormContentType then Some x else None)
93- let statefulForSession = OptionT << fun ( x : Context ) -> async.Return ( Some x)
99+ let statefulForSession = OptionT << fun ( x : Context ) -> async.Return ( if tryGetSession x |> Option.isSome then Some x else None )
94100
95101 let GET ( x : Http.Context ) = response " GET" x
96102 let POST ( x : Http.Context ) = response " POST" x
@@ -127,12 +133,7 @@ module Request =
127133 | _ -> None
128134
129135module HttpContext =
130- let state ( ctx : Context ) =
131- try
132- let session = ctx.request.HttpContext.Session
133- Some session
134- with
135- | :? InvalidOperationException -> None
136+ let state ( ctx : Context ) = tryGetSession ctx
136137
137138module Session =
138139 let tryGet key ( session : ISession ) =
0 commit comments