Middleware in OWIN

There are 2 ways of writing OWIN middleware in ASP.NET Core.

  • Pure OWIN – usefull if you want to integrate with OWIN compatible module but not written in C#
  • ASP.NET Core OWIN middleware

ASP.NET Core Middleware:

This type of middleware can be:

  • delegate of Func<HttpContext, Func<Task>, Task>
  • class with IMiddleware imlementation
app.Use(async (context, next) =>
{
    // Do work that can write to the Response.
    await next.Invoke();
    // Do logging or other work that doesn't write to the Response.
});

Pure OWIN Middleware:

This rather would be used to integrate with OWIN middlewares written in different language, so that ASP.NET Core implementation directly is not possible.

  • delegate of type Func<IDictionary<string, object>, Task>
  • must set keys of owin.ResposeBody and owin.ResponseHeaders

usage in ASP.NET Core pipeline:

public void Configure(IApplicationBuilder app)
{
    app.UseOwin(pipeline =>
    {
        pipeline(next => DelegateTask);
    });
}

Further read:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-6.0

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/owin?view=aspnetcore-6.0

Owin model and Kestrell request handling

  • OWIN – Open Web Interface for .NET
  • allows web apps to be decoupled from web servers
  • defines standard way for middleware to be used in a pipeline to handler requests and provide responses
  • ASP.NET Core applications can interoperate with OWIN-based applications, servers and middleware

Microsoft.AspNetCore.Owin package provides two adapter implementations:

  • ASP.NET Core to OWIN
  • OWIN to ASP.NET Core

This allows ASP.NET Core to be hosted on top of an OWIN compatible server/host or for other OWIN compatible components to be run on top of ASP.NET Core.

Further read:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/owin?view=aspnetcore-6.0

Expression Trees

  • ‘tree like’ data structure
  • each node is expression (method call, equality comparison, binary operation etc.) representing a piece of code
  • you can compile expression tree and run code it represent
  • enables dynamic code generation at runtime
  • LINQ queries on DBs are implemented with Expression Trees
  • used in the dynamic language runtime (DLR)
  • anonymous lambda expression can be converted to ExpressionTree via compiler (i.e. x => x*2 can be represented as tree instead of delegate or anonymous lambda function)

Further read:

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/expression-trees/