WebApi series ~ Implement the self-hosted HttpSelfHost and webapihttpselfhost

Source: Internet
Author: User

WebApi series ~ Implement the self-hosted HttpSelfHost and webapihttpselfhost

The word "host" can be seen as an infrastructure that provides underlying support for some services and functions, for example, your web application can run on iis or apache, and these two items are the web application host. Today, the self-hosted SelfHost is, it can monitor its own services. For example, you can host a web application to a console program, or host a webApi to a console or windowService, this is all possible.

I need to add some assembly references

Code 2 Implementation

  

# Region Web Api listener var config = new HttpSelfHostConfiguration ("http: // localhost: 3333"); config. routes. mapHttpRoute ("default", "api/{controller}/{id}", new {id = RouteParameter. optional}); var server = new HttpSelfHostServer (config); server. openAsync (). wait (); Console. writeLine ("Server is opened"); # endregion
3. web api code
/// <Summary> /// test webapi /// </summary> public class TestController: ApiController {// GET api/<controller> public IEnumerable <string> Get () {return new string [] {"value1", "value2" };}// GET api/<controller>/5 public string Get (int id) {return "value" ;}// POST api/<controller> public void Post ([FromBody] Demo value) {Thread. sleep (10000); Logger. core. loggerFactory. instance. logger_Info (value. toString ();} // PUT api/<controller>/5 public void Put (int id, [FromBody] string value) {}// DELETE api/<controller>/5 public void Delete (int id) {}} public class Demo {public string appName {get; set ;} public string url {get; set;} public override string ToString () {return string. format ("appName: {0}, url: {1}, datetime: {2}", this. appName, this. url, DateTime. now );}}
4. Test

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.