/
Custom Code device usage

Custom Code device usage

The Custom Code device allows you to use C# or Visual Basic code to write your own device within the constraints of Connexion. This is useful if, for example, you need more flexible transform options than those offered in the standard HL7 Transform device.

It is also possible to write your own device using C# or Visual Basic. This is different than using the Custom Code device. If any of the following conditions apply to your organization, you may want to write your own device. If you do decide to write a device, it is recommended that you first use the Custom Code device to create the required logic, and then move that logic to your own device.

  • You are deploying Connexion outside of your infrastructure.
  • Connexion will be administered by third parties.
  • You require a user interface for device configuration/display.
  • You will be using the device logic in multiple channels.
  • Your code contains proprietary information.
  • You need advanced IDE features.

Add a Custom Code device in the same way you would add any other device. Stop the channel, open it for editing, and choose Custom Code Device from the drop-down.

Using the Custom Code window

If you select the Custom Code device in the channel, you can launch the Custom Code window.

This window contains several components:

  • Code Editor: Use this window to write the custom code for the device (in either C# or Visual Basic)
  • Auto-run: Use this option to turn the auto-run feature on or off. When Auto-run is on, your code is run after every successful compile and the Input Message (as shown in the Input Message pane), will be transformed. If your ProcessMessage method contains long-running or expensive operations, you will most likely want this feature turned off.

When the auto-run feature is off, a new Run button is displayed. If your code is error-free, click this button to apply your code to the current Input Message.

  • Code language: toggle between C# and Visual Basic.

When you change the code language, any code you have written is replaced with boilerplate code. For that reason, it is important to choose your code language before starting.

  • Run Stop Method: runs the Stop() method.
  • References: maintains a list of references used by the device (see Using References below).
  • Service References:  maintains a list of web and service references (see Using Web and Service References below).
  • Resources: maintains a list of embedded resource files (see Using Resources below).
  • Code: maintains a list of code files (see Using code files below).
  • Transform status: displays the compile status and method calls (Start, Stop, ProcessMessage).
  • Input Message: If there are messages in the upstream Queue, you can view the messages in this pane by selecting one. Use the forward/back arrows to browse through the messages in the queue. You can also paste a message into this pane. This message is used as the source message for your transform.
  • Output Message: displays the resulting message after the transform is executed, and highlights any differences.
  • Debug Output Pane: displays the results of any Logger method calls (for example, Logger.DebugFormat(...)).
  • Compile Errors: displays any errors generated by the compiler.

Using Custom Code methods

The Custom Code device boiler-plate code provides the required CustomDevice class. It also provides three, empty, overridden methods:

  • Start: Called once when this device is started by the containing channel (channel started).
  • Stop: Called once when this device is stopped by the containing channel (channel stopped).
  • ProcessMessage: Called for every message received by this device. Your message logic should be in this method.

Using References

The Custom Code device allows the addition of local custom assemblies and .NET-framework assemblies. To add an assembly to your project, right click the References item and select Add Reference. A references window is opened showing a list of all assemblies registered in the GAC. You can either select a GAC-installed assembly from the list, or click the Browse button to point to a specific assembly file.

The window contains the following:

  • Filter: Filter the GAC assemblies list.
  • Include: Check the check box beside the assembly you would like to include. Multiple assemblies can be selected before clicking OK.
  • Assembly Name: The GAC assembly name.
  • Version: The assembly version.

You may have multiple framework assembly versions listed. Be sure to choose the version that targets the .NET 4.5.1 framework.

  • Path: The path of the assembly file.
  • Browse: Click to open a specific assembly file.

It is your responsibility to include all dependent assemblies for each assembly you reference. Often the error "Metadata file 'ActiproSoftware.BarCode.Wpf351.dll' could not be found" indicates a missing required assembly. ® To remove a reference, right-click it in the list and select Remove Reference.

Using Web and Service References

You can also use references to a remote service (usually a SOAP service) in your Custom Code device. Right click the Service Reference item and select Add Service Reference. The Add Service Reference window will be displayed.

The window contains the following:

  • Service address: the URL/URI of the service to connect to.
  • Parse Service button: click to connect to the service and query for methods and properties.
  • Service Information: displays the mthods and properties exposed by the given service.
  • Add Reference button: click to generate the service proxy class and add it to your project.

Using a Service Proxy

Unlike service proxies created in Visual Studio (which use separate definition files), you must explicitly initialize your proxy object before use. For example, you can use the following code in your Start() method [Also see the sample templates included with Connexion]:

//init the bing service. The values you use here should match the contents of the wsdl file for your service.
//Binding - choose a binding which is defined in the wsdl. You can set security in the binding.security classes.
BasicHttpBinding binding = new BasicHttpBinding();
//the uri of the web service.
EndpointAddress endpoint = new EndpointAddress("http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc");
var serviceFactory = new ChannelFactory<IGeocodeService>(binding, endpoint);
m_BingService = serviceFactory.CreateChannel();
if(m_BingService != null)
Logger.Debug("Successfully created proxy to Bing geocode service.");

To remove an existing service reference, right-click the reference and select Remove Reference.

Using Resources

The Custom Code Device allows you to add embedded resources which can be accessed from your source code. To add a resource to your project, right-click the Resources item and select Add Resource. Select the file(s) to include in your project and click Open.

Embedded resources become part of the Custom Code Device configuration, allowing them to be part of exported channel files and templates. The Connexion client application does inspect the channel configurations frequently during operation, and large resource will slow down channel management. Ideally, large files accessed by your code should not be added as resources, but instead accessed as external files.

To access resources, use the following syntax:

To remove an existing resource, right click the resource to remove and select Remove Resource.

Using code files

The Custom Code Device supports multiple code files, allowing you to separate your code as desired. To add a new code file, right-click the code item and choose Add Code File. You can rename your code file by left-clicking the code file name while that item is already selected (similar to Windows Explorer).

The Custom Code Device requires one code file defining your custom device. It must implement the BaseCustomDevice, be named CustomDevice, and be defined in the Connexion.Device namespace.

To remove an existing code file, right click the code file to remove and select Remove Code File.

Using the sample devices

Connexion ships with template channels showing the custom code device being used to:

  • Access SOAP web service functionality.
  • Access REST web service functionality.
  • Host a SOAP web service.
  • Host a REST web service.

See Loading Templates in the main help file for information about loading channel templates.

Related content

New Features & Updates in v16.1
New Features & Updates in v16.1
Read with this
Building Custom Devices
Building Custom Devices
Read with this