VeriStand

cancel
Showing results for 
Search instead for 
Did you mean: 

GetIWorkspace2 failed with "Exception has been thrown by the target of an invocation"

Solved!
Go to solution

I deployed Engine Demo to `localhost`.

HongboMiao_0-1731369187694.png

 

Based on https://www.ni.com/docs/en-US/bundle/veristand-net-api-reference/page/vsnetapis/c_exec_deploying.htm...

I wrote my code:

 

 

 

using NationalInstruments.VeriStand.ClientAPI;

class Program
{
    static void Main(string[] args)
    {
        // The path to the system definition file and the IP address of the target
        string systemDefinitionPath = @"C:\Users\Public\Documents\National Instruments\NI VeriStand 2024\Examples\Stimulus Profile\Engine Demo\Engine Demo.nivssdf";
        string gatewayIP = "localhost";

        // Initialize Factory class instance to access NI VeriStand system

        try
        {
            Factory facRef = new Factory();
            Console.WriteLine("Factory created successfully");
        
            IWorkspace2 workspace = facRef.GetIWorkspace2(gatewayIP);
            Console.WriteLine("Workspace obtained successfully");

            // Connect to the system and deploy the system definition file
            workspace.ConnectToSystem(systemDefinitionPath, true, 60000); // ms
            Console.WriteLine("Deployed system.");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error type: {ex.GetType()}");
            Console.WriteLine($"Error message: {ex.Message}");
            Console.WriteLine($"Error stack trace: {ex.StackTrace}");
        }
    }
}

 

 

 

Based on the log, you can see Factory created successfully, but then failed with error:

 

 

 

Factory created successfully
Error type: System.Reflection.TargetInvocationException
Error message: Exception has been thrown by the target of an invocation.
Error stack trace:    at System.RuntimeType.CreateInstanceOfT()
   at System.Activator.CreateInstance[T]()
   at NationalInstruments.VeriStand.ClientAPI.ClientServerManager`1.GetClientServer(String address)
   at NationalInstruments.VeriStand.ClientAPI.ClientServerManagerInstances.GetDataServices(String address)
   at NationalInstruments.VeriStand.ClientAPI.WorkspaceImpl..ctor(String address)
   at NationalInstruments.VeriStand.ClientAPI.WorkspaceAggregator.Create(String address)
   at NationalInstruments.VeriStand.ClientAPI.Factory.GetIWorkspace2(String gateway_ip_address)
   at Program.Main(String[] args) in C:\Users\Hongb\Documents\Git\ConsoleApp1\ConsoleApp1\Program.cs:line 18

 

 

 

 Any guide would be appreciate, thanks! ☺️

0 Kudos
Message 1 of 2
(153 Views)
Solution
Accepted by topic author Hongbo-Miao

I got some guide at Stack Overflow and posted my answer at https://stackoverflow.com/a/79179785/2000548

 

Here is a copy:

 

---

 

Based on the error missing `System.ServiceModel.NetNamedPipeBinding`, I realized this has to be a .NET Framework 4 app instead of .NET Core 8 app, here is full working code that deploys "Engine Demo" to the gateway at localhost

 

using NationalInstruments.VeriStand.ClientAPI;

namespace VeriStandController
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            string gatewayIp = "localhost";
            string systemDefinitionPath = @"C:\Users\Public\Documents\National Instruments\NI VeriStand 2024\Examples\Stimulus Profile\Engine Demo\Engine Demo.nivssdf";

            Factory factory = new Factory();
            IWorkspace2 workspace = factory.GetIWorkspace2(gatewayIp);

            // Connect to the system and deploy the system definition file
            workspace.ConnectToSystem(systemDefinitionPath, true, 60000); // ms
            
        }
    }
}

This code now can succeed deploying the "Engine Demo" in the VeriStand.

0 Kudos
Message 2 of 2
(119 Views)