RServer class
Il riferimento e' in inglese: viene dai commenti del sorgente di RAVAL, che dalla 2.0 ha nomi e commenti in inglese.
public sealed class RServer : IDisposable, IAsyncDisposableJSON API server on Kestrel: each path calls a method of the program.
Costruttori
RServer(int port = 0)The server on the given port; 0 = a free port chosen by the system (see Addresses).
ProprietÃ
int Port { get; }The requested port (0 = chosen by the system at start).
bool AllInterfaces { get; }true if it listens on every network interface, false only on this computer (127.0.0.1).
bool UiThread { get; }true if the methods run on the user interface thread.
JsonSerializerOptions Json { get; }How JSON is written and read: camelCase, like JsonSerializerDefaults.Web. It can be changed before StartAsync.
bool Started { get; }true after StartAsync and until StopAsync.
IReadOnlyList<string> Addresses { get; }The addresses it answers on, with the real port (for instance «http://127.0.0.1:51234»); empty if not started.
Metodi
RServer Get(string path, Func<RRequest, object> method)GET: read. The path can have parameters in braces: «/api/machines/{id}».
RServer Post(string path, Func<RRequest, object> method)POST: create or run an action, usually with a JSON body.
RServer Put(string path, Func<RRequest, object> method)PUT: replace a resource.
RServer Delete(string path, Func<RRequest, object> method)DELETE: delete a resource.
RServer GetAsync(string path, Func<RRequest, Task<object>> method)GET with an asynchronous method (that reads a database, calls another service...).
RServer PostAsync(string path, Func<RRequest, Task<object>> method)POST with an asynchronous method.
RServer OnAllInterfaces()Listens on every network interface: the server can be reached from other computers or from a phone.
RServer OnUiThread()Runs the methods on the user interface thread, so they can read and change the controls.
void Start()Starts the server and returns when it is listening. For those who do not use async/await yet.
void Stop()Stops the server and frees the port. For those who do not use async/await yet.
void Run()Starts the server and stays here until another thread calls Stop: the classic way, with a dedicated thread. new Thread(server.Run).Start() in a program with windows, or Run() at the end of the Main of a console program that is only a server.
bool WaitStarted(TimeSpan timeout)Waits (at most for the given time) until the server is listening: for those who started it with Run in another thread.
Task StartAsync()Starts Kestrel with the registered paths; returns when the server is listening.
Task StopAsync()Stops the server and frees the port; it can be called even if it is not started. The requests in progress have at most 5 seconds to finish: with OnUiThread a request waiting for the user interface thread, while the interface waits for Stop, does not block the program forever.
void Dispose()Like Stop: to use the server with using.
ValueTask DisposeAsync()Like StopAsync: to use the server with await using.