For every HTTP request the Ventura SQL server runtime receives, a Custom Request Handler is instantiated. The custom request handler is used for authentication and looking up the database connection string.


The class instance will remain alive until the request is completed and the response has been transmitted back to the client.


The incoming HTTP requests originate from Recordset.ExecSql(), Transactional.ExecSql(), Recordset.SaveChanges() and Transactional.SaveChanges().


The custom request handler is a class that implements the IVenturaRequestHandler interface. The IVenturaRequestHandler interface implements 2 methods:


When the web application starts you need to tell the runtime which request handler class to use. This is done with the RequestHandler property. The VenturaServerConfig.RequestHandler is a delegate is set to a method that returns a new instance of the custom request handler class.

Setting the RequestHandler delegate

A lambda expression is the simplest way set the delegate:


VenturaServerConfig.RequestHandler = () => new CustomRequestHandler();


Every time the server needs a new instance of the custom request handler class, it executes the RequestHandler delegate.


It is not obligated to set the RequestHandler property. If not set, authentication will be disabled and the VenturaConfig.DefaultConnector will be used for processing requests.


The example below shows where in the ASP.NET server code you need to set the RequestHandler property.


ASP.NET

public class Global : System.Web.HttpApplication

{

   protected void Application_Start(object sender, EventArgs e)

   {

       VenturaServerConfig.RequestHandler = () => new CustomRequestHandler();

   }

}


ASP.NET Core

public class Startup

{

   public void ConfigureServices(IServiceCollection services)

   {

   }


   public void Configure(IApplicationBuilder app, IHostingEnvironment env)

   {

       VenturaServerConfig.RequestHandler = () => new CustomRequestHandler();

   }

}

Implement a custom request handler class

Below is an example of a complete custom request handler for an ASP.NET or ASP.NET Core Web application.


using System;

using System.Configuration;

using System.Data.SqlClient;

using Ventura;

using VenturaServer;


namespace AspNetServer

{

   public class CustomRequestHandler : IVenturaRequestHandler

   {

       public void Authenticate(bool is_secure_connection, string username, string password, string databag)

       {


           if (is_secure_connection == false) // SSL is obligated

               throw new Exception("Authentication is not allowed over an open (non-SSL) connection.");


           if (username != "admin" || password != "password")

               throw new Exception("Incorrect username and/or password.");


       }


       public AdoConnector LookupAdoConnector(string remote_connector_name)

       {

           var connection_string = "Server=(local);Initial Catalog=AdventureWorks2017;Integrated Security=SSPI;Max Pool Size=250;Connect Timeout=30;";


           return new AdoConnector(SqlClientFactory.Instance, connection_string);

       }


   }

}