惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Vulnerabilities – Threatpost
P
Proofpoint News Feed
The Hacker News
The Hacker News
Know Your Adversary
Know Your Adversary
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tenable Blog
AWS News Blog
AWS News Blog
S
Securelist
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
IT之家
IT之家
腾讯CDC
WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
C
Check Point Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Engineering at Meta
Engineering at Meta
Latest news
Latest news
A
About on SuperTechFans
The Register - Security
The Register - Security
L
LINUX DO - 热门话题
T
The Exploit Database - CXSecurity.com
C
Cisco Blogs
T
Tailwind CSS Blog
Simon Willison's Weblog
Simon Willison's Weblog
阮一峰的网络日志
阮一峰的网络日志
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
T
Tor Project blog
L
Lohrmann on Cybersecurity
G
GRAHAM CLULEY
B
Blog RSS Feed
Scott Helme
Scott Helme
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
NISL@THU
NISL@THU
P
Privacy International News Feed
Security Latest
Security Latest
Recorded Future
Recorded Future
L
LangChain Blog
Cyberwarzone
Cyberwarzone
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
Apple Machine Learning Research
Apple Machine Learning Research
F
Fortinet All Blogs
O
OpenAI News
T
Threat Research - Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale

博客园 - 龍的傳人

再谈代码生成器,拥有自己最适合的代码生成器 javascript+css+xml 全选树(再续) 设表格细钱 javascript+css+xml 全选树(续) javascript+css+xml 全选树 白话CMMI 如何做好需求收集[来之《程序员》第2期] javascript、CSS、XML动太生成树菜单 - 龍的傳人 - 博客园 可选择也可填写的下拉框(用鼠标\键盘的上下键选择) - 龍的傳人 - 博客园 兼容firefox和IE的两级联动下拉菜单的javascript代码 HTML 图片分析 JavaScript - Import XML Document firefox与IE对javascript和CSS的区别(转) IE和Firefox在JavaScript方面的兼容性(转) javascript在中ie与firefox的区别与解决方案(转) 针对Firefox兼容性,要注意的一些问题 (转) sql2000和文本文件的写入和读取(转) [转]JS代码格式化工具(附源代码) ASP.NET2.0调用MySql的存储过程
Remote建立分析
龍的傳人 · 2006-09-21 · via 博客园 - 龍的傳人

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();

     }       

    }

}