This repository has been archived by the owner on Jan 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Async
Kai Eichinger edited this page Apr 5, 2014
·
5 revisions
Both the Before/After pipelines, and the main route delegates, can be specified as async. The syntax is almost identical to the sync code, but with the following changes:
- The before and after hooks take two parameters, context and a cancellation token, rather than just the context.
- The route definition has an additional boolean parameter, and the delegate takes two parameters, the captured params, and the cancellation token.
public MainModule()
{
Before += async (ctx, ct) =>
{
this.AddToLog("Before Hook Delay\n");
await Task.Delay(5000);
return null;
};
After += async (ctx, ct) =>
{
this.AddToLog("After Hook Delay\n");
await Task.Delay(5000);
this.AddToLog("After Hook Complete\n");
ctx.Response = this.GetLog();
};
Get["/", true] = async (x, ct) =>
{
this.AddToLog("Delay 1\n");
await Task.Delay(1000);
this.AddToLog("Delay 2\n");
await Task.Delay(1000);
this.AddToLog("Executing async http client\n");
var client = new HttpClient();
var res = await client.GetAsync("http://nancyfx.org");
var content = await res.Content.ReadAsStringAsync();
this.AddToLog("Response: " + content.Split('\n')[0] + "\n");
return (Response)this.GetLog();
};
}
While there is nothing in the async support that requires 4.5 async/await (it's not used internally and still targets .net 4), we do expect that Tasks returned by routes to be "hot" (already started) If you choose to use this using only the TPL or using RX, please ensure every task that's returned from the delegates is hot, or the request will likely never return.
[<< Part 3. Legacy Routing](Legacy Routing) - Documentation overview - [Part 5. Taking a look at the DynamicDictionary >>](Taking a look at the DynamicDictionary)
- 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