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 handle 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

Ninject and WCF

Ninject is very simple and in the same time powerful IoC container. I used it in few project and had very little or none problems.

Most of the time getting instances from Ninject kernel requires simple line with binding expression, or even this is sometime unnecessary if type is self bind able (i.e. if it is concrete class with parameterless constructor).

Little harder is getting Ninject to work with WCF. You cannot just bind interfaces types because proxies which implements them are created through .NET mechanism. Luckily WCF system is very flexible and mostly can be changed/extended with custom functionality. Continue reading “Ninject and WCF”

Server controls in separete assembly part 2

Yesterday I was writing about creating server controls in separate assembly. Today I will cover more complicated example then simple “Hello World!” control. My goal was to create text box that will take handler method to run when validation event will be triggered, and when validation fail will show appropriate message. Text box with builtin validation would be nice, since you always should validate user input, right? But lets start creating our control. Continue reading “Server controls in separete assembly part 2”

Server controls in separate assembly

In my project I wanted to create text box control with builtin configurable validation. ASP.NET definition in .aspx or .ascx file would get validation function by name. But I do not wanted to create just .ascx file with text box. I could not then reuse that control in other projects and it certainly would be nice, right? Creating such server control from code is not hard, but maintainability of HTML code in C# (it cannot be done other way) is not good. It’s pain really. Why Microsoft would create way to automatically create C# code from .ascx, .aspx files is mystery for me. Continue reading “Server controls in separate assembly”