远程对象定义了函数object ReceiveMessage(ALARM message);
SendClient发送消息到Server转发到ReceiveClient;
这样通过ReceiveMessage自动接收到消息。在ReceiveClient中自定义了事件AlarmMsg,每次接收到消息触发OnAlarmMsg事件,但是为什么AlarmMsg都为null呢。是不是Remoting远程调用的委托不能在触发自定义事件啊?

解决方案 »

  1.   

    参考这篇文章吧。
    http://www.rainsts.net/article.asp?id=419
      

  2.   

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Runtime.Serialization.Formatters;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;namespace Learn.Library.Remoting
    {
      /// <summary>
      /// 委托类型
      /// </summary>
      public delegate void TestHandler();  /// <summary>
      /// 远程类型
      /// </summary>
      public class Data : MarshalByRefObject
      {
        public TestHandler OnTest;    public void Test()
        {
          Console.WriteLine("Test...(AppDomain:{0})", AppDomain.CurrentDomain.FriendlyName);
          if (OnTest != null) OnTest();
        }
      }  /// <summary>
      /// 客户端远程类型
      /// </summary>
      public class ClientData : MarshalByRefObject
      {
        public void OnTestMethod()
        {
          Console.WriteLine("Test...(AppDomain:{0})", AppDomain.CurrentDomain.FriendlyName);
        }
      }  public class RemotingTest2
      {
        /// <summary>
        /// 服务器端代码
        /// </summary>
        static void Server()
        {
          AppDomain server = AppDomain.CreateDomain("server");
          server.DoCallBack(delegate
          {
            BinaryClientFormatterSinkProvider cbin = new BinaryClientFormatterSinkProvider();
            BinaryServerFormatterSinkProvider sbin = new BinaryServerFormatterSinkProvider();
            sbin.TypeFilterLevel = TypeFilterLevel.Full;        Hashtable properties = new Hashtable();
            properties["port"] = 801;        TcpChannel channel = new TcpChannel(properties, cbin, sbin);
            ChannelServices.RegisterChannel(channel, false);        RemotingConfiguration.RegisterWellKnownServiceType(typeof(Data), "data", 
              WellKnownObjectMode.Singleton);
          });
        }    /// <summary>
        /// 客户端代码
        /// </summary>
        static void Client()
        {
          TcpChannel channel = new TcpChannel(802);
          ChannelServices.RegisterChannel(channel, false);
          RemotingConfiguration.RegisterWellKnownClientType(typeof(Data), "tcp://localhost:801/data");      Data data = new Data();
          data.OnTest += new ClientData().OnTestMethod;
          data.Test();
        }    static void Main()
        {
          Server();
          Client();
        }
      }
    }
      

  3.   

    在 Remoting 中使用 Event 主要是为了实现 CallBack 机制,让服务器在接收到某个 "消息" 时,主动调用某个或多个客户端的方法。
    这个我能够实现;
    服务器调用了注册的客户端,客户端事件ReceiveMessage( )触发了,我要让ReceiveMessage()在触发本地写的事件。可是老是***Event=null;