C # Security Exception

When I run this program, I get an error all the time: An exception of type "System.Security.SecurityException" is fixed. Additional information: ECall methods must be packed into the 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?

An 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; } } } 
+9
c # visual-studio securityexception
source share
4 answers

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

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

If you want to use it autonomously, you will need to compile the library without reference to any Unity libraries, which probably means that you need to provide an implementation for anything that the library uses (e.g. 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/

+22
source share

Also, if your only problem is that Debug.Log () throws an exception, you can use reflection to set your own Logger instance instead of Unity one.

Step 1: Create a “MyLogHandler” that will do your actual logging (write to a file or console or do nothing). Your class should implement the ILogHandler interface.

Step 2: Replace the default unit with a 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 the reflection refers to the field by name, and if Unity decides to change it in the future, you will not receive a compilation error - an exception will be thrown at runtime.

0
source share

You can write tests in Unity by opening and running the unity tester, you can open it by going to the window and clicking "Tester". Then click on “Create test in edit mode” and write your tests to the created file.

https://docs.unity3d.com/Manual/testing-editortestsrunner.html

Thus, you do not need to omit the use of unity libraries.

0
source share

I know this is old, but I came across a way to unit test Unity assemblies from Visual Studio by simply switching the symbol definition in the Unity build settings. If everything is in order, when you can either perform tests or the tested components can be used in unity at the same time, you can switch the unit testing mode and unity mode as follows (images follow):

  1. Make your unity component a partial class. There is one file in which you declare that the partial class extends MonoBehaviour and puts MonoBehaviour everything that should actually use MonoBehaviour assemblies. This will not be verified by unit tests, but everything else will be.
  2. Use conditional compilation so that the contents of this file are compiled only when a specific character is defined during assembly. I used UNIT_TEST_NO_UNITY_INTEGRATION in my case.
  3. If you want to run unit tests from Visual Studio, update the assembly parameters to determine this symbol. This will eliminate Unity-specific things 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 delete this symbol definition. Now your unit tests will not be able to work, but your assemblies will work again in Unity.

enter image description here enter image description here

0
source share

All Articles