Original: ASP. NET Linux Deployment (2)-MS Owin + WebApi + Mono + Jexus
ASP. NET Linux Deployment (2)-MS Owin + WebApi + Mono + Jexus
This article takes my last blog post: the ASP. NET 5 Linux deployment, the article is mainly for the latest ASP. NET 5, but in the subsequent study, I am still not very satisfied with this type of entertainment deployment, of course, because the main reason is that ASP. NET 5 is still in the RC version, is not very mature. But predictably, even if this month's ASP. NET 5 RTM version as scheduled, its development and deployment of Linux on the outlook is still not very clear: particularly confusing, Ms on Linux has so far only introduced a few development-based simple server implementation, It is difficult to find a full deployment environment like IIS in its plan, so is the so-called ASP. NET 5 cross-platform development to stay at the lab level only? Now and for a long time to come (until the net 5 is fully rooted on Linux), do we have a better choice? Below I will give my own ideas.
The first thing to declare here is that the ASP. NET Linux deployment family is for the Linux deployment environment only and does not involve the Windows Deployment environment. Here are some ideas to make it easier for you to understand what's going on.
asp |
Asp. NET is part of the. NET Framework, a Microsoft technology, a server-side scripting technology that enables scripts in embedded Web pages to be executed by Internet servers, and the latest version of this month is version 5, which is vnext. |
Linux |
Linux is a free-to-use and free-to-propagate Unix-like operating system, a POSIX and Unix-based multiuser, multitasking, multi-threaded and multi-CPU operating system. The Linux in this article mainly takes Ubuntu as an example. |
Mono |
Mono is defined by Novell (launched by Xamarin and led by Miguel de Lcaza, a dedicated pioneer. NET open source project used on Linux. For now,. NET applications on Linux must also be run on a mono basis. |
Jexus |
Jexus, Jexus Web server, or JWS, is an ASP. NET Web server on the Linux platform and is currently the only one that can support enterprise-class ASP (other server scenarios without similar targeting). |
OWIN |
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. Based on this standard, developers are encouraged to develop simple, flexible modules to advance the development of the. NET Web Development Open source ecosystem. |
MS Owin |
Microsoft developed the bottom-level implementation based on the Owin specification, the latest version is 3.0.1, whose main project name is Kanata |
ASP. WebApi |
ASP. NET MVC 4 contains the ASP. NET Web API, a new framework for creating HTTP services that can connect multiple clients, including browsers, mobile devices and so on, and the ASP is the ideal platform for building RESTful services |
Restful |
A software architecture style, a design style rather than a standard, provides only a set of design principles and constraints. It is mainly used for client and server interaction classes of software. The design based on this style of software can be more concise, more layered, more easy to implement caching mechanisms. |
Nancyfx |
Nancy is a. NET and Mono platform for building lightweight HTTP-based WEB services. Based on the. NET and Mono platforms, the goal of the framework is to keep as many ways as possible and provide a super-duper-happy-path of all interactions. Official website http://nancyfx.org/ |
Three different options
In terms of web development of the. NET Roadmap, the fact that the future must be based on Owin development is unshakable in any way; On this basis, I think there are 3 routes to develop and deploy. NET Web applications on Linux:
- Bottom Owin Route: Choose the low-level owin implementation of MS, with other independent Owin-based components, to form a lightweight, self-contained architecture with the bottom owin as the core, which is now perfectly deployable to Jexus.
- three-way frame route: Choose the same based on Owin standard, non-MS three-party framework implementation, the most potential, the most famous is NANCYFX, Nancy Framework is now also better deployed to jexus up.
- Orthodox Vnext route: Select MS-Orthodox next-generation ASP. VNext, which is based on the Owin implementation of the version (note that any old ASP. NET version is not based on Owin) and is a pro-son; But there is no official version yet, its future is unpredictable, and most crucially, there is still no perfect business environment-based deployment server environment support on Linux, including Jexus.
In terms of these 3 scenarios, I think there are pros and cons: the underlying solution needs more self-selection and assembly, but with any owin based components; Three-party solution facing the problem of ecological environment, because most of the high-end components are from MS, whether the real seamless connection needs to be tested, its own viability is also a problem; The Orthodox program content is complete, supports the strong, with the MS each technology fusion degree is high, but faces the maturity cycle question (waits for time), in addition my most uncomfortable point is, vnext again made the iron chain chain boat, even webapi all and the MVC fusion, but also gives the person a whole set of marketing feeling, Contrary to the original intention of the Owin system, and the most fundamental problem is that there is no plan for Vnext to provide a Linux IIS-level server, there is no good carrier, just the Vnext Linux deployment is defined as entertainment this obviously does not see much sincerity.
In conclusion, I'm still inclined to use the bottom Owin . programme, the current commercial development routes are: based on Ms Owin implementation, Add as many MS as you need Stabilizing components, such as Web API 2.2 OWIN 5.2.3, Identity OWIN 2.2.1, SignalR OWIN 1.2.2, OAuth 3.0.1, and all the other general-purpose components, such as EF, Logging, IoC and so on; finally through Mono and Jexus erecting to Linux environment.
below I build a step to demonstrate how to assemble Ms Owin and Web API 2.2, and deploy them to Jexus . up.
development Environment vs. Windows 7 or 8; deployment environment Ubuntu.
Step One: Build a project
First, create a class library project in VS 2103, and note that the framework 4.5.2 or 4.5.1 can be selected as long as the library project is in place. This project assumes that the name is Owinexample.
Then we add the necessary components to this project, according to the above description, we need 2 components: the core implementation of MS Owin Microsoft Owin and ASP. WEBAPI 2.2 Owin
Let's join Microsoft Owin first
Then add the ASP. WEBAPI 2.2 Owin
Step two: Establish the Owin entry code
First, Owin's traditional entry class appeared: 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}
);
// Forcibly set the current WebApi return format to json
httpConfig.Formatters.Remove (httpConfig.Formatters.XmlFormatter);
// Load WebApi middleware
app.UseWebApi (httpConfig);
#endregion
}
}
Several points:
The configuration in startup is written as a class member, rather than a static way, it is to match with the Jexus adapter, in fact, the difference is not small.
The configuration of L Webapi is basically similar to MVC.
L The naming of the startup and configuration is not fixed, just a predetermined custom.
Step three: Build Webapi code
Establish DefaultController.cs as a default Webapi, which 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 installed. Under the Owin system, we found that everything became very simple and clear.
Fourth step: Establish the Jexus adapter code
In order to deploy the project to Jexus, we also need a very simple adapter class, after adding this class in the project, can be deployed seamlessly to the Jexus server, we have this code named Adapter.cs:
/ ************************************************* *************************************
* Load Microsoft.Owin.dll for owin compilation adapter (plugin) example
* ====================================================== ===================================
* Purpose:
* Demonstrate how to add your own processing method (middleware) to the processing link of Microsoft.Owin.dll
*
* Instructions:
* Put the compiled DLL together with Owin.dll, Microsoft.Owin.dll and other files into the bin folder of the website
************************************************** *********************************** /
#region <USINGs>
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Owin.Builder;
#endregion
namespace 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 have a "Configuration" method
var startup = new Startup ();
// Call the Configuration method and register your own processing function in the processing flow
startup.Configuration (builder);
// Generate OWIN "entry" function
_owinApp = builder.Build ();
}
/// <summary>
/// *** Key functions required by JWS ***
/// <para> Every request comes, JWS will package the request into a dictionary and pass it to the user via this function </ para>
/// </ summary>
/// <param name = "env"> Newly requested environment dictionary, see OWIN standard for specific content </ param>
/// <returns> return a running or completed task </ returns>
public Task OwinMain (IDictionary <string, object> env)
{
if (_owinApp == null) return null;
// Give the request to Microsoft.Owin to process this request
// (Your processing method has been added to its processing sequence in the constructor of this class)
return _owinApp (env);
}
}
}
Here again thanks to the Jexus author Woo Cloud provided code, out of respect for the original author of this code in addition to the namespace I have not changed a letter, in fact, do not need to change. In fact, we can see that such a sick note should not be my intention to write.
Since our development of the mini-version application based on Ms Owin and WEBAPI, we have changed to release mode compilation, we can get a series of DLLs as shown:
Can these DLLs form a WEBAPI application? This is true, and the application is well-deployed to the Linux environment.
Fifth Step: Install Jexus Environment
Here, first of all, based on the limitations of personal ability, can only give the latest version of Ubuntu a deployment plan, the use of other versions of Linux brothers can only trouble you to find their own.
First, we re-install the latest version of Mono on Ubuntu. You can refer to the following hyperlink article guidelines:
http://www.linuxdot.net/bbsfile-3090
Then we install the latest version of Jexus. (Please also refer to the following super-chain)
http://www.linuxdot.net/bbsfile-3500
Sixth step: Deploy to Jexus
For general guidance on deploying the Jexus website, you can get here:
http://www.linuxdot.net/bbsfile-3084
Here's our special deployment step (I'm not going to list the specific Linux commands):
- Set up the Site Directory/var/www/owinexample, and then create a bin directory under this directory.
- In a variety of ways, the above-developed DLL copies the site directory of the bin directory.
- Create a configuration file for the Jexus website, assuming we are named Owinexample
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
Special emphasis is placed on the Owinmain This required configuration, and need to correspond to the correct DLL file name and Apdater class. According to the previous description, We can know that the configuration of our application should be OwinExample.dll, Owinexample.adapter.
Additionally, the Web. config files and any other files are not required in this framework.
- Restart Jexus Service
- Open your browser and enter Http://linuxserverip:88/api/default/hello to see the results.
Note that Linuxserverip is the IP for the deployment server, 88 is the port that we set in the Jexus configuration, Api/default/hello corresponds to our WEBAPI path mapping, the controller class name and the method name.
Conclusion
Finally, the advantages, disadvantages and meanings of our model are as follows:
Advantages: Based on Owin bottom, simple and clear stability, can be fused with any Owin-based related technology, strong extensibility, and Mono, Jexus perfect combination, the highest performance.
Disadvantage: the equivalent of a self-organizing structure, build a large workload, because there is no independent MVC components, the lack of support in MVC development (part of Nancy's MVC architecture such as the razor engine can be moved independently, but this scheme is to be verified).
The ultimate implication of this scheme is that combined with the current. NET and Linux In the direction of the most stable and representative mono, MS Owin and Jexus, in ASP. VNext Ultimately, it can be optimally deployed to Linux before the This is one of the most closely-suited scenarios for commercial production environments.
Finally, the development environment for this scenario can be considered as hosting with Tinyfox or Ms Owin self host. Can be seamlessly connected and the code does not need to be modified.
ASP. NET Linux Deployment (2)-MS Owin + WebApi + Mono + Jexus