Use MicroService4Net to quickly create a simple microservice

Source: Internet
Author: User

Use MicroService4Net to quickly create a simple microservice

"Microservice architecture (Microservice ArchitectureThe term ")" has been widely spread over the past few years and is used to describe a special way to design applications as a set of independently deployable services. At present, this architecture method has not been accurately defined yet, but it is embedded in the organization, automatic deployment, and intelligent terminal (intelligence in the endpoints) of business capabilities) the Distributed Control of language and data has some common characteristics.

"Microservices" is just a new term in the software architecture full of streets. Although we despise such a thing very much, the software style described by this thing is getting more and more attention. Over the past few years, we have found that more and more projects are using this style, so that our colleagues naturally regard it as a default development form when building enterprise-level applications. However, unfortunately, it is hard to find such theoretical descriptions about what the microservice style is and how it should be developed.

In short, the microservice architecture is like developing a single application as a small service. Each small service runs in its own process and uses a lightweight mechanism for communication, it is usually an http api. These services are built around business capabilities and deployed independently through a fully automated deployment mechanism. These services use different programming languages, as well as different data storage technologies, and maintain a minimum of centralized management.

 

In this article, I will introduce foreigners (MicroService4Net) https://github.com/thecodecleaner/microservice4net. in the C program, this kind of library is very simple to create a microservice.

You can choose either a Windows service or a console application hosting method. Step 1: Create a console project named MicroService4Net. Example, and then install MicroService4Net through the Nuget Package Manager.

 

Step 2: add the namespace using MicroService4Net;

Add the following code to the Main method:

static void Main(string[] args){    var microService = new MicroService();    microService.Run(args);}

 

Default port: 8080. If you want to change the port

static void Main(string[] args){    var microService = new MicroService(port: 8086);    microService.Run(args);}

 

  

Part 3: Add a new ApiController, such as ExampleController

using System.Web.Http; namespace MicroService4Net.Example.Controllers{    public class ExampleController : ApiController    {        [Route("Example")]        public string GetExample()        {            return "Example";        }    }}

 

 

Complete console startup

Enter http: // localhost: 8086/Example:

So far, we have simply created a small microservice so that we can spend more time on business logic.

If you want to host the service running on Windows, you need to add two empty classes

12 public class MicroServiceInstaller : ProjectInstaller { }public class MicroServiceService : InternalService { }

After the solution is regenerated, it will be much simpler. Open CMD as an administrator, use cd to switch to the project compilation directory, and execute MicroService4Net.Example.exe-install (MicroService4Net. example is the project name, which can be replaced according to your own name). After the installation is successful, open the Service Management Console and you will find the successfully installed Service.

 

 

If you want to uninstall the service, execute MicroService4Net.Example.exe-uninstall

Success, such as the need for source code, click https://github.com/TheCodeCleaner/MicroService4Net

Reposted to: crazy ant financial

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.