Deployment of ASP. NET Linux (2) and asp. netlinux
ASP. NET Linux deployment (2)-MS Owin + WebApi + Mono + Jexus
This article undertakes my previous blog post: ASP. NET 5 Linux deployment, the article is mainly for the latest ASP. NET 5, but in subsequent studies, I was still not very satisfied with the deployment of this entertainment model, of course, the main reason is that ASP. NET 5 is still in the RC version, not very mature. but it is foreseeable that, even this month, ASP. the NET 5 RTM version was launched on schedule, and its development and deployment prospects on Linux are still not very clear: what is particularly confusing is that, MS has released only a few simple Server implementations for development in Linux, so it is difficult to find a complete deployment environment similar to IIS in its plan. So the so-called ASP. NET 5 cross-platform development can only stay at the lab level? Is there a better choice for a long period of time (until ASP. NET 5 is fully built on Linux? I will give my own ideas below.
Here, we first declare that the ASP. NET Linux deployment series only targets the Linux deployment environment and does not involve the Windows deployment environment. Here are some concepts to help you better understand the subsequent content.
ASP. NET |
ASP. NET is. NET Framework is a Microsoft technology. It is a server-side scripting technology that allows scripts embedded in web pages to be executed by Internet servers. The latest version to be released this month is version 5, it becomes vNext. |
Linux |
Linux is a free and free-to-use Unix-like operating system. It is a POSIX and UNIX-based multi-user, multi-task, multi-thread and multi-CPU operating system. linux in this article mainly uses Ubuntu as an example. |
Mono |
Mono is initiated by Novell (initiated by Xamarin and led by Miguel de lcaza), a company dedicated to creating mono.. NET open source project used on Linux. currently. NET Applications must also be run based on Mono. |
Jexus |
Jexus, Jexus Web Server (JWS), is an ASP. net web server is currently the only one that supports enterprise-level ASP.. NET (other server solutions do not have similar positioning ). |
OWIN |
OWIN defines a set of standard interfaces between. NET Web Servers and Web applications. The goal of OWIN is to decouple Web Server and Web Application. Based on this standard, developers are encouraged to develop simple and flexible modules to Promote the Development of the. NET Web Development open-source ecosystem. |
MS Owin |
The underlying implementation based on the OWIN specification developed by Microsoft. The latest version is 3.0.1 and its main project name is Kanata. |
ASP. NET WebApi |
ASP. net mvc 4 contains ASP. NET Web API, which is a new framework for creating Http services that can connect to browsers, mobile devices, and other clients. ASP. NET Web API is also an ideal platform for building RESTful services |
RESTful |
A software architecture style, design style rather than standard, provides a set of design principles and constraints. It is mainly used for client-to-server interaction software. The software based on this style can be simpler, more hierarchical, and easier to implement caching and other mechanisms. |
NancyFx |
Nancy is a lightweight HTTP-based Web service built on. NET and Mono platforms. Based on the. NET and Mono platforms, the framework aims to maintain as many methods as possible and provide a super-duper-happy-path for all interactions. Official Website http://nancyfx.org/ |
Three options
Just. from the perspective of Web development on the NET route, No matter what method, the future will inevitably be based on OWIN development. On this basis, I think the development and deployment on Linux are currently in progress. NET Web applications have three routes to choose from:
In terms of these three schemes, I think they have their own advantages and disadvantages: The underlying scheme requires more options and assembly, but can be used with any Owin-based components. The three-party Scheme faces the ecological environment problems, because most of the high-end components come from MS, whether they can be seamlessly connected requires a test, and their viability is also a problem. The Orthodox solution has complete content and powerful support, and is highly integrated with various MS technologies, however, it is facing a mature Cycle Problem (not waiting for a while). What makes me the most uncomfortable is that vNext is another tie suo serial ship, and even webapis are integrated with MVC, it also gives people a sense of complete sales promotion, which is against the original intention of the Owin system. The most fundamental problem is that there is no solution yet to provide vNext with an IIS-level server on Linux, there is no good carrier, just to define the Linux deployment of vNext as entertainment, which clearly does not have much sincerity.
To sum up,I am still inclined to use the underlying OwinSolution,The current commercial development path is:MS Owin-basedImplementation,Add various MS as neededStable components,For example, Web API 2.2 OWIN 5.2.3, Identity Owin 2.2.1, SignalR OWIN 1.2.2, OAuth 3.0.1,And all other general-purpose components,Such as EF, Logging, and IoCAnd so on;MonoAnd JexusSet up to LinuxEnvironment.
Next I will build a step to demonstrate how to assemble the MS OwinAnd the Web API 2.2,And deploy them to JexusGo up.
Development Environment VS 2013, Window 7Or 8;Deployment environment Ubuntu 15.
Step 1: Create a project
First, create a Class Library project in VS 2103. Note that if you only need a Library project, you can select Framework 4.5.2 or 4.5.1. This project is named OwinExample.
Then, we add the necessary components for this project, according to the above description, we need two components: the Core Implementation of the MS Owin Microsoft Owin and ASP. NET WebApi 2.2 Owin
We should first join Microsoft Owin
Add ASP. NET WebApi 2.2 Owin
Step 2: Create the Owin entry code
First, the traditional entry class of Owin was launched:Startup. cs
Using Owin; using System. web. http; public class Startup {public void Configuration (IAppBuilder app) {# region WebApi var httpConfig = new HttpConfiguration (); httpConfig. routes. mapHttpRoute (name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new {id = RouteParameter. optional}); // sets the current WebApi return format to json httpConfig. formatters. remove (httpConfig. formatters. xmlFormatter); // load the WebApi middleware app. useWebApi (httpConfig); # endregion }}
Key points:
L The Configuration in Startup is written as a class member instead of a static one to work with the Jexus adapter. In fact, there is little difference.
L The WebApi configuration method is similar to MVC.
L The names of Startup and Configuration are not fixed. They are just reserved.
Step 3: Create WebApi code
CreateDefaultController. csIs a default WebApi that contains the simplest Hello function.
using System.Web.Http; [AllowAnonymous] public class DefaultController : ApiController { [HttpGet] public string Hello() { return "Hello Owin!"; } }
Since then, the simple MS Owin + WebApi program has been set up. Under the Owin system, we have found everything very simple and clear.
Step 4: Create the Jexus adapter code
To deploy the project to Jexus, we also need a very simple adapter class. After this class is added to the project, it can be seamlessly deployed to the Jexus server, we name this codeAdapter. cs:
/*************************************** **************************************** * ****** Load Microsoft. owin. dll owin compilation adapter (plug-in) example * ===================================================== ========================================================== ===* purpose: * demonstrate how to add your processing method (middleware) to Microsoft. owin. ** usage: * The compiled dll is used together with the Owin. dll, Microsoft. owin. ***************************** **************************************** * ***************/# region <USINGs> using System; using System. collections. generic; using System. threading. tasks; using Microsoft. owin. builder; # endregionnamespace OwinExample {public class Adapter {static Func <IDictionary <string, object>, Task> _ owinApp; /// <summary> /// default constructor /// </summary> public Adapter () {// create the default AppBuilder var builder = new AppBuilder (); // create a user-defined Startup class // This class must contain the "Configuration" method var startup = new Startup (); // call the Configuration method, register your handler to start up in the processing process. configuration (builder); // generate the OWIN "portal" function _ owinApp = builder. build () ;}/// <summary> /// *** key functions required by JWS *** // <para> each request arrives, JWS package requests into dictionaries, use this function to send the environment Dictionary of the new request to the user </para> /// </summary> // <param name = "env">, for details, see OWIN Standard </param> // <returns>. A running or completed Task is returned. </returns> public Task OwinMain (IDictionary <string, object> env) {if (_ owinApp = null) return null; // send the request to Microsoft. owin processes this request // (your processing method has been added to its processing sequence in the constructor of this class) return _ owinApp (env );}}}
Thanks again to the Code provided by the Jexus author, Yu neiliyun.com, for my respect for the original author, this code has not been changed except for the namespace. In fact, it does not need to be changed. as you can see, I did not mean to write such abnormal comments.
Since then, our mini version of Application Development Based on MS Owin and WebApi has been completed and changed to the Release mode for compilation. We can get a series of DLL as shown in:
Can these DLL form a WebApi application? This is the case, and this application can be well deployed in the Linux environment.
Step 5: Install the Jexus Environment
Here we first declare that, based on personal capabilities, we can only provide a Deployment Solution for the latest Ubuntu version. If you are using another version of Linux, you can only find your own path.
First, install the latest Mono version on Ubuntu. Refer to the instructions in the following hyperlink article:
Http://www.linuxdot.net/bbsfile-3090
Then we will install the latest Jexus version. (For details, refer to the hyperlink below)
Http://www.linuxdot.net/bbsfile-3500
Step 6: deploy to Jexus
General Instructions for deploying the Jexus website:
Http://www.linuxdot.net/bbsfile-3084
The following describes the specific deployment steps (I will not list the specific Linux commands ):
Its important content should include the following settings:
# For owinexample
Port = 88
Root = // var/www/owinexample
Hosts = * # or your.com, * .your.com
OwinMain = OwinExample. dll, OwinExample. Adapter
OwinMainThis required configuration,And corresponding to the correct DLLFile Name and ApdaterClass.According to the preceding description,We can know that the configuration of our application should be OwinExample. dll, OwinExample. Adapter.
In addition, Web. ConfigFiles and any other files are not required in this architecture.
Note that linuxserverip is the IP address of the Deployment Server, and 88 is the port set in Jexus configuration. api/default/hello corresponds to the path ing of our WebApi, Controller class name and method name.
Conclusion
Finally, let's talk about the advantages, disadvantages and significance of this model:
Advantage: Based on the bottom layer of Owin, it is simple, clear, and stable. It can be integrated with any Owin-based technology and has high scalability. It can be perfectly integrated with Mono and Jexus, with the highest performance.
Disadvantage: It is equivalent to building a self-built architecture with a heavy workload. Because there is no independent MVC component, there is no support for MVC development. (Some of Nancy's MVC frameworks, such as the Razor engine, can be moved in independently, but this solution is to be verified ).
The final significance of this solution is,Combined with current. NETAnd LinuxThe most stable and representative MONO, MS OwinAnd Jexus,In ASP. NET vNextFinally, it can be perfectly deployed to LinuxBefore,This is one of the solutions closest to the commercial production environment.
Finally, you can consider using the development environment of this solution.TinyFoxOr MS Owin Self Host can be used as the Host. The connection can be seamless and the Code does not need to be modified.