WebApi系列~自主宿主HttpSelfHost的实现

回到目录

宿主一词我们不会陌生,它可以看作是一个基础设施,它为一些服务和功能提供最底层的支持,如你的web应用程序可以运行在iis或者apache上,而这两个东西就是web应用程序的宿主,而今天说的自主宿主SelfHost就是说,它可以自己去监听自己的服务,如你可以把一个web应用程序宿主到一个console控制台程序上,或者把一个webApi宿主到一个console或者windowService上,这都是可以的。

一 需要添加一些程序集引用

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAALsAAAAUCAIAAABauNsGAAABIUlEQVRoge2ZSxKDIBBEPUkOk/ufh3V21hT0tLSgJqTfIoWRzwDPwUq2YozC9nQA5sewMUbDxhgNG2M0bIzRsDFGw8YYDc2YdwOs9grMCFJgH3QwgNj2kYl8LdiYzIkeY+Liqgs9ZWPiTo+MBftpW/2bTNcaozLLmD3NjIxlYyBdxlSFzlOpuoyHRUEnFzxHYIVDFVpjSEi8Hx5hVmFhjebnmB34aPIjo9UlFtpPMm61kSSkw/ilHMMnuAAXGlOSLY93s+WOFaqccc4YmB6k4LNWNoYx8uab5WqyH/Cy0xhYIJ339ANb2ZhRsrOc54/q+7ZaSYzJui3IVzIWv8XVh/kMLM0S3PcL3sKLGFl+mncYs/YzF1k+wRT/S2BUbIzR+AD4TLgSQ7fGkQAAAABJRU5ErkJggg==" alt="" />

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAN4AAAA/CAIAAAAAHeHHAAACS0lEQVR4nO3aW26EMAyF4VlJFtP9r4fnPlRCGWI7CZhwKv7voZpLJjH0KHPBn22VUsqytZ71niO91WfBGqWUl/y33nOkC6yIJnAC0YQooglRRBOiiCZEEU2IIpoQRTQhai6aPw1zWKlkFDnhb8X6rzmGH8b12dH0wjcSzfq/PpuA64kZiWbKQrjbvdGcRTSxG4rm4cbgG/rh7h6X+kY90vwMYA6Ig9WdxxsDKfm75s58Z29veC9pB0/tiIOrk05ZN0Zzc7JVP+tlsR5Qb7TeVO0k5h5JNP+R/G/o3tcg7w3UG+/dvbJrto8QTVn5v2t6nwjjHfHweDtsc6Lpxc5bzrwLQet+cicHmEKXO0RxoRKiiCZEEU2IIpoQRTQhimhCFNGEKKIJUXS5u/Mcrmqmlok+utz7xQx2iiAXXe79YojmI+hy788TVOgNIK/X0eXe31mnds2RmTGCLneiKYoud6Ipii73qLygpHgwubyOLvdkLznMBehyz8SWmYgLlRBFNCGKaEIU0YQooglRRBOiiCZEEU2Iosvdnce7UJlSZHx+gmfLt5sqVECXe7+Y+IiuHGP82u5aZoWzNciiy71fzH3RnB1JNOlyt5drX+UNGJm5+6B5CN3lvArj8yyILvfH+jXjCs+tde48a6LL/bFoHgab+9/sWl7N8XnWRJf76mh2N8srawUTxkchiC73qLygpHhwd2Zvkr34dtjhEa9C7wTG51kQXe7JXnKYC9Dlnsncn3AOFyohimhCFNGEKKIJUUQToogmRBFNiPoFJxWweEHLi+MAAAAASUVORK5CYII=" alt="" />

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQcAAAAbCAIAAAD+qt2kAAABL0lEQVR4nO3aQRLCIBBEUU/CYbz/eVi7MoU9QIYUCAn/rWIkMAs6UOIrbiyEMLsErOg1u4BpwtfsQrCcfVMBlJAKQJEKQJEKQJEKQJEKQJEKQJEKQDWk4m3YNse52GpnZK31hF9Da8NqMqkozXtPKmLyN4rWyTRu8tmenWORhz0NScWxXDSVQiqwiPNUyMW1VKT7kPTb9Kbdq8hTtk3lZqWNf1/UNBY7rscYtYOyMYi5PMgMs/04LypP1ZvVOTvxFIYb+VMqPG/rC6mQgSptskWesiXVxyIVz9D5N6jY8kbvkgpPm16p8BdGKm6t83lFughkN+XHx1iYrKe79vS+XYVKnWSXptIkrixKrRXijjjFy8imCPsgFYAiFYAiFYAiFYAiFYAiFYAiFYAiFYD6ALcVkOau9xz3AAAAAElFTkSuQmCC" alt="" />

二 代码实现

  

   #region Web Api监听
Assembly.Load("Lind.DDD.TestApi");  //手工加载某个api程序集的controller
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

三   web api代码

 /// <summary>
/// 测试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();
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);
}
}

四   测试

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAU8AAADZCAIAAAAv5yVPAAAWpklEQVR4nO2d+XMbx5XH8fdsZXertrzZzSbeZDcbVyprJ+uNE5fjYx3Zsh35WlmOY8mWyFgp2XLiZG3HKtsKTdEUKUoQxUsSSUvUYYkyLxHESQIECJIAQRIXDwA8dJj7A2IYmpl+09MY4mB/P/VKBfR0v379ur/TA2A4ssxMT/BYeCoQngqEJv2hycB00D89mWdBf2gyEJr0Z+twOoTBYEU2S3g6QFtoyh+a8k9P+gMB7+D1oZ7Ll093nbO2n7G2nbG2nbG2nzndda7n8uWh4euBgHd60p+tr+sWBoMV2SzhKT9hocnx6eC4x+PquXTZ2nL6+oh9PhpNpVJra2vr6+vr6+tra2upVGpuPjpsc1hbTvdcujzqcU0Hx0OT47RnGAxWZLNkt2K1TU+OTwV9gcDYhc+vnOroDIXDWYWvra2trq6urKxkMplMJrOysrK6upo7FAqHT3V0Xvj8ykTAOxX0TU+Os/zDYLAimyU0Oa626aBvasLncTtbT3e6R8dW11bW1lYzmbSeZbKnAtfoWEfnZ74xd9A/Nh30aXYBg8GKbJbpSZ/CpoLeyQmvy21vOdsVi8XW19czmUyam0wms76+HovF2jq7X92zd9++qqqqqmoAQKmxTAd9CpsMjHk8Tmvb6UQyubq6mk6nBGx1dTWxkLzUZ0tnVm7fvr0BACg1lqkJb75NBsbGfe7m9jOzc3Orqyup1LKwra6uLCylewcd6zdvfvnll6UeKQCyY1FIPegfO3/pksPtWVlZWS6YGzduBqdnvRPTt29/CcEDUFoskxNjOQsGRl3ukeb2M+lMOpVaXl5eou3ThmONx61EhVRqeX19/eqgI7OyCrUDUFosk4HRrAX9nolxz7menonJqXQ6tbS0SNsbB97++3/63t9983sffPgXolpmJRNNLHp8Qdb2vrKy4na7L1y40NHR0dHRceHCBbfbvbKyUvxcALC1sQT9o1mbGPd4x5yN1pZUKrW0tLRIsv/A2//8b/95789/+aMHHrvrOz9YWFhg1VxaWlpdXeu5OrR244Za7eFw+PTp09euXRseHvZ4PMPDw4FAoK2traOjIxQKlSQjAGxVLEG/J2sBn6uvv7dv8Ho6nVpcXCBs/5sHv/Xv997/yFMPbf/f+x564kh9I10/k0mPBabDc1HFl/OhUKizszMajaZSKZvN1tHR0d7e3tvb29LS0tHR0draCsETVFdXl5UfUP5YJsbdWfN7Xd3nzwenppaWFxcWkyx74823/+X7993/6DO/eOql+x95+khD0+LSAlF/YTG5tLw0F0u4vcHbt7/e2zOZTFtbWyKRuHXr1s2bN5eXl69cudL8FZ2dnXa7vbW1NZPJKCP+ikJHrvJjilsWud88CyzJlQsEwFkItip/VXvA5xofc55qPx2NRRcXFxYWkpr2xoHff/s/fvzTx3Y8tuOVX2zf2XC8maics8XFhYWFpb5h961bt3MX83a7/eLFizdu3Lhx40YikWhubrbeicPhaG5uttvt2nGbJEuFH2G3dEOFgIVLWG+FMcUtThmVQp7avc5jJ08tLi0tLCaTCwm1vXHg99+55yc/e/zZx597ddsLex55etcPf/roN7/7w7/9x3/9xl13f+Ouu//mH7793w8+qm64sJhcTqUufjF86/bXaj979uz169dDoZDdbm9razuuoru7O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VwUuhC4b/VrORqV9/1YbSCuZGY6B+CBzwUtEoM3VVa2t9UWbd/0SVinksCBA90EV8ihu4qrbZU97zeA7VvKhA8oBFcH0ZvINdUe263Vz/bhDikaMt5VzatbXVNoi+ile641EEqesFjXsDmIbI4dJcUS+0b7B01X5+6hzSr0YeE93aiL6IVPS66L82xEGNUAMEDFubv7UafnVDN/iNK4hDhk+cEwVnCKuSpwBM83URA7ZA6IDD/czvr+zn161yJgNqN7ufCat8gT1V0KzzmBZQVJn8nL/DsBAG16zokNnxDJbpdE602b2/XBFIHupj8e3s5qH0jT/CsVvy7tMBZQ3dcRF94zAvYPExYJfSTUhQLVLFkWSuYOMSqQGtSMxie7nQj2VCdXOgmrKNEotQjyn8LqQNOzFwo5bPseK7JKwvO0xkABOWiT1Ogd+BKR/cSAwCaLaV2AAAB1A6ALEDtAMgC1A6ALEDtAMgC1A6ALEDtAMgC1A6ALEDtAMgC1A6ALEDtAMgC1A6ALEDtAMgC1A6ALEDtAMgC1A6ALEDtAMgC1A6ALEDtAMgC1A6ALEDtAMgC1A6ALPw/ZT9vEQUHifgAAAAASUVORK5CYII=" alt="" />

回到目录

上一篇:Linux经常使用的命令(必看)


下一篇:QT 常用设置