Owin Self Host

时间:2021-01-28 07:03:57

http://owin.org/

Owin 定义了webserver和webapplication之间的标准接口,目标就是为了解耦webapplication对webserver的依赖,

就是说以后可以轻松的建立一个轻量级的HttpServer,

1.Basic Sample  Self Host

下面建立一个Basic Self Host Http Server Via Owin ,全部功能就是获取客户端的Http请求,然后做出回应,当发生Unhandled Exception的时候,会自动跳转到错误页,

Step1:Install-Package Microsoft.Owin.SelfHost

Step2:Configuration in Startup.css

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin; namespace OwinSelfHostBasic
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseErrorPage(); // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
app.Run(context => {
if (context.Request.Path.Value == "/fail")
{
throw new Exception("Random exception");
}
context.Response.ContentType = "text/plain";
return context.Response.WriteAsync("Hello, world.");
});
}
}
}

app.UseErrorPage();

为WebApplication指定错误页,

app.Run加入响应逻辑

Step3:指定监听端口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace OwinSelfHostBasic
{
class Program
{
static void Main(string[] args)
{
using (Microsoft.Owin.Hosting.WebApp.Start<Startup>("http://localhost:9000"))
{
Console.WriteLine("Press [enter] to quit...");
Console.ReadLine();
}
}
}
}

OK,一切准备就绪,启动这个Console程序,请求 http://localhost:9000/ 这个地址,看起来比NodeJS更加方便。

2.Self Host Web API using Owin

Step1:Install-Package Microsoft.AspNet.WebApi.OwinSelfHost

Step2:Startup.cs

using Owin;
using System.Web.Http; namespace OwinWebAPISelfHost
{
public class Startup
{
// This code configures Web API. The Startup class is specified as a type
// parameter in the WebApp.Start method.
public void Configuration(IAppBuilder appBuilder)
{
// Configure Web API for self-host.
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
); appBuilder.UseWebApi(config);
}
}
}

Step3:APIController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http; namespace OwinWebAPISelfHost
{
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
}

Step4:设置监听端口

using Microsoft.Owin.Hosting;
using System;
using System.Net.Http; namespace OwinWebAPISelfHost
{
public class Program
{
static void Main()
{
string baseAddress = "http://localhost:9000/"; // Start OWIN host
using (WebApp.Start<Startup>(url: baseAddress))
{
// Create HttpCient and make a request to api/values
HttpClient client = new HttpClient(); var response = client.GetAsync(baseAddress + "api/values").Result; Console.WriteLine(response);
Console.WriteLine(response.Content.ReadAsStringAsync().Result); Console.ReadLine();
} }
}
}

Step5:启动Console程序,这里我们看到返回的是Json格式的数据,当我们用火狐浏览器进行请求,会发现返回的是XML格式的数据,因为火狐浏览器默认的Accept为XML

Owin Self Host

Owin Self Host

Owin Self Host