设置回调是可以的。另外你如果想要通知其他所有client的话,最好使用observe模式。
给你一点sample:private void btnStart_Click(object sender, System.EventArgs e)
{
//Connecting to Server
if(_channel == null)
{
// 定义provider,如果需要加入callback,TypeFilterLevel就一定要是Full
BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
// creating the IDictionary to set the port on the channel instance.
IDictionary props = new Hashtable();
// Create a channel at port number
// 当server调用client的时候一定要为client定义channel
props["port"] = 0;
// pass the props for the port setting and the server provider in the
// server chain argument. (Client remains null here.)
this._channel = new TcpChannel(props, null, provider);    
}
try
{
// Register the channel
ChannelServices.RegisterChannel(_channel);
}
catch
{
_channel = null;
WriteLine("RegisterChannel Failed!!!");
return;
}try 
{
_RO = (RemoteService)Activator.GetObject(typeof(RemoteService), textUrl.Text);
_RO.myEvent+= new MyDelegate(myFunc);
//_RO.join(_observerRO);
WriteLine("Start ok!");
//throw new NullReferenceException();
}
catch(System.Exception me) 
{
if(_channel != null)
{
ChannelServices.UnregisterChannel(_channel);
}
_RO = null;
_channel = null;
string msg = "Server not available";
WriteLine(msg);
return;
}
}

解决方案 »

  1.   

    我试了很多方法,包括你的程序代码,但是错误统一都是加载事件的时候,如:_RO.myEvent+= new MyDelegate(myFunc);说不能反序列化,而我的事件在定义的时候都从MarshalByRefObject继承了,还是不行。
    我把c#高级编程的里的远程事件例子代码敲进去试了一下,错误也是不能反序列化。
    请教如何解决那?
      

  2.   

    反序列化我已经解决了这个问题,provider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
    我把这些代码写进配置文件里了。
    但是不能在客户端注册一个tcpserverchannel进行侦听了,会提示已经注册过tcp信道了
    这样我的事件每次都是一个客户端调用remote的事件,事件只返回到这个客户端调用本地的处理事件,其他客户端没有反应。
    请问如何解决呀。
      

  3.   

    你可以在你的服务端注册一个实例,用这个实例来操作服务端的属性等,如下:
    YouClass c=new YouClass();
    c.CtrForm=this;(或者任一个你要操作的窗体)
    RemotingServices.Marshal(c,"Messenger");
    然后在你的YouClass类里就可以对这个this的各个属性操作了.
    当然这个实例是在客户端来用的.
      

  4.   

    to lakeineye(湖泊仙女):
    你确定保证了这个channel是0吗?我这里都可以,客户端要是0才能多客户同时注册。
    可能是remoting里面把0作为自动设置得channel吧,具体原因我也不确定。
    我这里这样做是可以的,你在测试一下
      

  5.   

    我用这段代码实现了局域网内的给多个client发送事件
    Delegate[] invocationlist = status.GetInvocationList();
    foreach (Delegate del in invocationlist)
     {
       object[]  args = {this,e};
       del.Method.Invoke(del.Target,args);
    }但是GetInvocationList是多路广播,出局域网可能就不行了。请问你们上面说得方法可以在公网上实现嘛?
      

  6.   

    Delegate[] invocationlist = status.GetInvocationList();
    foreach (Delegate del in invocationlist)
     {
       object[]  args = {this,e};
       del.Method.Invoke(del.Target,args);
    }
    这段代码实在公共common类里写的。我的解决方案里有三个项目:client,common,server
    我希望共用的类都在common里,尽量避免在服务器写公共类。上面几位说得方法都是放在server里写的嘛?能实现公网上的事件触发多个client嘛?
      

  7.   

    发现我这个方法只能在局域网里用,在公网上时不行的
    有谁知道怎样在公网上实现服务器事件调用多个client
      

  8.   

    to lakeineye(湖泊仙女):
    我没有尝试在外网的情况下的问题。你可以尝试调整一下channel类型,tcp/http/udp(udp不知道有没有)。另外你没有必要用delegate 的一个list来实现啊,用
    event yourDelegate myEvent;
    myEvent += instanceOfYourDelegate;
    来实现会更好。
      

  9.   

    这个我已经解决了在公网上服务器事件触发多个client
    但是由于client注册事件后,如果客户端忽然由于其他原因突然死掉,重起客户端程序,发现这个客户端事件就不能正常工作了.
    我猜是因为重起后又重新注册事件,而以前注册的事件和客户端没有在服务器断注销的原因
    不知道这种事情如何解决?
      

  10.   

    发现事件问题还是很多,如果服务器一方或者客户端一方忽然死掉,重新启动.另一方事件不能启动了.
    最后没有办法,只好反向控制信息用socket做,然后根据反向控制信息再客户端调用服务器相应的方法.此方法虽然笨,但是可以捕获服务器死掉或者客户端忽然死掉.如果谁能解决remote事件的这个问题,可以写写自己的代码
    以上的ggdd们,不知道有没有jjmm们,给了我很多启发和帮助,也深学了一把remote
    谢谢大家了!