Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Hosting Nancy on ASP .NET Core 3.1 (using Kestrel)

Sean edited this page Mar 7, 2020 · 2 revisions

You can configure a .NET Core ASP application to use Nancy with some basic setup.

To begin, create a new ASP Core Project (via the dotnet tool):

dotnet new web

Installing Packages

Next, you will need to install the Nancy package, as well as the Microsoft.AspNetCore.Owin and Microsoft.AspNetCore.Server.Kestrel packages. You can do this by either adding the packages using Nuget commands, using Visual Studio's 'Manage Nuget Packages' function, or adding the following to your csproj:

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Owin" Version="3.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
    <PackageReference Include="Nancy" Version="2.0.0" />
  </ItemGroup>

Adjusting C# Files to Use Nancy

We'll need to modify both the provided Program.cs and Startup.cs before we can add Nancy modules to our project. In Program.cs, modify your main method to use Kestrel, and to use the provided Startup file:

        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseContentRoot(Directory.GetCurrentDirectory())
                        .UseKestrel(o => o.AllowSynchronousIO = true)
                        .UseStartup<Startup>();
                });
    }

Finally, adjust your Startup.cs to use OWIN, and to connect OWIN to Nancy:

    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseOwin(x => x.UseNancy());
        }
    }

At this point, you should be able to add Nancy module classes and they will be picked up automatically. You can start the application by calling dotnet run in your project directory.

Hosting on IIS

ASP.NET Core applications can be hosted by IIS. Do do this, first follow the instructions in IIS' ASP Core hosting to prepare your IIS instance to host your applications.

Special Notes for IIS and .NET Core 3.0+

IIS offers two hosting models: in-process and out-of-process. As of .NET Core 3.0, in-process is the default. However, you cannot use Kestrel for in-process IIS deployments in .NET Core 3.0+. You can either modify your Program.cs as described in the link to run on IIS only (replace UseKestrel() with UseIISIntegration()), or switch to the out-of-process hosting model.

To adjust the hosting model, add the following to the main PropertyGroup in your csproj file:

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
    ...
  </PropertyGroup>
Clone this wiki locally