Search code examples
c#eventsinterface.net-remoting

parameter less constructor error


I'm trying to make my client subscribe to events that happen on my server.

I have an interface that looks like this:

public delegate void RemoteEventHandler(object sender, ClientEventArgs args);


[Serializable]
public class ClientEventArgs : EventArgs
{
    public ClientEventArgs()
    { }

    public ClientEventArgs(Client _client)
    {
        MyClient = _client;
    }
    public Client MyClient { get; set; }
}

public interface IMonitor
{
    event RemoteEventHandler RemoteEvent;
}

My Server Class looks like this:

public class ConnectionManager : MarshalByRefObject, IMonitor
{
    public event RemoteEventHandler RemoteEvent;

    // call the below code when th event should fire.
     if (RemoteEvent != null)
            RemoteEvent(this, new ClientEventArgs(e.MyClient));
}

Then To set my channels up on the server I do this:

BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel = TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["port"] = 5001;
TcpChannel channel = new TcpChannel(props, null, provider);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(
typeof(ConnectionManager),
ConnectionManager",
WellKnownObjectMode.Singleton);

And On the client to set the channels up and subscribe to the event:

TcpChannel channel = new TcpChannel();
        ChannelServices.RegisterChannel(channel, false);
        _monitorObject = (IMonitor)Activator.GetObject(
            typeof(IMonitor),
            "tcp://localhost:5001/ConnectionManager");

_monitorObject.RemoteEvent += _monitorObject_RemoteEvent;

Can anyone explain where this is going wrong please?

Exception:

System.MissingMethodException was unhandled HResult=-2146233069 Message=No parameterless constructor defined for this object.
Source=mscorlib


Solution

  • To answer your last question: when using Serializable you need a constructor without parameters. So this one would definitely fail:

    [Serializable]
    public class ClientEventArgs : EventArgs
    {
        public ClientEventArgs(Client _client)
        {
            MyClient = _client;
        }
        public Client MyClient { get; set; }
    }
    

    You need to add a parameterless constructor:

    [Serializable]
    public class ClientEventArgs : EventArgs
    {
        public ClientEventArgs()
        { }
    
        public ClientEventArgs(Client _client)
        {
            MyClient = _client;
        }
        public Client MyClient { get; set; }
    }