Use a third-party library in a C# rule

This topic describes how to add new external libraries to your C# scripted rules and to use existing libraries in Service Virtualization.

About third-party libraries

You can add external libraries to your C# scripted rules. For example, you may want to:

  • Use a third-party library for reading a data file or accessing a database

  • Share your own library across multiple rules or services instead of duplicating the code in every rule/service

Your DLL library must be a valid .NET assembly that is built for 64-bit (AMD64) or neutral (MSIL) architectures. Make sure the DLL is compatible with the version of .NET used by Service Virtualization. To obtain the .NET version in use, check the sku attribute in the supportedRuntime element in VirtualServiceDesigner.exe.config or HP.SV.StandaloneServer.exe.config. The following example shows Service Virtualization uses .NET framework version 4.8, therefore, your DDL should be compiled against .NET 4.8 or a compatible version such as .NET Standard 2.0.

<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />.

Caution: Referencing a third-party library can introduce risk. It may not function properly, or may negatively impact Service Virtualization Server functionality.

Back to top

Add a new third-party library

You can add third-party libraries to be used in Service Virtualization. For example, you can add a new library that you developed, or a library from elsewhere that Service Virtualization doesn't already use.

To add a third-party library:

  1. Disable the C# rule sandbox mode. For details, see Configure C# Security.
  2. Copy the external library that you want to use (for example, MyCompany.MyLibrary.dll) to the Extensions folder of the Service Virtualization Designer or Server, depending on where the virtual service will run.

    Tip: We recommend creating a sub-folder with a descriptive name such as MyLib in the Extensions folder, and putting your DLL library there to keep things organized.

    • <Designer installation folder>\Service Virtualization Designer\Designer\Extensions
    • <Server installation folder>\Service Virtualization Server\Server\Extensions
  3. Restart the relevant application to apply the changes:

    • Designer: Close and reopen the Designer.
    • Server: Restart the Service Virtualization Server service.

Back to top

Use an existing third-party library

You can use a library that Service Virtualization already uses, or a library already installed on that machine which Service Virtualization doesn't already use.

To use an existing third-party library:

  1. Disable the C# rule sandbox mode. For details, see Configure C# Security.
  2. By default, Service Virtualization loads common C# libraries (such as LINQ, HttpClient, and so forth) and other user-provided assemblies from the Extensions folder. All other assemblies need to be specified within the scripted rule context, as described below.

    Open the appropriate scripted-rule-context.xml file:

    • Designer: <Designer installation folder>\Service Virtualization Designer\Designer\bin\Config\scripted-rule-context.xml
    • Server: <Server installation folder>\Service Virtualization Server\Server\bin\Config\scripted-rule-context.xml
  3. Instruct the C# rule to reference your third-party library/assembly:

    1. If the assembly is used by SV, add it to the ReferenceAssemblies property using the short assembly name. Separate multiple assemblies with a comma. For example,

      <property name="ReferenceAssemblies" value="Newtonsoft.Json,NPOI,your_assembly_name" />

    2. If the assembly is not used by SV, you must provide a full assembly name, with one assembly per value element. For example,

      <value>System.Security.Cryptography.X509Certificates, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>

  4. Restart the relevant application to apply the changes:

    • Designer: Close and reopen the Designer.
    • Server: Restart the Service Virtualization Server service.

Back to top