Altova MapForce 2022 Professional Edition

This example shows how to create a sample C# library and reference it in a MapForce Function File (.mff). The .mff file can then be imported as a MapForce library. Referencing a C# library in a .mff file is one of the ways to import C# libraries into MapForce. A simpler alternative is to import .NET assemblies directly. For more information, see Example: Import Custom .NET DLL Assembly.

 

Configuration steps

To reference a C# library in a .mff file, follow the instruction below.

 

Note:If you want to use custom .NET functions in the built-in output preview (in the Output pane), these functions need to be compiled for .NET Framework 4.x or .NET Standard 2.0.

 

Step 1. Create a new class library in VS

Create a new class library project in Visual Studio. Notice that the function has been defined as public static.

 

namespace MyLibrary
{
   public class Greetings
   {
       public static string SayHello(bool isMorning)
       {
           if (isMorning)
               return "Good morning!";
           return "Good Day!";
       }
   }
}

 

Step 2. Add a reference to Altova.dll

If you need special XML Schema types (such as date and duration), add a reference from your Visual Studio project to the Altova.dll library. To obtain this library, generate C# code from a mapping without custom functions. The Altova.dll file will be located in the ..\Altova\bin\debug directory relative to the directory where the code was generated. To add the reference to Altova.dll in Visual Studio, click Add Reference in the Project menu and search for Altova.dll. Then add the line using Altova.Types; to your code. For information about how XML Schema types map to C# types, see Data Type Mapping.

 

Step 3. Build your VS project

Build your Visual Studio project. The MyLibrary.dll file is generated in your project output directory.

 

Step 4.Create .mff and reference your C# library

Using an XML editor, create a new .mff file and validate it against the following schema: C:\Program Files\MapForceLibraries\mff.xsd. Make sure that all references under implementation language="cs" point to the correct C# members and paths created previously. The line function name="SayHello" must refer to the function name exactly as it was defined in C#. For details, see Configure .mff File.

 

<?xml version="1.0" encoding="UTF-8"?>
<mapping version="9" library="mylib" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mff.xsd">
  <implementations>
     <implementation language="cs">
        <setting name="namespace" value="MyLibrary" />
        <setting name="class" value="Greetings" />
        <setting name="reference" value="C:\Libraries\cs\MyLibrary\bin\debug\MyLibrary.dll" />
     </implementation>            
  </implementations>
  <group name="string functions">
     <component name="sayhello">
        <sources>
           <datapoint name="ismorning" type="xs:boolean"/>
        </sources>
        <targets>
           <datapoint name="result" type="xs:string"/>
        </targets>
        <implementations>
           <implementation language="cs">
              <function name="SayHello"/>
           </implementation>
        </implementations>
        <description>
           <short>result = sayhello(ismorning)</short>
           <long>Returns "Good morning" or "Good day", depending on the input parameter.</long>
        </description>
     </component>      
  </group>
</mapping>

 

Step 5. Import .mff as a library

Now that your custom library is referenced in the .mff file, you can import the .mff file into MapForce as a library. For more information, see Import.mff Libraries.

 

© 2016-2022 Altova GmbH