Showing posts with label Hydra. Show all posts
Showing posts with label Hydra. Show all posts

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

Sunday, July 17, 2016

Embracing .NET with RemObjects Hydra

If you're a Delphi shop and find yourself (sometimes) jealous of all the .NET goodies have a look at RemObjects Hydra (more details here). It's a framework which allows you to mix and use VCL, FireMonkey and .NET code modules in a single mixed-mode application. (They've recently started the "Escape from Delphi" offer.)

Your host application can be an unmanaged 32-bit or 64-bit Delphi VCL or FireMonkey application (written in Delphi 7 or later), or a managed WinForms .NET application (written in any CLR-compliant language like C#, Oxygene, VB.NET, etc).
Your plugin libraries (loadable by the host dynamically at runtime) can implement non-visual or visual plugin modules which can be unmanaged Delphi VCL or FireMonkey, or managed .NET WinForms, WPF or Silverlight DLLs. The host and the plugins can communicate with each other through interfaces. IDE support with templates and wizards to create the hosts and the plugins, for both Visual Studio and Delphi, is included.

If your trusty old Delphi codebase comprises millions of lines of code the idea of porting that code over to .NET might be intimidating. It probably seems completely out of question, an investment of huge amount of time and effort into a project with uncertain benefits. The development of new features during the transition might be stalled or limited, and you're going to have a period of time when nothing works. In short, probably a nightmare.

With Hydra now, you can think about it again: You can start with a skeleton host application, quite simply rebuilding your existing Delphi units into plugin modules, keeping all your existing code working as before. You can then port the modules to .NET one by one, at your own pace, if at all. Simultaneously, you can develop new modules in .NET already. All of a sudden, it's not so bad anymore, it's definitely doable, and probably fun.

Below is a screenshot of a very quick exercise in rebuilding the SearchBox VCL sample as a Hydra plugin. It took me only a few minutes to resolve some issues: to include the VCL styles, the DLL must link in the sample's resource file, and use the VCL.Styles unit. I also had to adjust the form slightly: remove border icons and make it visible. Here's the result: