Adding dynamic synchronous resources
To publish resources through our web server, we just call the Register
method on the HttpServer
object. We can choose between two methods: either we provide resource objects, inherited from the HttpResource
class, such as the HttpSynchronousResource
or HttpAsychronousResource
classes, or we use lambda expressions or delegates for simple GET
and POST
resources.
We first demonstrate the latter to publish a resource for reading momentary sensor data. We provide a lambda expression, taking a request and response parameter. This expression will be executed when a GET
method is received on the /Momentary
resource:
this.httpServer.Register("/Momentary", (req, resp) => { ... });
Note
The resource will be added as a synchronous resource. This means that the response must be generated completely before returning from the expression. We don't have to worry about exceptions or explicitly sending the response. The web server will do that for us if we forget...