-
Notifications
You must be signed in to change notification settings - Fork 0
OWIN
-
Anders Abel, What's This OWIN Stuff About?
-
Satheesh Babu, Understanding and Creating OWIN Middlewares - Part 1
-
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:
C# using (WebApp.Start(baseAddress, startup1)) { ...
C# 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<esc><</esc>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.
OWIN (The Open Web Interface for .NET)
Project Katana: Whereas both the OWIN specification and Owin.dll are community owned and community run open source efforts, the Katana project represents the set of OWIN components that, while still open source, are built and released by Microsoft. These components include both infrastructure components, such as hosts and servers, as well as functional components, such as authentication components and bindings to frameworks such as SignalR and ASP.NET Web API.
Steps to use Katana
- In VS2017, create a new ASP.NET Web Application (.NET Framework) project with .NET Framework 4.6.1, choose the Empty project template.
- Install Nuget package Microsoft.Owin.Host.SystemWeb
- Create a StartUp.cs file with the following content:
public class Startup {
public void Configuration(IAppBuilder app) {
app.Run(context => {
context.Response.ContentType = "text/plain";
return context.Response.WriteAsync("Hello World!");
});
}
}- run the application, we should be able to see "Hello World" in the browser.
- Howard Dierking, An Overview of Project Katana
- Praburaj Thiagarajan and Rick Anderson, OWIN Middleware in the IIS integrated pipeline