Use a third-party library in a C# rule

You can add external libraries to your C# scripted rules.

For example, you may want to:

  • use a third-party library for reading an Excel 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.

Use a third-party library

To use 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.

    • <Designer installation folder>\Service Virtualization Designer\Designer\Extensions
    • <Server installation folder>\Service Virtualization Server\Server\Extensions
  3. 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
  4. 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=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>

  5. Restart the relevant application to apply the changes:

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