-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Nancy v2 Upgrade Notes
In Nancy 1.x you had the option to turn a route into async by setting the runAsync property to true. In 2.x all routes are now async, and as such the method signature has changed
Get["/"] = (parameters, ct) => {
return <your async operation>
}
parameters is, as before, an DynamicDictionaryinstance that contains all route parameters. The newctparameter is aCancellationToken`.
⚠️ To ease the transition,2.0-alphacontains a class calledLegacyNancyModulewhich exposes the same route declaration syntax as before. Inherit from this to help ease the transition to async routes. However, please note that this is makes as obsolete and will be removed in a later update (undecided which)
Most of the members in StaticConfiguration have been moved out and migrated over to the new Configuration API.
The following settings have been migrated
- Json
- Xml
- View
- Tracing
- Routing
In Nancy 1.x versions Context.CurrentUser was a IUserIdentity, you could add properties to your implementation and then access these properties that were not on the interface by casting eg. var customerId = ((MyUser)Context.CurrentUser).CustomerId;
In v2.x of Nancy Context.CurrentUser is a ClaimsPrincipal. By default this contains an array of claims that the user has. So to get a name value for example you would have do something like Context.CurrentUser.FindFirst(ClaimTypes.Name).Value Not very elegant and what about the User object you already had in 1.x . The best way to do this is to tweak the User object and use an extension method to cast it from ClaimsPrincipal to your User object.
public class MyPrincipal : ClaimsPrincipal
{
public MyPrincipal(IPrincipal principal) : base(principal)
{
}
public string FullName => FindFirst(ClaimTypes.Name).Value;
}
public static class PrincipalExtensions
{
public static MyPrincipal AsMyPrincipal(this IPrincipal principal)
{
if (principal != null)
{
return principal as MyPrincipal
?? new MyPrincipal(principal);
}
return null;
}
}
public class HomeModule : NancyModule
{
public HomeModule()
{
Get["/] = (parameters, token) =>
{
var user = Context.CurrentUser.AsMyPrincipal();
return Task.FromResult<dynamic>(user.FullName);
};
}
}In Nancy v2 there is a whole new configuration API. This is now handled in the bootstrapper by overriding the Configure method. For Diagnostic you can use it like so:
public override void Configure(Nancy.Configuration.INancyEnvironment environment)
{
base.Configure(environment);
environment.Diagnostics(Password: "vqportal!");
}This has a different signature but now takes in a ITypeCatalog argument. If you are using the WithOverrides API this should still work, you just need to change the method signature:
protected override Func<ITypeCatalog, NancyInternalConfiguration> InternalConfiguration
{
get
{
var processors = new[] { typeof(JsonProcessor) };
return NancyInternalConfiguration.WithOverrides(x => x.ResponseProcessors = processors);
}
}This namespacehas been renamed to Nancy.Serialization.ServiceStack
One of the arguments was previously an array of ISerializer, this is now a ISerializerFactory
As of 2.x the Nancy.ViewEngines.Razor engine now uses Roslyn internally to compile views. Unfortunately the Nuget for Nancy.ViewEngines.Razor 2.0-alpha is missing a couple of dependencies which are needed to compile and render views. For the time being you will need to explicitly install these packages yourself (in the presented order)
Install-Package Microsoft.Net.Compiler -Versions 1.1.1Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform -Version 1.0.1Install-Package Microsoft.CodeAnalysis.CSharp -Version 1.1.1
If you get any runtime exceptions, please check your
*.configfile for any invalid assembly binding redirects
- 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