Link to home
Create AccountLog in
Avatar of adermon
adermon

asked on

.Net Remoting with Generic List Interface Problem: Object cannot be stored in an array of this type

Remoting neebie so please excuse is this is an obvious mistake
I have a remote object in a windows service that i can attach to and get properties (int string etc) to work OK in the client. I also have properties that return List<T> generic type but this throws an error when I try to access it from the client

"An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll"
"Object cannot be stored in an array of this type"

in the attached code snippet the code fails

Any ideas
Client Code
       private void ConnectDMProcessMonitor()
        {
            string strURL =
              String.Format(
                "tcp://{0}:{1}/ProcessMonitor.tcp",
                m_strRemotingServer,
                m_intRemotingPort);
 
            if (m_DSRemote == null)
            {
                m_DSRemote =
                  (IDSRemoteAccess)
                    Activator.GetObject(
                      typeof(IDSRemoteAccess),
                      strURL);
            }
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            ConnectDMProcessMonitor();
 
            string count = m_DSRemote.Test;
            MessageBox.Show(count);
 
                                         //Fails here
            foreach (IMonitorGroup mg in m_DSRemote.Groups)
            {
                lbx1.Items.Add(mg.Name.ToString());
                foreach (IMonitorProgram prog in mg.Programs)
                {
                    lbx1.Items.Add("\t" + prog.MachineName + 
                                    "\t" + prog.ProcessName + "\r\n" +
                                    "Failed = " + prog.Failed.ToString());
                }
            }
        }
 
Interface
   public interface IDSRemoteAccess
    {
        string Test
        {
            get;
        }
 
        List<IMonitorGroup> Groups
        {
            get;
        }
    }
 
    // Defines the interface for the MonitorProgram class
    public interface IMonitorProgram
    {
        bool Failed
        {
            get;
            set;
        }
 
        string MachineName
        {
            get;
            set;
        }
        string FullPath
        {
            get;
            set;
        }
        string ProcessName
        {
            get;
            set;
        }
 
        bool Running
        {
            get;
        }
        
        bool isRunning();
       
    }
 
    // defines the interface for the MonitorGroup class
    public interface IMonitorGroup
    {
        List<IMonitorProgram> Programs
        {
            get;
        }
        string Name
        {
            get;
            set;
        }
        bool Alert
        {
            get;
            set;
        }
        bool Healthy
        {
            get;
        }
        void CheckAllRunning();
    }
}

Open in new window

Avatar of Gururaj Badam
Gururaj Badam
Flag of India image

Try doing this

List<IMonitorGroup> monitorGroupList = m_DSRemote.Groups;

foreach (IMonitorGroup mg in monitorGroupList)

and see if you still see any exception.
Change it to:

object oTest = m_DSRemote.Groups;

break after that line in the debugger and look at what is in oTest.
ASKER CERTIFIED SOLUTION
Avatar of adermon
adermon

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account