locked
Deserializing a stream..... brings to "Permission denied: cannot call non-public or static methods remotely."

    Question

  • Hello to everybody,
    I'll try to explain my problem before showing you the exeption I receive. Initially I had an application that reads data from a DB and elaborates the information. Each layer of the application was on the same machine the application runs. Now I've been asked to separate the Data Access Layer and to put it into a Windows Service that will run on the DataBase platform.

    And here the troubles start!

    In the main application I init the DBHelper in that way :

    Code Block

       public void InitNetRemotingClient(string type)
            {
                // Setup the configuration parameters through a dictionary
                try
                {
                    // Create an instance of the remote object
                    myDBHelper = (DBHelper)Activator.GetObject(
                        typeof(DBHelper),
                        "tcp://localhost:8080/DBHelperSVC");

                  //  myDBHelper = new DBHelper(idCup, idSeason, idMatchCode);
                }
                catch (Exception ex)
                {
                    //TODO: Add Logging
                    throw (ex);
                }

            }


    Here's the code inside the DBHelper.cs that generates me troubles ....


    Code Block

             public Stream GetFirstFrameForMatch()
            {
                using (SqlConnection sqlConn = GetSqlConnection("TRK"))
                {
                    int frameValue = -1;

                    string strStore = "usp_GetFirstFrameByMatch";
                    SqlCommand cmd = new SqlCommand(strStore, sqlConn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@CupId", IdCup);
                    cmd.Parameters.AddWithValue("@Season", IdSeason);
                    cmd.Parameters.AddWithValue("@MatchCode", IdMatchCode);

                    MemoryStream ms = new MemoryStream();
                    try
                    {
                        frameValue = (int)cmd.ExecuteScalar();
                        myBinaryFormatter.Serialize(ms, frameValue);
                    }
                    catch (Exception ex)
                    {
                        frameValue = -1;
                        //TODO : Add a log event
                        throw;
                    }
                    finally
                    {
                        cmd.Dispose();
                    }

                    return ms;
                }
            }


    ... when I call if from the ProxyDataAccess (That stays in the main application):

    Code Block

      public int GetFirstFrameForMatch()
            {
                int value = 0;
                Stream s = myDBHelper.GetFirstFrameForMatch();
               
                if (s != null && s.CanSeek | s.CanRead)
                {
                    s.Seek(0, 0);

                    try
                    {
                        value = (int)myBinaryFormatter.Deserialize(s);   
                    }
                    catch(Exception ex)
                    {
                        int i = 0;
                    }
                   
                }


    I get an error on value = (int)myBinaryFormatter.Deserialize(s); , the error is "Permission denied: cannot call non-public or static methods remotely." and here's the exception stack trace :

    System.Runtime.Remoting.RemotingException occurred
      Message="Permission denied: cannot call non-public or static methods remotely."
      Source="mscorlib"
      StackTrace:
        Server stack trace:
           at System.Runtime.Remoting.Channels.ChannelServices.DispatchMessage(IServerChannelSinkStack sinkStack, IMessage msg, IMessage& replyMsg)
        Exception rethrown at [0]:
           at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
           at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
           at System.IO.MemoryStream.InternalReadInt32()
           at System.IO.BinaryReader.ReadInt32()
           at System.Runtime.Serialization.Formatters.Binary.BinaryObjectWithMapTyped.Read(__BinaryParser input)
           at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
           at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
           at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
           at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
           at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
           at ETSPlus.PO.ProxyDataAccess.GetFirstFrameForMatch() in C:\Projects\ETSPlus\ETSPlus.PO\ProxyDataAccess.cs:line 138

    Any suggestion?
    Thanks in advance

    Paolo

    Thursday, January 17, 2008 9:53 AM

Answers

  • Paolo,

    Is your code using some static or non public member? Static methods and fields accessed via a remoting proxy, are actually executed locally on the client side. So even though a type is configured to go remote static accesses/invokations dont go remote. If you need to access static data on the server side, you need to wrap the static access with instance methods / properties or fields. For non public, you will need to make them public. Please inspect your code and see if this helps.

     

    Friday, January 18, 2008 6:31 PM
    Moderator