Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE
EV Contributor.
Contributor.
283 views

How to create global variable in HP SV

Jump to solution

Hi. I have some files with string data. How to read it once (at simulation start)  to variables and use this variables in scripted c# rules?

0 Likes
1 Solution

Accepted Solutions
Jakub Honored Contributor.
Honored Contributor.

Re: How to create global variable in HP SV

Jump to solution

No event on which scripted rule can hook is triggered at simulation start. You can however initialize the data on the first request. You can do this by storing the loaded data in a static variable. You should also synchronize the access with lock, so the data are loaded really only once even if multiple clients start triggering the virtual service at the same time.

For example:

using System;
using System.IO;
using HP.SV.CSharp;

namespace HP.SV
{
    public class CSharpRule {
        private static readonly Object Lock = new Object();
        private static string myData;

        public static void Execute(HpsvRootObject hpsv)
        {
            lock (Lock) {
                if (myData == null) {
                    myData = File.ReadAllText("c:\temp\file.txt");
                }
            }
        }
    }
}

---
Service Virtualization R&D
0 Likes
1 Reply
Jakub Honored Contributor.
Honored Contributor.

Re: How to create global variable in HP SV

Jump to solution

No event on which scripted rule can hook is triggered at simulation start. You can however initialize the data on the first request. You can do this by storing the loaded data in a static variable. You should also synchronize the access with lock, so the data are loaded really only once even if multiple clients start triggering the virtual service at the same time.

For example:

using System;
using System.IO;
using HP.SV.CSharp;

namespace HP.SV
{
    public class CSharpRule {
        private static readonly Object Lock = new Object();
        private static string myData;

        public static void Execute(HpsvRootObject hpsv)
        {
            lock (Lock) {
                if (myData == null) {
                    myData = File.ReadAllText("c:\temp\file.txt");
                }
            }
        }
    }
}

---
Service Virtualization R&D
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.