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

No comments: