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

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

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