我试图让我的客户订阅我服务器上发生的事件。
我有一个如下所示的界面:
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 Message =没有为此对象定义无参数构造函数。
源= mscorlib程序
答案 0 :(得分:0)
回答你的上一个问题:使用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; }
}
答案 1 :(得分:0)
我的钱在你的ConnectionManager
类没有默认/无参数构造函数。远程处理基础架构需要能够在服务器端创建它的实例。