With next release, kbmMW REST servers will be able to be hosted under Microsoft Internet Information Server by compiling the REST server as an ISAPI dll.
This blog explains how to make such a dll, how to setup IIS under Win10 to be able to run it, and how to debug it from within Delphi.
Creating the REST server DLL
I will show you how to take the SMARTSERVICE unit (Unit2) created in previous blog posts and make it available (without any changes) as part of this ISAPI based REST server.
Create a new dynamic link library project in Delphi by clicking File – New – Other and select the Dynamic Line Library icon.
It will create a DLL project, containing only the project files.
Add a TDatamodule to the project, and name it Unit1.pas. This wil lbe the unit that hosts the TkbmMWServer, transport, authorization manager, connection pools and whatever other central non visual components you may want there.
In our case we have put a TkbmMWServer (server), a TkbmMWAuthorizationManager and a TkbmMWISAPIRESTServerTransport.
Please do not use the older TkbmMWISAPIServerTransport, since it was only designed for proxying native kbmMW STANDARD transport stream format data hru, and it will thus not work with the REST transport stream format.
Set the following properties to hook up the components:
- server.AuthorizationManager = kbmMWAuthorizationManager1
- kbmMWISAPIRESTServerTransport1.Server = server
Doubleclck the datamodule to enter the create event handler. Add the following code. Actually most of it is optional, except for the highlighted lines.
procedure TDataModule1.DataModuleCreate(Sender: TObject); begin // Ask log manager to split up long messages for system logging // instead of only outputting first few hundreds characters and // snipping the rest. (Log.LogManager as IkbmMWSystemLogManager).Snip:=false; // Add a user for our sample. kbmMWAuthorizationManager1.AddActor('AUSER','','SOMEROLE'); // Register all services automatically. // Services will only be autoregistered if they have a kbmMW_Service attribute. server.AutoRegisterServices; server.Active:=true; end;
Further add the following lines in Unit1.pas.
Next step is to add the SMARTDEMO Unit2.pas file which contains the REST interface, and that you have already created in one of the previous “REST easy” blog posts.
Make sure to add Unit2 to the uses clause of Unit1.
Save and compile it.
Now its basically ready for test and deployment.
Installing Internet Information Service on Win10
Default Windows 10 do not contain IIS, but its easy to get it installed.
In the Win10 Start menu, type appwiz.cpl and run it.
Click on Turn Windows features on or off.
Select Click on the checkbox for Web Management Tools and World Wide Web Services. It will select most of what we need, but make sure to also add ISAPI Extensions in Application Development Features. Then click OK, and wait that IIS is being installed.
Now find the application Internet Information Services (IIS) Manager in the Win10 Start menu, and run it.
If you want to run 32 bit ISAPI DLL’s (which is the easier to debug) do the following, otherwise you can skip this part until creation of a virtual directory.
Rightclick on Application Pools, Default App Pool and select Advanced settings.
Select Enable 32 bit Applications and set it to true. Click OK.
Now we need to create a virtual directory, which is a reference to the directory that will hold your ISAPI DLL file. It is generally recommended that it should be stored on a local harddrive and not on a network drive. So you may need to alter the output path of the Delphi project to output to that particular directory.
In my case, I add a virtual directory called kbmmw2 which points to a local directory.
Under Default Web Site you will now have an item named kbmmw2.
Now we need to tell IIS that its allowed to use both CGI and ISAPI controls.
It opens up an empty pane. In it right click and select Edit Feature Settings.
Make sure that both CGI and ISAPI is checked. Click OK.
Next step is to click on the Handler Mappings icon.
Select the ISAPI dll line, right click it and select Edit Feature Permissions.
Make sure to check Execute and click OK. Now the ISAPI-dll line will no longer be dimmed and its state will be enabled.
Your IIS is now confiugured for running ISAPI dll’s. You can simply copy the compiled ISAPI dlls to the folder c:\temp\isapi. Then their REST features will be accessible from the outside.
Debugging ISAPI DLL’s
It is easy to debug the DLL’s with Delphi. But first you must stop the IIS service that is running. We will start it from within Delphi when we need it.
In Services, locate the World Wide Web publishing service, right click it and stop it.
Now open your project in Delphi.
In Run – Parameters you will now have to define a host application and write some parameter info.
If you are debugging a 32 bit ISAPI DLL, write as is shown in this screen dump
If you want to debug a 64 bit ISAPI DLL, the Host application should be C:\Windows\System32\inetsrv\w3wp.exe instead
Save your project.
Put a break point in your code somewhere.
Run the project. It will open a command line like window where the IIS webserver will run in. You can just stop the application as usual in Delphi when you want to, or you can type Q in the window to stop it.
Now you can open a browser and type the following (change Server.dll to whatever name your DLL file has):
The result should be that it breaks at your breakpoint, or displays “Hello world” in the browser.
16,599 total views, 4 views today