parameter less constructor error
Asked Answered
I

2

0

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

Imperturbation answered 17/4, 2014 at 13:49 Comment(6)
Does the exception have an inner exception?Exhort
It's failing to load the main assembly or one of its dependencies?Imperturbation
Then you probably are missing a referenced assembly. Did you only copy the exe?Exhort
After fixing the reference, im getting a no parameter less constructor error?Imperturbation
You provided a nice stack trace, but not the actual exception...Altaf
@BenVoigt The stack trace has been replaced by the exception.Imperturbation
E
0

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; }
}
Exhort answered 17/4, 2014 at 14:24 Comment(7)
thanks for your response, after rebuilding its still throwing the same error though.Imperturbation
Are you sure there are no other places with this problem? Maybe the Exception will tell the failing class?Exhort
The only reference to [Serializable] is in the class from above. Thanks for your patience !Imperturbation
And what about your Client class. It is referenced in the EventArgs.Exhort
I've changed that object to type string, do I need to add anything else if ive done that?Imperturbation
Not as far as I can see. Give it a try.Exhort
let us continue this discussion in chatImperturbation
S
0

My money is on your ConnectionManager class not having a default / parameterless constructor. The remoting infrastructure needs to be able to create an instance of it on the server end.

Sensate answered 17/4, 2014 at 14:53 Comment(1)
Thanks for this, it compiles, the event never fires but at least it compiles!Imperturbation

© 2022 - 2024 — McMap. All rights reserved.