How can I resolve System.MethodAccessException: Attempt to access a private/protected method failed. in Unity WebPlayer Deployment?


I have the following run-time problem only with the webplayer in deployment.
I am using Unity 3.5.5f3 on Windows 7 64bit with IE 9.0.8112.16421(32-bit)(both dev & deployment environments are this environment).
I have also tested deployment in Firefox/Palemoon 3.6.3 (32-bit Mozilla based) with the same results.

I attempt the deployment via the File–>Build Settings console window.
When running as a PC and Mac Standalone and as a Web Player in the IDE my app works ok.
It is only on deployment (via the Web Player Build and Run) I have this issue.

I have found a similar issue here, but it is not exactly the same & its contents did not help me:

I have a series of C# files included in my Unity project based on the RabbitMQ 2.8.7 Client. A newer version (3.0.0) can be found here:

Again, everything works fine in the PC and Mac Standalone and as a Web Player in the IDE. My Start() method sets up the ConnectionFactory parameters, then tests the prefetch and attempts to create a connection shown below:

//////////////START CODE///////////////////////////////////////

	void Start () {
		ConnectionFactory cf = new ConnectionFactory();
		cf.UserName = "admin";
		cf.Password = "admin";
		cf.VirtualHost = "/carbon";
		cf.Protocol = Protocols.FromEnvironment();
		cf.HostName = "";
		cf.Port = 5672;
		//cf.Uri = serverAddress;

		bool isOpen = Security.PrefetchSocketPolicy(sHostName, iPort, 500);
		if (isOpen) 
			sPrefetchMsg = "Prefetch on " + sHostName + ":" + iPort.ToString() + " success!";
			sPrefetchMsg = "Prefetch on " + sHostName + ":" + iPort.ToString() + " failed.";
		conn = cf.CreateConnection();}
		catch (Exception ex){
			sEMsg = ex.ToString();
		ch = conn.CreateModel();
		string queueName = ensureQueue(ch);
		sub = new Subscription(ch, queueName);
		messageThread = new Thread(new ThreadStart(receiveMessages));

//////////////END CODE////////////////////////////////////////

In the WebPlayer deployment environment where things do not work, the prefetch still works, but the resulting error I print to the screen as is as follows:

//////////////BEGIN RUN-TIME OUTPUT//////////////////////////////////
None of the specified endpoints were reachable Endpoints attempted:
endpoint=amqp-0-9://, attempts=1
System.MethodAccessException: Attempt to access a private/protected method failed.
at System.Security.SecurityManager.ThrowException (System Exception ex)[0x00000] in <filename unknown>:0
at ManagedCodeSubscriber.SocketFrameHandler_0_9..ctor (ManagedCodeSubscriber.AmqpTcpEndpoint endpoint)[0x00000] in <filename unknown>:0
at ManagedCodeSubscriber.Framing.Impl.v0_9.ProtocolBase.CreateFrameHandler (ManagedCodeSubscriber.AmqpTcpEndpoint endpoint)[0x00000] in <filename unknown>:0...
Stack trace:
at ManagedCodeSubscriber.ConnectionFactory.CreateConnection(Int32 maxRedirects)[0x00000] in <filename unknown>:0
at ManagedCodeSubscriber.ConnectionFactory.CreateConnection()[0x00000] in <filename unknown>:0
at Subscriber.Start()[0x00000] in <filename unknown>:0
//////////////END RUN-TIME OUTPUT//////////////////////////////////

Any help in resolving this WebPlayer specific issue or suggested work-arounds is much appreciated!


Edit: You CANNOT open your own sockets when running within the web player due to the security sandbox

I would recommend finding and using a javascript MQ client on the web page wrapper for your player and access that through external calls in Unity. Or set up an MQ server with an http client and then you can interact with it from inside Unity using the WWW class.


I find a “solution” : in your file SocketFrameHandler_0_9, line ~120 (constructor), you can remove Buffered Stream :

 //BufferedStream stream = new BufferedStream(netstream);
 // m_reader = new NetworkBinaryReader(stream);
 m_reader = new NetworkBinaryReader(netstream);
//m_writer = new NetworkBinaryWriter(new BufferedStream(netstream));
m_writer = new NetworkBinaryWriter(netstream);

it seems an error to do that but it’s not a problem, BufferedStream is strange in this case, it don’t increase performance so I removed it :slight_smile: and my app works fine. (I’m working in Online team from a big videogames company and I debated from it with colleagues).