我对.Net Remoting 是从2002年就已经知道这个说法了。但是一直很害怕这个怪物。因为每次想试图去搞清楚它的时候,
一看他的那么多页的文档,我是看不下去了.因为他总是先告诉你很多恐怖的概念,中间夹杂一些"断章取义"的代码,从来就
没有一个完整的代码. 所以总是这样虎头蛇尾的.估计大家也有很多人是这样子的. 今天我不跟大家说概念了.  .Net Remoting 整个框架分成三部分:
1. 服务器端
  需要引用2
  代码如下:  
  
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
namespace HelloServer
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Server
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            HttpServerChannel tcpChannel = new HttpServerChannel(5256);
            ChannelServices.RegisterChannel(tcpChannel);
            //if you set WellKnownObjectMode.SingleCall,when client call, server side will create a new object even if there is a old object.
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello.Hello),"Shark",WellKnownObjectMode.SingleCall);
            System.Console.WriteLine("Hit to exit");
            System.Console.ReadLine();
        }
    }
    
}
 
2. 具体实现服务的类库
  代码如下:
 
using System;
using System.Data;
namespace Hello
{
    
    public class Hello:System.MarshalByRefObject
    {
        public Hello()
        {
            Console.WriteLine("Constructor");
        }
        ~Hello()
        {
            Console.WriteLine("Destructor");
        }
        public string Greeting(string name)
        {
            Console.WriteLine("Greeting");
            return "Hello," + name;
        }
        public DataTable GetData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ID",typeof(String)));
            DataRow rs = dt.NewRow();
            rs["ID"] = "1";
            dt.Rows.Add(rs);
            rs = dt.NewRow();
            rs["ID"] = "2";
            dt.Rows.Add(rs);
            rs = dt.NewRow();
            rs["ID"] = "3";
            dt.Rows.Add(rs);
            return dt;
        }
        
    }
}
2. 客户端
  需要引用2
  代码如下:
  
private void button1_Click(object sender, System.EventArgs e)
        {
            //You can use the following method to create remoting object
            //Method 1:Hello.Hello obj = (Hello.Hello)Activator.GetObject(typeof(Hello.Hello),"http://127.0.0.1:5256/Shark");
            //Method 2:Hello.Hello obj = (Hello.Hello)RemotingServices.Connect(typeof(Hello.Hello),"http://127.0.0.1:5256/Shark");
            Method 3: 
            
            Method 4: 
            if(obj != null)
            {
                this.dataGrid1.DataSource = obj.GetData();
                this.Text = obj.Greeting(System.DateTime.Now.ToString());
                ILease lease = (ILease)obj.GetLifetimeService();
            }
        }
        private void Form1_Load(object sender, System.EventArgs e)
        {
            ChannelServices.RegisterChannel(new HttpClientChannel());
        }
整个例子的完整代码,请下载https://files.cnblogs.com/SharkXu/DotNetRemoting.zip.
                    
                


                
            
        
浙公网安备 33010602011771号