Friday, March 5, 2010

Delphi Prism ASP.NET web chat service

Using Delphi 2010, we can build ASP.NET web services and projects, as I often demonstrate in my ASP.NET workshops and training events (in The Netherlands, UK and Sweden for example). In this article, I'll demonstrate how we can use Delphi Prism and ASP.NET to write a web chat service, consuming it in an ASP.NET web form as well.
In order to participate with the demo of this month, you need a copy of Delphi Prism. After starting Delphi Prism, do File | New ?Project, and in the Web node of the Delphi Prism project types, select the ASP.NET Web Application. Specify WebChat as name of the application. This will create a new solution with one project called WebChat

Unfortunately, inside the generated source code in file chat.asmx.pas the [WebMethod] attribute still appears above the implementation of the example method chat.HelloWorld instead of the definition of this method in the chat class. We need to move that attribute from the implementation to the definition. In fact, we don't need the entire HelloWorld method at all, so feel free to remove both the definition and implementation.
For the chatroom example, I need two methods: one to send a chat message, and another to retrieve all chat messages.

Delphi 2010 Delayed Dynamic Link Libraries

Traditionally, Dynamic Link Libraries (DLLs) can be loaded in two different ways: implicit or explicit.

unit DelayedHandler;
interface
uses

SysUtils;

type
ELoadLibrary = class(Exception);
EGetProcAddress = class(Exception);

implementation

function
DelayedHandlerHook(dliNotify: dliNotification;
pdli: PDelayLoadInfo): Pointer; stdcall;
begin
if
dliNotify = dliFailLoadLibrary then
raise ELoadLibrary.Create('Could not load ' + pdli.szDll)
else
if
dliNotify = dliFailGetProcAddress then
if
pdli.dlp.fImportByName then
raise
EGetProcAddress.Create('Could not load ' +
pdli.dlp.szProcName + ' from ' + pdli.szDll)
else
raise
EGetProcAddress.Create('Could not load index ' +
IntToStr(pdli.dlp.dwOrdinal) + ' from ' + pdli.szDll)
end;

initialization
SetDliFailureHook(DelayedHandlerHook);
finalization
SetDliFailureHook(nil);
end.

C++Builder Developer’s Journal – Call for Papers out for May 2010 issue

The C++Builder Developer’s Journal is a great magazine run by some really dedicated guys. I’ve been a subscriber for quite awhile and find it almost always has a couple of articles I’m interested in. They are very often applicable to Delphi as well. Anyway, the May 2010 issue will be an open one, meaning it will be freely available to anyone, not just subscribers. They are running a Call for Papers right now, so if you’ve had an idea for an article burning away in the back of your mind, or just like the idea of the fame and fortune that comes with being a published author (yes, I kept a straight face while typing that. Getting good at this, aren’t I?) go and check it out.

Delphi Prism 2010 Does Not Seem To Use Subdirectories

When working on an ASP.NET website, I find it a lot easier if you put related sections in seperate subdirectories. For example:

CSS and Javascript can be put in a directory with caching in order to speed up the page loads

Pages that are only available to certain roles (i.e. “Admin”) can be put in a seperate directory and then you set the web.config so that only that role can access the directory. This helps to make it really clear who the page is ment for.

While attempting to port a site from Delphi RAD 2007 to Delphi Prism 2010, I was amazed that it would not let me do this. I even opened a site in Visual Web Developer 2008 (the Microsoft compeditor that Prism basically uses as the IDE) to check if it was a Microsoft issue. Not only did the Microsoft Web Developer 2008 allow it, but when I went to create a code file, it suggested putting it into app_code and offered to move it there for me (but did not force me to if I didn’t want to).

In comparison, in Delphi Prism 2010, I opened {project}\css\proj.css. Instead of allowing me to use the file where it was, Prism created a brand new file {project}\proj.css and added that to the project. To add to the confusion, it left the old file {project}\css\proj.css. Now I have two copies of proj.css. I tried it with several different files and kept having this issue.

Not only is this annoying, I find that this really makes a larger site a lot harder to maintain.