Hello WAVE
NFX.WAVE represents "(W)eb(A)pp(V)iew(E)nhanced" web server which provides DYNAMIC web site services.
This server is not meant to be exposed directly to the public Internet, rather it should be used as an application server behind the reverse proxy, such as NGINX.
This server is designed to serve dynamic data-driven requests/APIs and not meant to be used for serving static content files (although it can).
The implementation is based on a lightweight HttpListener that processes incoming Http requests via an injectable WorkDispatcher which governs the threading and WorkContext lifecycle.
Below is step-by-step guide to create the simplest NFX.Wave application that outputs "Hello WAVE!" message to your browser.
Create new C# Console Application project
HelloWorld.Add reference to NFX (e.g. via NuGet).
Add the following code to
Program.cs:using System; using NFX.ApplicationModel; using NFX.Wave; namespace HelloWorld { class Program { static void Main(string[] args) { try { using (var app = new ServiceBaseApplication(args, null)) using (var server = new WaveServer()) { server.Configure(null); server.Start(); Console.WriteLine("server started..."); Console.ReadLine(); } } catch (Exception error) { Console.WriteLine("Critical error:"); Console.WriteLine(error); Environment.ExitCode = -1; } } } }Here we create web server instance (
WaveServer) in the scope of application container (ServiceBaseApplication). In this example the server is configured with application-level configuration -server.Configure(null)(to be added below), but one can configure web server with any specific configuration.Create project folder
Pagesand add a filePages\Index.nhtwith the following contents:#<laconf> compiler { base-class-name="NFX.Wave.Templatization.WaveTemplate" namespace="HelloWorld.Pages" } #</laconf> <!DOCTYPE html> <html> <head> </head> <body> <div id="root">Hello WAVE!</div> </body> </html>This is the simplest NTC template of application start page. The code in
#<laconf>brackets contains directives for NFX Template Compiler which generates C# class from the template.To obtain a file with C# class from the
Indexpage template you can choose from two variants: explicitly run ntc tool with proper parameters or add following line (fix path to ntc tool if it is necessary) to project pre-build events"$(SolutionDir)packages\NFX.3.4.0.1\tools\ntc" "$(ProjectDir)Pages\*.nht" -r -ext ".auto.cs" /srcAdd the resulting file (e.g. Index.nht.auto.cs) with
Indexclass to the project.Create project folder
Controllersand add a classControllers\Hello.cs:using NFX.Wave.MVC; using HelloWorld.Pages; namespace HelloWorld.Contollers { public class Hello : Controller { [Action] public object Index() { return new Index(); } } }Add configuration file
HelloWorld.laconfwith the following contents:application { wave { server { prefix { name="http://localhost:8070/" } dispatcher { handler { type="NFX.Wave.Handlers.MVCHandler, NFX.Wave" type-location { assembly="HelloWorld.exe" ns { name="HelloWorld.Controllers" } } match { path="/{type=Hello}/{mvc-action=Index}" } } } } } }Make sure that Build Action is set to None and Copy to Output Directory is set to Copy Always.
In prefix section we define the address where we can send a request to the server.
In handler section we define type of the handler, assembly where corresponding controllers are placed and pattern of path to controller and action.
- Run the
HelloWorldapplication, open browser and typehttp://localhost:8070/hello/indexor justhttp://localhost:8070in the address bar.