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
Highlighted
Jakub Honored Contributor.
Honored Contributor.
446 views

Scripting: How to share data between services

Storing value from a request in a Server context In virtual service A:

using HP.SV.CSharp;

namespace HP.SV {
    public class CSharpRule {
        private const string FirstNameKey = "FirstName";
public static void Execute(HpsvRootObject hpsv) { string firstName = hpsv.Request.memberSearch.name.firstName; hpsv.Contexts.Server.Add(FirstNameKey, firstName); } } }

Retrieving value from a Server context in virtual service B, clearing the context and writing the data to the response:

using HP.SV.CSharp;

namespace HP.SV
{
    public class CSharpRule {
        private const string FirstNameKey = "FirstName";

        public static void Execute(HpsvRootObject hpsv) {
            if (hpsv.Contexts.Server.ContainsKey(FirstNameKey)) {
                string firstName = (string)hpsv.Contexts.Server[FirstNameKey];
hpsv.Contexts.Server.Remove(FirstNameKey); Member1 member = new Member1(); member.socialSecurityNumber = firstName; hpsv.Response.memberSearchResponse.memberSearchResult.Member.Add(member); } } } }

Warning: Using Server level context is dangerous as it is cleared only when SV Server is restarted and therefore incorrect usage can cause memory leaks. If you need to share data just between individual calls to the same virtual service, it's better to use Service context.

Further help on Contexts and scripting in general is available here.

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