[Nancy On. Net Core Docker] lightweight web framework, nancydocker

Source: Internet
Author: User

[Nancy On. Net Core Docker] lightweight web framework, nancydocker

. Net core has been greatly developed. Although I have been engaged in python development, I have been paying attention to it. net development, when visiting the blog Park, I found that everyone will mention the Nancy framework. After simple use, I found it was so simple and elegant.

public class SampleModule : Nancy.NancyModule{    public SampleModule()    {        Get["/"] = _ => "Hello World!";    }}

The Code has been uploaded to git, including the source code and docker packaging script. If you are interested, you can study it on your own.

Https://github.com/BruceDone/webapi

 

This is similar to flask in python and is easy to use. Today, we will create a simple and easy-to-use. net core version of the nancy application, and then package the program into docker concurrent deployment and use.

  • Development Environment: Marc Pro
  • IDE: vscode
  • . Net Core
  • Docker

Make sure that the above environments have been installed before development.

dotnet new

2. After the execution, make necessary files first, open package. json, and copy the following content.

{  "version": "1.0.0-*",  "buildOptions": {    "debugType": "portable",    "emitEntryPoint": true  },  "dependencies": {    "Microsoft.NETCore.App": {      "version": "1.0.0",      "type": "platform"    },    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",    "Microsoft.AspNetCore.Owin": "1.0.0",    "Nancy": "2.0.0-barneyrubble"  },  "commands": {    "web": "Microsoft.AspNet.Server.Kestrel"  },  "frameworks": {    "netcoreapp1.0": {}  }}

3. OK. When the file is saved, vscode will automatically import the relevant dll file. After the file is imported, create the StartUp. cs file first.

using Microsoft.AspNetCore.Builder;using Nancy.Owin;namespace NancyApplication{    public class Startup    {        public void Configure(IApplicationBuilder app)        {            app.UseOwin(x => x.UseNancy());        }    }}

4. save and create the HomeModel. cs file.

using Nancy;namespace NancyApplication{    public class HomeModule : NancyModule    {        public HomeModule()        {            Get("/", args => "Hello World, it's Nancy on .NET Core");        }    }    public class PageModule : NancyModule    {        public PageModule()        {            Get("/person/{name}", args => new Person() { Name = args.name });        }    }    public class Person    {        public string Name { get; set; }    }}

 

The routes and returned values have been written. I will not explain the relevant code here. If you are interested, you can go into details on your own.

 

4. Edit the Program. cs File

using System.IO;using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting; namespace NancyApplication{    public class Program    {        public static void Main(string[] args)        {            var host = new WebHostBuilder()                .UseContentRoot(Directory.GetCurrentDirectory())                .UseKestrel()                .UseUrls("http://*:5000")                .UseStartup<Startup>()                .Build();             host.Run();        }    }}

5. OK. All the necessary files are ready. run dotnet run and access http: // 127.0.0.1: 5000 to see the result,

6. Use Docker to deploy programs

 

 

Access our api address:

± |master ?:1 ✗| → curl http://127.0.0.1:5000/person/bruce{"name":"bruce"}

 

 

I have packed all the programs and codes on git. There are related instructions in it, and the program is easily packaged into docker, so that you can easily cluster or deploy them.

 

Git address: https://github.com/BruceDone/webapi

 

If you think it is helpful to you, please do not mean your star, thank you :)

Related Article

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.