Nancy's little demo based on Nancy.owin

Source: Internet
Author: User
Tags hosting

A little demo based on Nancy.Hosting.Aspnet and Nancy.Hosting.Self was done earlier.

Today we're going to do a little demo based on Nancy.owin.

Before we start, let's talk about Owin and katana.

What is Owin?

Website address: http://owin.org

Owin defines a standard set of interfaces between. NET Web servers and Web application, and the goal of Owin is to decouple Web server and Web application.

What is Katana?

Website address: http://katanaproject.codeplex.com/

A sharp samurai sword.

A more detailed introduction you can look at Justrun's article:

Next Generation ASP Owin (1)--owin Background and brief introduction

Next Generation ASP Owin (2)--katana Introduction and use

This is not the topic of today, so let's get to the bottom of this.

First, create a new empty ASP. Add the associated assembly through NuGet

Nancy.owin, the protagonist of today. When you add this, Nancy will be added automatically.

Another important role is MICROSOFT.OWIN.HOST.SYSTEMWEB, which also automatically adds dependencies: Owin and Microsoft.owin

This is added through Visual Studio's manage NuGet packages and can be entered in the Package Manager console

Install-package Nancy.owin

Install-package Microsoft.Owin.Host.SystemWeb

To complete the Add.

Third, add Owin Startup Class

We add the code to the configuration in the added Startup.cs and give Nancy to Owin to handle

1      Public class Startup 2     {3public         void  Configuration (iappbuilder app)4          {           5            app. Usenancy (); 6         }7     }
Iv. Add Modules folder to store our modules

Create a new HomeModule.cs in the Modules folder

 1  public  class   Homemodule:nancymodule  2   pub Lic   Homemodule ()  4   { 5  get[ " / ] = _ = = "  owindemo by Catcher Wong   ;  6   7
        } 

Here we'll show the text, and the deployment will be done with the previous moviedemo.

OK, look at the effect!

This is in line with our expected results.

Note: 1), because the demo used here Microsoft.Owin.Host.SystemWeb is 3.0.1 version so no additional processing is required,

If your version is 1.x, please add the following configuration in Web. config

1 <appSettings>2     <add key="owin:handleallrequests" Value="true"/>3 </appSettings>

  Simplified our operation in the new version, it is still very intimate O (∩_∩) o

2), if you need to use a method other than Get, post (Put, Head, Delete) also need to make corresponding changes in the system.webserver node

1<system.webServer>23<remove name="extensionlessurlhandler-integrated-4.0"/>4<remove name="Optionsverbhandler"/>5<remove name="Traceverbhandler"/>6<add name="extensionlessurlhandler-integrated-4.0"Path="*."verb="*"Type="System.Web.Handlers.TransferRequestHandler"precondition="integratedmode,runtimeversionv4.0"/>78</system.webServer>
Five, a variety of operation and commissioning methods, any choice

Here we can see that we are running and debugging or using IIS Express.
Is there any way we can run and debug without IIS Express?
Of course there is!!! And more than one!! Here we will introduce separately!!

Way one: With OwinHost.exe

You can refer to this blog from Glavs: Owin, Katana and Getting Started
Implemented by launching external programs!
Make the following changes in the Web tab in the properties of our project

Just modify the properties.

Run again, you'll find a OwinHost.exe program started, and then we open the browser to see

The effect is not the same Ah, this is the first way!

Way two: With the help of Tinyfox (Jexus's sister)

Here I'm using Tinyfox V2.3.2 cross-platform Universal version

: Tinyfox V2.3.2 Cross-platform Universal Edition

How exactly do you want to do it? Read the extracted ReadMe.txt and see the demo included in the inside also probably know why.

The approach is similar to katana in that it is done by launching an external program.

But the difference is still there.

We need to add a Adapter.cs (this can be found in the Tinyfox after decompression, modify the namespace can!) )

1 /**************************************************************************************2 * = = = Application example running Nancy via Microsoft.Owin.dll = = =3  * ==================================================================================4 * Purpose:5 * Demo If you add Nancyfx to the Microsoft.owin process, then use Nancy to build a web app. 6 * How to use:7 * Compile DLL together with Owin.dll, Microsoft.Owin.dll, Nancy.dll, Nancy.Owin.dll and other files8 * Placed in the bin folder of the website9  *************************************************************************************/Ten  One  A  - #region<USINGs> -  the usingSystem; - usingSystem.Collections.Generic; - usingMicrosoft.Owin.Builder; - usingSystem.Threading.Tasks; +  - #endregion +  A  at  - namespaceOwindemo - { -  -     /// <summary> -     ///JWs Open Interface adapter for Microsoft.owin in     /// <para>this interface is suitable for applications compatible with the Microsoft.owin specification</para> -     /// </summary> to     classAdapter +     { -         Staticfunc<idictionary<string,Object, task>_owinapp; the  *         /// <summary> $         ///default constructorPanax Notoginseng         /// </summary> -          PublicAdapter () the         { +             varBuilder =NewAppBuilder (); A             varstartup =NewStartup (); the startup. Configuration (builder); +_owinapp =Builder. Build (); -  $         } $  -  -         /// <summary> the         ///* * * key functions required by JWS * * * -         /// <para>every request arrives, and JWS packs the request into a dictionary, calling the function</para>Wuyi         /// </summary> the         /// <param name= "env" >New requested environment dictionary, see Owin Standard for details</param> -         /// <returns>returns a task that is running or completed</returns> Wu          PublicTask Owinmain (idictionary<string,Object>env) -         { About             //if it is empty $             if(_owinapp = =NULL)return NULL; -  -             //submit the request to Microsoft.owin for processing -             return_owinapp (env); A         } +  the  -}//End Class $  the  the}//End Namespace

To do this, we are also the web tab content that needs to modify the project properties

Finish the above work and see the effect

The same result!!

Vi. deploy to Linux

As mentioned earlier, here we deploy the previous Moviedemo, here is modified to make it meet our topic today.

This deployment was done with Tinyfox, the little fox. Because the space is a bit long, the modified code is not shown here and will be uploaded to GitHub later.

Here is a list of the project structure and some instructions:

Will find a few more classes than the ones shown above: RazorConfig.cs and SiteRootPath.cs

which

RazorConfig.cs is the razor configuration, because we use this demo to razor this view engine, if it is useless to do without this.

SiteRootPath.cs is a class that provides a physical path to a Web site

These two classes we can find in the Tinyfox demo!

Now we upload post content to/var/www/owinnancy

Create a new folder under/var/www/Owinnancy:mkdir owinnancy

Then go into the Tinyfox directory cd/usr/tinyfox-2.3 execute./fox.sh-root ar/www/owinnancy-p 8181 has started successfully, the following is the visit to see OK No. Open the browser to access this demo.    By the way, put a few pictures to see the effect. A few necessary explanations
1, Woo in the ReadMe.txt is so described (I only intercepted part of the content)
  Q: Where do I put my web files?
A : In the Tinyfox folder, there is a folder called "Site", this folder contains two subdirectories, one is wwwroot, The user holds the traditional Web site file or static file, and the other is AppRoot, which holds the user's assembly and its dependent DLLs, of course, DLL files can also be stored in the Bin folder under Wwwroot; Q: If my Site folder is not in the Tinyfox default location, how to set? Answer: Add a parameter such as "-root path" after TinyFox.exe, such as: "Tinyfox-root d:\mysite\wwwroot" because of the habit problem, I put the website in a folder under the/var/www/, so it takes the form of a custom path.
2, the minor explanation, originally this article is to send in yesterday, but because of something failed to release in time! Besides, yesterday was Kobe Bryant's 10 anniversary on the 81-minute night, so take the 8181 port to commemorate it. O (∩_∩) o The last is the deployment of the Moviedemo, in fact, not much difference from the previous. : Https://github.com/hwqdt/Demos/tree/master/NancyDemoWithOwin/MovieDemoWithOwin Next article will be a little supplement to self hosing, please look forward to it.

Nancy's little demo based on Nancy.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.