實體層(DLL):
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Lifetime;

namespace EntityCommon
{
    public interface IEntity
    {
        string EntityName { get;set;}
        string EntityID { get;set;}
        void WriteSefInfo();
    }
    public interface IEntityManage
    {
         Entity GetEntity(string strID);

    }
    [Serializable]
    public class Entity :MarshalByRefObject, IEntity
    {
        public Entity()
        {
            Console.WriteLine("Entiry Active");
            EntityName = "E10000";
            EntityID = "Etext000";
        }
        string strName;
        string strID;
        #region IEntity 成員

       public string EntityName
        {
            get
            {
                return strName;
            }
            set
            {
                strName = value;
            }
        }

       public string EntityID
        {
            get
            {
                return strID;
            }
            set
            {
                strID = value;
            }
        }
        public void WriteSefInfo()
        {
            Console.WriteLine(this.EntityID+"==="+this.EntityName);
        }

        #endregion
        public override object InitializeLifetimeService()
        {
            return null;
        }


    }

Server(需引用上面的實體) :
加入一服務跟蹤對象:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;
using System.Runtime.Remoting.Services;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using EntityCommon;

namespace  TranckObject
{
    public class MyTracking : ITrackingHandler
    {
        public MyTracking()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        public void MarshaledObject(object obj, ObjRef or)
        {

            Console.WriteLine();
            Console.WriteLine("对象" + obj.ToString() + " is marshaled at " + DateTime.Now.ToShortTimeString());
        }

        public void UnmarshaledObject(object obj, ObjRef or)
        {
            Console.WriteLine();
            Console.WriteLine("象" + obj.ToString() + " is unmarshaled at " + DateTime.Now.ToShortTimeString());
        }

        public void DisconnectedObject(object obj)
        {
            Console.WriteLine();
            Console.WriteLine(obj.ToString() + " is disconnected at " + DateTime.Now.ToShortTimeString());
        }
    }
   
   
   
}

服務端注冊:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;
using System.Runtime.Remoting.Services;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Serialization.Formatters;

using System.Collections;
using EntityCommon;
using TranckObject;

namespace  Server

{
   public class class2
   {
        static void Main(string[] args)
        {
            IDictionary direct = new Hashtable();
            direct["name"] = "TCP8080";
            direct["port"] = "8080";
            BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
            BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
            serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
            HttpChannel channel = new HttpChannel(direct, clientProvider, serverProvider);

           LifetimeServices.LeaseTime = TimeSpan.Zero;
            TrackingServices.RegisterTrackingHandler(new TranckObject.MyTracking());
          
            EntityCommon.Entity entity = new EntityCommon.Entity();
            RemotingServices.Marshal(entity, "Entity");
           Console.WriteLine("Remoting服务启动,按退出…");
            Console.ReadLine();
            
         
        }
   }
}

客戶端(需引用上面的實全層DLL):

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;
using System.Runtime.Remoting.Services;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Collections;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Remoting.Activation;
using EntityCommon;

namespace Client
{
    class Client
    {
        static void Main(string[] args)
        {
            //RemotingConfiguration.Configure("Client.exe.config");

            IDictionary direct = new Hashtable();
            direct["name"] = "TCP8080";
            direct["port"] = "0";
            BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
            BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();

            serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
            HttpChannel channel = new HttpChannel(direct, clientProvider, serverProvider);
            ChannelServices.RegisterChannel(channel, false);            
           Entity EntityExp = (Entity)Activator.GetObject(typeof(EntityCommon.Entity), "http://localhost:8080/Entity");         
                
            EntityExp.EntityID = "E1001";
            EntityExp.EntityName = "TextName";
            EntityExp.WriteSefInfo();                  

        }
     }
   
}


posted on 2006-10-09 15:29  一刀  阅读(266)  评论(0)    收藏  举报