Remote建立分析

Remote作为一个远程调用对象技术,它有着.WebService没有的优势,首先它在一个企业内部系统使用的话,TCP通常就更好地发挥它的作用,当然它不跨平台这是一个不少的遗憾,下面是我对Remote的一点了解,有错误的地方大家指出来..

Remote通常分三步建立

1: RemoteObject(通常是类库文件)

2: RemoteServer(真正开发项目的时间建议用WINDOWS服务,调试的时候可以用控制台程序)

3: RemoteClient(也就是实现RemoteObject所定义的方法)

 

RemoteObject

():定義對象接口(: interface ISenLin)

1:定義屬性

2:定義方法

3:其他

:

():定義工廠接口(interface IMyFactory)作用:創建對象接口實例

1:定義返回接口實例的方法

2:其他

RemoteServer

():定義對象類並繼承MarshalByRefObject和實現對象接口

: public class SenLin:MarshalByRefObject,ISenLin

():定義工廠類並繼承MarshalByRefObject和實現工廠接口

(:通常上面两个是在RemoteObject中实现,只是当初为了表示更好的理解而在这里实现

)

: public class ServerFactory:MarshalByRefObject,IMyFactory

():注冊通道

: TcpChannel chan = new TcpChannel(7777);

    HttpChannel chan2 = new HttpChannel(8888);

():注冊對象(即工廠類ServerFactory)

: RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServerFactory), "senlin", WellKnownObjectMode.SingleCall);

RemoteClient

():注冊通道

: TcpChannel chan = new TcpChannel();

    HttpChannel chan2 = new HttpChannel();

(): 客戶端激活

1: 激活創建工廠接口對象

2: 通過工廠對象接口的實例方法返回對象接口實例

:IMyFactory imyf = (IMyFactory)Activator.GetObject(typeof(IMyFactory), "tcp://localhost:7777/longkc");

 ISenLin isen = imyf.GetSenLin();

(): 通過對象接口的實例可以實現對象接口所定義的方法和屬性等

總結:

1OBJECT中定義工廠接口和對象接口

2:SERVER中定義工廠類實現工廠接口,定義對象類實理對象接口, 注冊對象(即工廠類)

3:CLIENT 中激活工廠接口,創建對象接口, 用對象接口實現定義的屬性和方法

完整代碼

RemoteObject

ISenLin.cs

using System;

using System.Collections.Generic;

using System.Text;

namespace RemoteObject

{

    public interface ISenLin

    {

        string Depart

        { get; set;}

        string Position

        { get; set;}

        string Address

       { get; set;}

        string Name

       { get; set;}

        string GetDeatailAddress();

    }

}

IMyService.cs

using System;

using System.Collections.Generic;

using System.Text;

namespace RemoteObject

{

    public interface IMyService

    {

        int Age

         { get; set;}

        string Name

         { get; set;}

        string Address

         { get; set;}

 

        string GetMyName();

        string GetMyAddress();

       

    }

}

IMyFactory.cs

using System;

using System.Collections.Generic;

using System.Text;

namespace RemoteObject

{

    public interface IMyFactory

    {

        IMyService GetsService();

        ISenLin GetSenLin();

    }

}

RemoteServer

ServerFactory.cs

using System;

using System.Collections.Generic;

using System.Text;

using RemoteObject;

namespace RemoteServer

{

    public class ServerFactory:MarshalByRefObject,IMyFactory

    {

        #region IMyFactory Members

        public IMyService GetsService()

        {

            return new ServerImpl();

        }

        public ISenLin GetSenLin()

        {

            return new SenLin();

        }

 

        #endregion

    }  

}

SenLin.cs

using System;

using System.Collections.Generic;

using System.Text;

using RemoteObject;

namespace RemoteServer

{

    public class SenLin:MarshalByRefObject,ISenLin

    {

        private string depart;

        private string name;

        private string address;

        private string position;

        #region ISenLin Members

 

        public string Address

        {

            get

            {

                return address;

            }

            set

            {

                address = value;

            }

        }

 

        public string Depart

        {

            get

            {

                return depart;

            }

            set

            {

                depart = value;

            }

        }

       

        public string Name

        {

            get

            {

                return name;

            }

            set

            {

                name = value;

            }

        }

 

        public string Position

        {

            get

            {

                return position;

            }

            set

            {

                position = value;

            }

        }

 

        public string GetDeatailAddress()

        {

            return "招聘職位:" + Position + "   詳細地址: " + Address + Name;

        }

        #endregion

    }

}

ServerImpl.cs

using System;

using System.Collections.Generic;

using System.Text;

using RemoteObject;

namespace RemoteServer

{

public class ServerImpl:MarshalByRefObject,IMyService

    {

        private int age;

        private string name;

        private string address;

        #region IMyService Members

        public string Address

        {

            get

            {

                return address;

            }

            set

            {

                address = value;

            }

        }

        public int Age

        {

            get

            {

                return age;

            }

            set

            {

                age = value;

            }

        }

 

        public string GetMyAddress()

        {

            return "我的地址是: " + Address;

        }

 

        public string GetMyName()

        {

            return "我的名字叫: " + Name;

        }

 

        public string Name

        {

            get

            {

                return name;

            }

            set

            {

                name = value;

            }

        }

        #endregion

    }

}

Program.cs控制台程序

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Http;

using System.Runtime.Remoting.Channels.Tcp;

using RemoteObject;

namespace RemoteServer

{

    class Program

    {       

        static void Main(string[] args)

        {

            TcpChannel chan = new TcpChannel(7777);

            HttpChannel chan2 = new HttpChannel(8888);

            ChannelServices.RegisterChannel(chan, false);

            ChannelServices.RegisterChannel(chan2, false);

        

            RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServerFactory), "longkc", WellKnownObjectMode.SingleCall);

            RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServerFactory), "senlin", WellKnownObjectMode.SingleCall);         

            System.Console.WriteLine("Press Any Key to Exit ! ");

            System.Console.ReadLine();

        }

    }

}

RemoteClient

Program.cs 控制台程序

using System;

using System.Collections.Generic;

using System.Text;

using System.Configuration;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Channels;

using System.Runtime.Remoting.Channels.Http;

using System.Runtime.Remoting.Channels.Tcp;

using System.Runtime.Remoting.Activation;

using RemoteObject;

namespace RemoteClient

{

    class Program

    {      

        static void Main(string[] args)

        {           

            //注冊通道

            TcpChannel chan = new TcpChannel();

            HttpChannel chan2 = new HttpChannel();

            ChannelServices.RegisterChannel(chan, false);

            ChannelServices.RegisterChannel(chan2, false);

            //仿客戶端端激活方式

           //激活創建工廠接口對象

   IMyFactory imyf = (IMyFactory)Activator.GetObject(typeof(IMyFactory), "tcp://localhost:7777/longkc");

           

//通過工廠對象接口的實例方法返回對象接口實例

IMyService imy = imyf.GetsService();

            ISenLin isen = imyf.GetSenLin();

            //客戶端激活方式

            //object[] attrs = { new UrlAttribute("http://localhost:9999/Pur") };

            //object[] param = { };

            //PurClass pur = (PurClass)Activator.CreateInstance(typeof(PurClass), null, attrs);

            //imy.Name = "LONGKAGNCAI";

            if (imy == null)

                Console.WriteLine("無法定位服務器!");

            else

            {

                serverObj.Name = "國營農場";

                imy.Age = 27;

                imy.Name = "李小明";

                imy.Address = "東莞市";

                isen.Address = "東莞市";

                isen.Depart = "電腦部";

                isen.Position = "軟件工程師";

                isen.Name = "東莞市紡織有限公司";                           

                Console.WriteLine(imy.GetMyName());

                Console.WriteLine(imy.Age);

                Console.WriteLine(imy.GetMyAddress());

                Console.WriteLine(isen.GetDeatailAddress());

          }          

            Console.WriteLine();          

            Console.ReadLine();

     }       

    }

}

原文地址:https://www.cnblogs.com/jacklong/p/511092.html