-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Hosting Nancy on ASP .NET Core 3.1 (using Kestrel)
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
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>
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.
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.
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>
- Introduction
- Exploring the Nancy module
- Routing
- Taking a look at the DynamicDictionary
- Async
- View Engines
- Using Models
- Managing static content
- Authentication
- Lifecycle of a Nancy Application
- Bootstrapper
- Adding a custom FavIcon
- Diagnostics
- Generating a custom error page
- Localization
- SSL Behind Proxy
- Testing your application
- The cryptography helpers
- Validation
- Hosting Nancy with ASP.NET
- Hosting Nancy with WCF
- Hosting Nancy with Azure
- Hosting Nancy with Suave.IO
- Hosting Nancy with OWIN
- Hosting Nancy with Umbraco
- Hosting Nancy with Nginx on Ubuntu
- Hosting Nancy with FastCgi
- Self Hosting Nancy
- Implementing a Host
- Accessing the client certificate when using SSL
- Running Nancy on your Raspberry Pi
- Running Nancy with ASP.NET Core 3.1