Friday, August 19, 2016

Using RemObjects Hydra to consume a .NET WCF service from Delphi

Using RemObjects Hydra is probably the easiest way to consume a WCF service from Delphi in case you need to support .NET-specific bindings which might require a lot of work to implement on your side but are already available for .NET.

This series of videos shows how you can write a managed Hydra plugin module to consume a WCF service and expose it to a Delphi Hydra host application as a custom interface, delegating the method calls to the WCF service.

01. WCF Service Library

  • Create a new WCF Service Library project:
    • File\New\Project...
    • Templates\Visual C#\WCF\WCF Service Library
  • Add a new method to the service contract (interface)
  • Implement the new method
  • Test the service with the WCF test client

02. WCF Service Client Library

  • Create a new class library project:
    • Right-click solution, Add\New Project...
    • Installed\Visual C#\Class Library
  • Add service reference (to the service created in the previous step)
  • Delete the default empty class module


03. WCF Service Client Console Application

  • Create a new console project:
    • Right-click solution, Add\New Project...
    • Installed\Visual C#\Console Application
  • Add System.ServiceModel reference
  • Add service client library reference
  • Add code to call the service methods
  • Run the console application



04. Hydra C# Plugin Module

  • Create a new Hydra Plugin Library project:
    • Right-click solution, Add\New\Project...
    • Installed\Visual C#\Hydra\Plugin Module
  • Create a new Hydra Plugin:
    • Right-click project, Add\New Item...
    • Installed\Visual C# Items\Non-Visual Plugin
  • Add System.ServiceModel reference
  • Add service client library reference
  • Create a COM-compatible interface
    • Right-click project, Add\New Item...
    • Installed\Visual C# Items\Code\Interface
    • Declare interface IService1Client (public)
    • Inherit from IHYCrossPlatformInterface (RemObjects.Hydra.CrossPlatform)
    • Add IID
    • Add methods
    • Add CompositeType struct
      • Add StructLayout attribute
      • Add MarshalAs attributes
    • Declare data contract parameter ref on the C# side, const on Delphi side
  • Make the plugin implement the interface by delegating to the WCF service client library



05. Delphi Hydra Host Console Application

  • Create a console application
  • Add Vcl to unit scope names
  • Change the output directory to be the same as the plugin output directory
  • Write code to load the plugin and call its interface
  • Copy and patch uHYCLRWrappers.pas unit to load configuration from a .config file
  • Create the configuration file



You can find the source code here:
https://github.com/tondrej/hydra-wcf-delphi

Friday, August 05, 2016

Include resource files in your Delphi build process

Quite often people don't know about this neat little feature, as recently in this g+ discussion, too.

You can simply make any .rc file a part of your project and its build process. There's a BrccCompile target defined in your $(BDS)\bin\CodeGear.Delphi.Targets file which is included in every Delphi project by default and it takes care of rebuilding the resources with the brcc32 resource compiler.

One easy way to do this is to select "File\New\Other..." menu item to open the "New Items" dialog and select "Text file" under "Other Files":


In the following "New file" prompt, select ".rc Resource Script" and check the "Add new file to current project" checkbox:


Alternatively, you can create your .rc file in an external editor and drag-and-drop it from the Windows Explorer onto your project's node in the Project Manager treeview.

In either case your new .rc file becomes a part of the project:


At the same time a $R directive similar to this is added to your project's source file (.dpr):

{$R 'New1.res' 'New1.rc'}

That's it. Any modifications to your .rc files will now be recompiled automatically into corresponding .res files and linked into your executable, as a part of the build process. And you can open and edit them easily in the IDE directly, too.