【问题标题】:parameter less constructor error无参数构造函数错误
【发布时间】:2014-04-17 13:49:24
【问题描述】:

我正在尝试让我的客户端订阅发生在我的服务器上的事件。

我的界面如下所示:

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;
}

我的服务器类如下所示:

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));
}

然后要在服务器上设置我的频道,我这样做:

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);

并在客户端设置频道并订阅事件:

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

_monitorObject.RemoteEvent += _monitorObject_RemoteEvent;

谁能解释一下这是哪里出了问题?

例外:

System.MissingMethodException 未处理 HResult=-2146233069 消息=没有为此对象定义无参数构造函数。
来源=mscorlib

【问题讨论】:

  • 异常是否有内部异常?
  • 加载主程序集或其依赖项之一失败?
  • 那么您可能缺少引用的程序集。你只复制exe吗?
  • 修复引用后,我得到一个无参数少构造函数错误?
  • 你提供了一个很好的堆栈跟踪,但不是实际的异常......

标签: c# events interface .net-remoting


【解决方案1】:

回答您的最后一个问题:使用Serializable 时,您需要一个不带参数的构造函数。所以这个肯定会失败:

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

你需要添加一个无参数的构造函数:

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

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

【讨论】:

  • 感谢您的回复,但重建后仍然会抛出相同的错误。
  • 你确定没有其他地方有这个问题吗?也许异常会告诉失败的班级?
  • 对 [Serializable] 的唯一引用是在上面的类中。感谢您的耐心等待!
  • 那么您的Client 课程呢。它在EventArgs 中被引用。
  • 我已将该对象更改为字符串类型,如果我这样做了,是否需要添加其他内容?
【解决方案2】:

我的钱花在你的 ConnectionManager 类上,没有默认/无参数构造函数。远程基础设施需要能够在服务器端创建它的实例。

【讨论】:

  • 感谢,它编译,事件永远不会触发,但至少它编译!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-14
  • 1970-01-01
  • 2014-07-06
  • 2012-03-25
  • 1970-01-01
  • 2014-03-09
相关资源
最近更新 更多