C# Security Exception

14.8k views Asked by At

When running this program I keep receiving the error: An unhandled exception of type 'System.Security.SecurityException' occured Additional Information: ECall methods must be packaged into a system module.

 class Program{
        public static void Main()
        {
            Brekel_ProBody2_TCP_Streamer s = new Brekel_ProBody2_TCP_Streamer();
            s.Start();
            s.Update();
            s.OnDisable();
        }
    }

How can I fix this?

The important part of the Brekel library is as follows:

//======================================
    // Connect to Brekel TCP network socket
    //======================================
    private bool Connect()
    {
        // try to connect to the Brekel Kinect Pro Body TCP network streaming port
        try
        {
            // instantiate new TcpClient
            client = new TcpClient(host, port);

            // Start an asynchronous read invoking DoRead to avoid lagging the user interface.
            client.GetStream().BeginRead(readBuffer, 0, READ_BUFFER_SIZE, new AsyncCallback(FetchFrame), null);

            Debug.Log("Connected to Brekel Kinect Pro Body v2");
            return true;
        }
        catch (Exception ex)
        {
            Debug.Log("Error, can't connect to Brekel Kinect Pro Body v2!" + ex.ToString());
            return false;
        }
    }


    //===========================================
    // Disconnect from Brekel TCP network socket
    //===========================================
    private void Disconnect()
    {
        if (client != null)
            client.Close();
        Debug.Log("Disconnected from Brekel Kinect Pro Body v2");
    }

 public void Update()
{
    // only update if connected and currently not updating the data
    if (isConnected && !readingFromNetwork)
    {
        // find body closest to the sensor
        closest_skeleton_ID = -1;
        closest_skeleton_distance = 9999999f;
        for (int bodyID = 0; bodyID < skeletons.GetLength(0); bodyID++)
        {
            if (!skeletons[bodyID].isTracked)
                continue;
            if (skeletons[bodyID].joints[(int)brekelJoint.waist].position_local.z < closest_skeleton_distance)
            {
                closest_skeleton_ID = bodyID;
                closest_skeleton_distance = skeletons[bodyID].joints[(int)brekelJoint.waist].position_local.z;
            }
        }

        // apply to transforms (cannot be done in FetchFrame, only in Update thread)
        for (int bodyID = 0; bodyID < skeletons.GetLength(0); bodyID++)
        {
            for (int jointID = 0; jointID < skeletons[bodyID].joints.GetLength(0); jointID++)
            {
                // only apply if transform is defined
                if (skeletons[bodyID].joints[jointID].transform != null)
                {
                    // apply position only for waist joint
                    if (jointID == (int)brekelJoint.waist)
                        skeletons[bodyID].joints[jointID].transform.localPosition = skeletons[bodyID].joints[jointID].position_local;

                    // always apply rotation
                    skeletons[bodyID].joints[jointID].transform.localRotation = skeletons[bodyID].joints[jointID].rotation_local;
                }
            }
        }
3

There are 3 answers

1
Brandon On BEST ANSWER

It appears you are using a Unity library but trying to run it as a standalone application?

This error means you are calling a method that is implemented within the Unity engine. You can only use the library from within Unity.

If you want to use it standalone, you'll need to compile the library without referencing any Unity libraries, which probably means you'll need to provide implementations for anything the library is using (such as MonoBehaviour

http://forum.unity3d.com/threads/c-error-ecall-methods-must-be-packaged-into-a-system-module.199361/

http://forum.unity3d.com/threads/security-exception-ecall-methods-must-be-packaged-into-a-system-module.98230/

0
Aleksandar On

Additionally, If your only problem is Debug.Log() throwing an exception, you could use reflection to plant your own Logger instance instead of Unity's one.

Step 1: Create "MyLogHandler" that will do your actual logging(write to file or to console or do nothing). Your class needs to implement "ILogHandler" interface.

Step 2: Replace unity default one with new one.

var newLogger = new Logger(new MyLogHandler());
var fieldInfo = typeof(Debug).GetField("s_Logger", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);
        fieldInfo.SetValue(null, newLogger);

Note: Keep in mind that reflection accesses field by name and if Unity decide to change it in future, you will not get compile error - exception will be thrown in run-time.

0
S.C. On

I know this is old, but I came across a way to unit test Unity assemblies from within Visual Studio just by toggling a symbol definition from the Unity build settings. As long as you're OK with only being either able to either run tests or have the testable components usable in unity at one time, you can toggle unit testing mode and unity mode like this (images follow):

  1. Make your unity component a partial class. Have one file where you declare that the partial class extends MonoBehaviour and put any stuff that has to actually use unity assemblies in there. This will not be tested by the unit tests but everything else will.
  2. Use conditional compilation to make that file's contents only compile when a specific symbol is defined during the build. I used UNIT_TEST_NO_UNITY_INTEGRATION in my case.
  3. When you want to run the unit tests from Visual Studio, update the build settings to define that symbol. This will exclude the Unity specific stuff from step 1 from the build and allow Visual Studio to be able to run your unit tests.
  4. When you are finished testing, edit the build settings again and remove that symbol definition. Now your unit tests won't be able to run but your assemblies will work within Unity again.

enter image description here enter image description here