-
Notifications
You must be signed in to change notification settings - Fork 0
OWIN
OWIN (Open Web Interface for .NET): from http://owin.org
defines a standard interface between .NET web servers and web applications. The goal of the OWIN interface is to decouple server and application, encourage the development of simple modules for .NET web development, and, by being an open standard, stimulate the open source ecosystem of .NET web development tools.
- Use OWIN to Self-Host ASP.NET Web API
- Anders Abel, What's This OWIN Stuff About?
- Satheesh Babu, Understanding and Creating OWIN Middlewares - Part 1
Steps to create a self-host OWIN application:
- create a console application in VS2017
- install Nuget package Microsoft.AspNet.WebApi.OwinSelfHost
- create a Startup class
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);
}
}- modify Program.cs
static void Main() {
string baseAddress = "http://localhost:9000/";
// Start OWIN host
using (WebApp.Start<Startup>(url: baseAddress)) {
Console.ReadLine();
}
}- create an api controller
public class ValuesController : ApiController {
// GET api/values
public IEnumerable<string> Get() {
return new string[] { "value1", "value2" };
}
}- then we can access this web api in browser using the url http://localhost:9000/api/values
WebApp.Start runs a web server. This method has a variation that takes a Configuration method as parameter, one just like the Configuration method in the Startup class:
using (WebApp.Start(baseAddress, startup1)) { ... public static void Startup1(IAppBuilder appBuilder) { ... OwinSelfHost provides a OWIN-compatible web server. We can add our own OWIN-compatible application components (middlewares):
private static void Startup(IAppBuilder app) {
app.Use(new Func<AppFunc, AppFunc>(StartPageMiddleware));
}using AppFunc = Func<IDictionary<string, object>, Task>;
class StartPageHandler {
AppFunc next;
public StartPageHandler(AppFunc next) {
this.next = next;
}
public async Task InvokeAsync(IDictionary<string, object> context) {
if (context["owin.RequestPath"] as string == "/") {
using (var writer = new StreamWriter(context["owin.ResponseBody"] as Stream)) {
await writer.WriteAsync("Hello World!");
}
}
else {
await next.Invoke(context);
}
}
}This middleware is very similar to asp.net core middleware:
- Func<IDictionary<string, object>, Task> vs RequestDelegate
- IDictionary<string, object> vs HttpContext
- IAppBuilder vs IApplicationBuilder
- Use vs UseMiddleware
ASP.NET Core has similarity to OWIN. But there are still difference, and we need a bridge to use OWIN middleware in ASP.NET Core:
- install Nuget package Microsoft.AspNetCore.Owin
- use the OWION middleware in the following way:
public void Configure(IApplicationBuilder app) {
app.UseOwin(pipeline => {
pipeline(next => StartPageHandler);
});
}For details, see Open Web Interface for .NET (OWIN) with ASP.NET Core.