コード例 #1
0
        /// <summary>
        /// Initialize<br/>
        /// 初始化<br/>
        /// </summary>
        /// <param name="parentContext">Parent context</param>
        /// <param name="owinRequest">Original http request</param>
        public OwinHttpRequestWrapper(
            OwinHttpContextWrapper parentContext, IOwinRequest owinRequest)
        {
            ParentContext = parentContext;
            OwinRequest   = owinRequest;
            OwinContent   = new StreamContent(OwinRequest.Body);
            var contentType = OwinRequest.ContentType ?? "";

            if (!string.IsNullOrEmpty(contentType))
            {
                OwinContent.Headers.Add("Content-Type", contentType);
            }
            OwinFormCollection = new Lazy <NameValueCollection>(() => {
                if (contentType.StartsWith("application/x-www-form-urlencoded"))
                {
                    return(OwinContent.ReadAsFormDataAsync().Result);
                }
                return(null);
            });
            OwinMultipartFormCollection = new Lazy <Dictionary <string, IList <HttpContent> > >(() => {
                if (contentType.StartsWith("multipart/form-data"))
                {
                    var result   = new Dictionary <string, IList <HttpContent> >();
                    var provider = OwinContent.ReadAsMultipartAsync().Result;
                    foreach (var content in provider.Contents)
                    {
                        var key = content.Headers.ContentDisposition.Name.Trim('"');
                        result.GetOrCreate(key, () => new List <HttpContent>()).Add(content);
                    }
                    return(result);
                }
                return(null);
            });
            HttpContentReadResults = new Dictionary <HttpContent, string>();
        }
コード例 #2
0
        /// <summary>
        /// Configure owin application
        /// </summary>
        /// <param name="app">Owin application</param>
        public virtual void Configuration(IAppBuilder app)
        {
            // Initialize application
            var websiteRootDirectory = app.Properties.GetOrDefault <string>("host.WebsiteRootDirectory");

            websiteRootDirectory = websiteRootDirectory ?? GetWebsiteRootDirectory();
            Application.Ioc.RegisterMany <OwinWebsiteStopper>();
            Application.Initialize(websiteRootDirectory);
            // Configure middlewares
            ConfigureMiddlewares(app);
            // Set request handler, it will running in thread pool
            // It can't throw any exception otherwise application will get killed
            app.Run(owinContext => Task.Run(() => {
                var context = new OwinHttpContextWrapper(owinContext);
                try {
                    // Handle request
                    Application.OnRequest(context);
                } catch (OwinHttpResponseEndException) {
                    // Success
                } catch (Exception ex) {
                    // Error
                    try {
                        Application.OnError(context, ex);
                    } catch (OwinHttpResponseEndException) {
                        // Handle error success
                    } catch (Exception) {
                        // Handle error failed
                    }
                }
            }));
        }
コード例 #3
0
 /// <summary>
 /// Initialize<br/>
 /// 初始化<br/>
 /// </summary>
 /// <param name="parentContext">Parent http context</param>
 /// <param name="owinResponse">Original http response</param>
 public OwinHttpResponseWrapper(
     OwinHttpContextWrapper parentContext, IOwinResponse owinResponse)
 {
     ParentContext = parentContext;
     OwinResponse  = owinResponse;
 }