Monday, April 20, 2009

Web Service Projects and Sessions

Apart from ASP.NET Web Service solutions using the Web Site target, the Feb 2009 update of Delphi Prism introduced Web Projects that also support ASP.NET Web Services. Let's use this new way as basis for the next example that demonstrates the use of the EnableSession attribute.
First of all, do File New, but this time do not select File New - Web Site, but select File New - Project instead. As one of the New Project targets, we can select Web and then pick an ASP.NET Web Application, as can be seen below.

Note that we can select the (minimum) version of the .NET Framework for this project (3.5 in the screenshot above), and unlike the Web Site, we cannot select HTTP as location, but need to specify a normal location for the Web Project. When we deploy the project, we can select a HTTP location as we'll see later in this section. For the new example, select MyWebService as Name for the solution as well as the project. Note that this will produce an ASP.NET Web Application, and not directly an ASP.NET Web Service, but this can be "fixed" after the project is created.
After we click on OK, a new ASP.NET Web Application project called MyWebService is created (in a solution which is also called MyWebService). The Solution Explorer shows the project with a Default.aspx web page, as well as a Global.asax (for the ASP.NET application object) and a web.config file. We don't need the Default.aspx and related source files, so open the node in the Solution Explorer, select the three files Default.aspx, Default.aspx.designer.pas as well as Default.aspx.pas, right-click on the nodes and select Remove. In the dialog that follows, you can click on Delete to permanently delete Default.aspx (and the related .pas files).
Without the Default web page, the project is only ASP.NET and needs another main item. Right-click on the MyWebService project node, and select "Add - New Item. In the dialog that follows, we need to go to the Web category and select a Web Service.

Change the name of the Web Service from WebService1.asmx to Service.asmx and click on Add.
First, modify the namespace attribute again,

type
[WebService(&Namespace := 'http://eBob42.org/',
Description := 'This web service is a new demo Web Service<br>' +
'generated by <b>Delphi Prism</b> for ASP.NET 2.0 or higher')]
[WebServiceBinding(ConformsTo := WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET
// AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
Service = public class(System.Web.Services.WebService)
public


Note the new comment about the System.Web.Script.Services.ScriptService, to allow the web service to be called from JavaScript, a topic for a future article.


EnableSession

Then, we should remove the demo method HelloWorld, and add four new WebMethods.
All of them with the EnableSession attribute set to true, to enable the use of the ASP.NET session object for these four web methods:



Service = public class(System.Web.Services.WebService)
public
[WebMethod(EnableSession := true)]
method Remember(const Name,Value: String);
[WebMethod(EnableSession := true)]
method Recall(const Name: String): String;
[WebMethod(EnableSession := true)]
method Forget(const Name: String);
[WebMethod(EnableSession := true)]
method Amnesia; // forget all
end;


Press Ctrl+Shift+C to generate the skeletons for the four web methods, and implement them as follows:



method Service.Remember(const Name,Value: String);
begin
Session[Name] := Value
end;

method Service.Recall(const Name: String): String;
begin
if
Assigned(Session[Name]) then
Result := Session[Name].ToString
else Result := ''
end;

method Service.Forget(const Name: String);
begin
Session.Remove(Name)
end;

method Service.Amnesia;
begin
Session.Abandon
end;


The implementation of the four web methods is relative straightforward, using the ASP.NET Session object to store the name-value combinations, or clear them (or clear everything, by calling the Session.Abandon method).


We can now run the server in a browser using Debug Start without Debugging.
This will start the ASP.NET Deployment Server (at http://localhost:2743 on my machine), showing the web service on our localhost.





If you didn't selected Service.asmx as Start Page (by right-clicking on it in the Solution Explorer), you may need to manually add Service.asmx to the URL in the browser. Then, you can use the browser to test the web service. For example, you can click and execute the Remember method and make the web service remember a name-value combination, which you can then use to test Recall (or Forget). This should all work just fine, preparing the way for the actual web service client.

Client
Although you may want to deploy the web service before releasing the client, we can already create a client application and allow it to consume the web service part of the same solution.
First, let's create a new Windows Application, call it WindowsClientApplication.

Knowing what and when to optimize

In this thread on the Delphi forums Ante Bonic brought back to intention this excellent Delphi Optimization Guide in Delphi article by Robert Lee. The article has aged a bit, but many tips remain true with the Delphi 2009 compiler (sadly so). Like many optimization articles, Robert’s focuses on mostly local optimization tips, which can draw in warnings like this one one by Anders Isaksson:

Optimization should be done after profiling, not before.

Which I couldn’t agree more with. But to be fair, Robert’s states so in his article, as do most authors of optimization articles. Recipes and local optimization tips are to be used after all algorithmic and data structures improvements have been taken advantage off.

If one can list tips and tricks for local optimization, do’s and don’ts that are true often enough to be good tips in many scenarios. However, it’s practically impossible to come up with a “reusable” list of tips for algorithms and data structures. Too many specifics can come together, even when the problems are similar, considerations of scale or reactivity can drastically influence architectural and algorithmic options.

Hence the most visible optimization recipes are often local optimization ones, but mostly because there are few global optimization recipes. You only have global optimization methodologies. But even these methodologies can usually be summarized with few words:

Time, profile, analyze and confirm your bottlenecks.
Improve algorithms & data structures.
Exhaust 1 & 2 before looking at local optimizations, and then don’t forget 1.
To optimize efficiently, ie. not waste your time, you have to master the first point.
To optimize effectively, ie. not waste the machine time, you have to master the second.

And the third point you ask? It’s a razor’s edge, when applied effectively, it can be very efficient, with very few changes like in this case, but if not, it’s a good way to end up there. To be effective, local optimization has to be about taking care of hidden machinery, hidden shortcomings of the compiler, hidden algorithms and data-structures that get in the way.

I’ll close this post by quoting Robert Lee’s article on timing:

Timing code is generally called “profiling”. If you want to improve the performance of your code, you first need to know precisely what that performance is. Additionally, you need to re-measure with each change you apply to your code. Do not spend a single second twiddling code to improve performance until you have analytically determined exactly where the application is spending its time. I cannot emphasize this enough.

SmartFlash Amazing splash screens


SmartFlash is a VCL with additional features in Macromedia Flash ActiveX.
SmartFlash support real trasparency of the flash and antialiasing effect in the flash frame. Also you can load flash, FLV from stream and grab frame to the bitmap with alpha-channel. With SmartFalsh you can create flash interface in your applications (flash forms, menus, controls and more).

What's FFVCL and What's FFmpeg?


FFVCL is a native VCL components suit including Video Converter and Video Player which wrap FFmpeg library perfectly. FFVCL is more easy, more flexible and more powerful than the official FFmpeg command line tool for converting and playing audio and video files.

FFmpeg is a complete solution to record, convert and stream audio and video. It includes libavcodec, the leading audio/video codec library. FFmpeg is developed under Linux, but it can compiled under most operating systems, including Windows.


Relation between FFVCL and FFmpeg


Because FFVCL is a direct wrapper of FFmpeg's libavcodec APIs, it is able to reflect the power of FFmpeg library. And it is more flexible and more powerful than the official FFmpeg command line tool. At the same time, FFVCL is very easy to use. Therefore, FFVCL is better than FFmpeg tool in some measure.

TAdvStringGrid 4.5 And TMS Smooth Controls Pack v2.0 View