Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use ASP.NET Core 3.0 types from a library project for shared Controllers, Middleware etc?

While ASP.NET Core up to 2.2 could be consumed through NuGet to create library projects for shared Controllers, Middleware etc, how do I create a library that is able to use ASP.NET Core 3.0 types?

While for projects containing views there is a "Razor Class Library" (razorclasslib) template, how do I create a library that only contains logic components?

like image 874
Martin Ullrich Avatar asked Sep 02 '19 16:09

Martin Ullrich


People also ask

When should you use the .NET Core Class Library project type?

Use a . NET Core library when you want to increase the . NET API surface area your library can access, and you are okay with allowing only . NET Core applications to be compatible with your library.

What is the use of middleware in ASP.NET Core?

Middleware is software that's assembled into an app pipeline to handle requests and responses. Each component: Chooses whether to pass the request to the next component in the pipeline. Can perform work before and after the next component in the pipeline.

How many types of middleware are there in .NET Core?

As you can see in the above image, the Configure() method sets up the request processing pipeline with just three middleware components are as follows. Before understanding the above three built-in Middleware components.


1 Answers

Applications built for .NET Core 3.0 can reference one or more shared frameworks. ASP.NET Core is one of these shared frameworks (others would be the base .NET Core Shared framework and the Windows Desktop Shared Framework containing WinForms and WPF).

To reference ASP.NET Core from a classic .NET Core library targeting .NET Core 3.0 (netcoreapp3.0, not .NET Standard), you can use a FrameworkReference in the csproj to reference the framework:

<Project Sdk="Microsoft.NET.Sdk">    <PropertyGroup>     <TargetFramework>netcoreapp3.0</TargetFramework>   </PropertyGroup>    <ItemGroup>     <FrameworkReference Include="Microsoft.AspNetCore.App" />   </ItemGroup>  </Project> 

When opened in Visual Studio, this additional framework reference will show up in the dependencies node in solution explorer:

enter image description here

like image 188
Martin Ullrich Avatar answered Sep 25 '22 22:09

Martin Ullrich