Is it possible to make descendant of a TService and publish my own properties?
-
28-04-2021 - |
Frage
After making a few various services in Delphi, I've realized that the TService
is lacking some of the necessary things which should come with a service application, such as logging, exception handling, and the 'Description' property in the registry.
I was wondering if it's possible for me to make my own service shell such as TJDService
which is inherited from a TService
but with some additional things, such as a 'Description' property showing in the object inspector. Can I make my own service shell like this? I know I can make my own "default project" inheriting from a TService
but that includes all my code with any new project.
When a new service is created, it should look like this:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, Vcl.SvcMgr,
JDServices;
type
TJDService1 = class(TJDService)
private
public
function GetServiceController: TServiceController; override;
end;
var
JDService1: TJDService1;
implementation
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
JDService1.Controller(CtrlCode);
end;
function TJDService1.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
end.
Same as a typical service, but using my TJDService
instead of just TService
.
Lösung
Simple question with a simple answer. Yes you can do this. I do exactly this myself to share code between the various services that are implemented in my company's codebase.
RegisterCustomModule
is the way to make your Description
property show up in the Object Inspector. Having said that, I don't find the ability to set these service properties in the Object Inspector to be all that valuable. I would regard it perfectly acceptable to set them at runtime in code, but that decision is down to personal preference.
Even if you use RegisterCustomModule
to make your service class known to the IDE, the default new service application will not use your service class. You can customise the default service application to your needs and then save it to the Object Repository.
My answer here shows how I implement an app that can be run as either a service or as a standard desktop process for debugging purposes.
Andere Tipps
Yes you can and below link on how to do it
If you don't want to have to do the work yourself SvCom offers a nice set of tools for creating services in Delphi that provide far more than what's available out of the box.
I'm going to be a rebel here, but the easiest way to do this is make your own copy of SvcMgr and make the changes there. As long as you only make changes that are accessed at run-time, build and run with your own local copy of SvcMgr. I do this to change the basic exception handling and application logging that come with a service and you can download my copy of SvcMgr.pas from my web site.
This won't work if you need design-time changes, but pretty much everything you do at design-time you can do at run-time anyway. There are better solutions, but none faster and easier.