Search code examples
asp.net-core.net-8.0

How to expose REST API (minimal API) in a console application?


I have a console application which must expose a HTTP API.

How do I add the ASP.NET Core's Minimal API to a console application?

Ideally without unnecessary dependencies to stuff like controllers, razor views, etc.


It seems that WebApplication.CreateBuilder(args) is supposed to be called from web application:

csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">

but I want the console application to use Microsoft.NET.Sdk because

  1. I want to deploy as a windows app, not web app and changing SDK changes also publish options
  2. I want the console app to be lightweight. Web SDK brings in too many dependencies (>100MB output)

Solution

  • I want to deploy as a windows app,

    You can add the framework reference to the shared framework as the docs suggest:

    <ItemGroup>
        <FrameworkReference Include="Microsoft.AspNetCore.App" />
    </ItemGroup>
    

    Which will allow you to use/run ASP.NET Core functionality in any kind of application (note that ASP.NET Core is already a console app by itself so possibly you can just use the appropriate template). Though it should not handle your second requirement:

    I want the console app to be lightweight. Web SDK brings in too many dependencies (>100MB output)

    Consider looking into trimming options which can allow reducing the size of the application (and with Native AOT support for ASP.NET Core 8 it should become better and better).

    See also: