Nancy-Load with Owin

Source: Internet
Author: User
Tags hosting

What is OWIN?

OWIN defines a standard interface between. NET Web servers and Web applications. The goal of the OWIN interface is to decouple server and application, encourage the development of simple modules for. NET Web development, and, by being a open standard, stimulate the open source ecosystem of. NET Web Development tools. For more information visit the official website at http://owin.org/

Katana

Katana is OWIN implementations for Microsoft servers and frameworks.

Katana-asp.net Host

Note:assumes your project is an ASP. Project with Nancy.dll referenced and without any Nancy.Hosting.xxxxx.dll Referen Ced.

    • Install Package Using Nuget
Install-Package Microsoft.Owin.Host.SystemWebInstall-Package Nancy.Owin
    • Add the following key in web.config . (This was required for the current v1.0.1 for Systemweb OWIN host and was likely to being removed in future versions.)
<appSettings>    <add key="owin:HandleAllRequests" value="true"/></appSettings>
    • Create an OWIN startup file
Owin;  Nancy.owin;  Configuration (app) {app. Usenancy (); }}

Note:you must keep the Startup file ' s namespace the same as your application name or it would return a 403.14 - Forbidden .

    • Edit Web . config only if is using PUT, HEAD or DELETE verbs
<system.webserver><Handlers> <RemoveName="Extensionlessurlhandler-integrated-4.0"/> <RemoveName="Optionsverbhandler"/> <Removename= "Traceverbhandler"/> <add name= " Extensionlessurlhandler-integrated-4.0 "path= "verb=" * " Span class= "PL-E" >type= "System.web.handlers.transferrequesthandler "precondition=" Integratedmode, Runtimeversionv4.0 "/></handlers></ System.webserver>             

note! You might has to perform a extra step to get custom static content conventions and diagnostics to work:

    • See Extra steps required when using Microsoft.Owin.Host.SystemWeb
Katana-httplistener (Selfhost)
    • Install Packages using NuGet
Install-Package Microsoft.Owin.HostingInstall-Package Microsoft.Owin.Host.HttpListenerInstall-Package Nancy.Owin
    • Create an OWIN startup file
Owin;   startup{    Configuration (app) {app. Usenancy (); }}
    • Main Entry Point
Microsoft.Owin.Hosting;  program{    Main ("http://+:8080using (webapp.start<startup> (URL)) { Console.WriteLine ("Running on {0}", URL); Console.WriteLine ("Press ENTER to exit"); Console.ReadLine (); } }}
    • Run Visual Studio or the compiled executable in admin mode.

Running without Admin mode

Note that on Windows hosts a is thrown with a HttpListenerException Access Denied message. To resolve the URL of the have to is added to the ACL.

On Windows vista/server/Later, execute the following in PowerShell or CMD running as Administrator:

netsh http add urlacl url=http://+:8080/ user=DOMAIN\username

Replace with DOMAIN\username your domain and username or your computer name and username if is not joined to a domain. See http://msdn.microsoft.com/en-us/library/ms733768.aspx for more information. You can find the user with whoami command.

You can remove the URLACL using the following command.

netsh http delete urlacl url=http://+:8080/

Also but the port could need to being opened on the machine or corporate firewall to allow access to the service.

Accessing OWIN environment variables
PublicClassHomemodule:nancymodule{PublicHomemodule () {get["/"] = x = = {Varenv =This. Context.getowinenvironment ();VarRequestbody = (Stream) env["Owin. Requestbody"];VarRequestheaders = (idictionary<Stringstring[]>) env["Owin. Requestheaders"];VarRequestmethod = (String) env["Owin. Requestmethod"];VarRequestpath = (String) env["Owin. Requestpath"];VarRequestpathbase = (String) env["Owin. Requestpathbase"];VarRequestprotocol = (String) env["Owin. Requestprotocol"];VarRequestquerystring = (String) env["Owin. Requestquerystring"];VarRequestscheme = (String) env["Owin. Requestscheme"];VarResponsebody = (Stream) env["Owin. Responsebody"];VarResponseheaders = (idictionary<Stringstring[]>) env["Owin. Responseheaders"];VarOwinversion = (String) env["Owin. Version"];VarCancellationToken = (CancellationToken) env["Owin. Callcancelled"];VarURI = (String) env["Owin. Requestscheme"] +"://"+ requestheaders[The Hoststring) env[ Owin. Requestpathbase "+ (string) env[ "Owin. Requestpathif (Env[ "Owin. Requestquerystring ""! =  "") URI + =  "" + (string) env[  Owin. Requestquerystringreturn string. Format ( "{0} {1}   
Conditional pass-through

Nancy in an OWIN pipeline are, by default, terminating. If it fails to resolve a handler or static content, it'll complete the request and return a 404. Subsequent middleware won't be invoked. For example, given this Startup ...

Owin;   startup{    Configuration (app) {app. Usenancy (). Useothermiddleware (); }}

... the middleware'll UseOtherMiddleware never be invoked.

In order to configure Nancy to pass-through, you can supply a delegate that's invoked on a per-request basis after it ha s been initially handled by Nancy:

Owin;  Nancy;  Configuration (app) {app. Usenancy (options = options. Performpassthrough = context. Response.statuscode = = Httpstatuscode.notfound). Useothermiddleware (); }}

Here, when Nancy was responding with a 404, the request was passed-through to and UseOtherMiddleware Nancy's response (any headers and Bo DY) is discarded.

There is also a extension helper make it more succinct if you be just dealing with statuscodes for pass-through:

Owin;  Nancy;  Needed to use extension helper. Nancy.owin; Configuration (app) {app. Usenancy (options = options. Passthroughwhenstatuscodesare (Httpstatuscode.notfound, Httpstatuscode.internalservererror)). Useothermiddleware (); }}
Extra steps required when using Microsoft.Owin.Host.SystemWeb

When you were hosting Nancy using you Microsoft.Owin.Host.SystemWeb need some additional the configuration to get it working in IIS.

First of all, in your OWIN you'll Startup.cs need to add app.UseStageMarker(PipelineStage.MapHandler) , for example:

Microsoft.Owin.Extensions;  Owin;  Configuration (app) {app. Usenancy (); App. Usestagemarker (Pipelinestage.maphandler); }}

You'll also has the to add the following to your Web. config:

<system.webServer>    <modules runAllManagedModulesForAllRequests="true" /></system.webServer>

Without this additional configuration, the from StaticFileModule OWIN would intercept the request and return a 404.

Nancy-Load with Owin

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.