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"); # endregion3. 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