NET Remoting 最简单示例

版权声明:本文为博主原创文章,未经博主允许不得转载。
学习技术知识一个好的方法是先动手,再深入,
给出一个最简单的Remoting程序示例(C#)如下:
Step1:创建类库(DLL)工程RemotingObjects,类Person代码如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace RemotingObjects
- {
- public interface IPerson
- {
- String getName(String name);
- }
- public class Person : MarshalByRefObject, IPerson
- {
- public Person()
- {
- Console.WriteLine("[Person]:Remoting Object 'Person' is activated.");
- }
- public String getName(String name)
- {
- return name;
- }
- }
- }
Step2:创建控制台工程RemotingServer(添加项目引用RemotingObjects),类Server代码如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Remoting;
- using System.Runtime.Remoting.Channels;
- using System.Runtime.Remoting.Channels.Tcp;
- using System.Text;
- using System.Threading.Tasks;
- namespace RemotingServer
- {
- class Server
- {
- static void Main(string[] args)
- {
- TcpChannel channel = new TcpChannel(8080);
- ChannelServices.RegisterChannel(channel, false);
- RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingObjects.Person), "RemotingPersonService", WellKnownObjectMode.SingleCall);
- System.Console.WriteLine("Server:Press Enter key to exit");
- System.Console.ReadLine();
- }
- }
- }
Step3:创建控制台工程RemotingClient(添加项目引用RemotingObjects及必要类库),类Client代码如下:
(PS:正式应用开发,不需要也不应该直接引用RemotingObjects类库,而应该引用相关Remoting类的接口库。)
- using RemotingObjects;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.Remoting.Channels;
- using System.Runtime.Remoting.Channels.Tcp;
- using System.Text;
- using System.Threading.Tasks;
- namespace RemotingClient
- {
- class Client
- {
- static void Main(string[] args)
- {
- TcpChannel channel = new TcpChannel();
- ChannelServices.RegisterChannel(channel, false);
- IPerson obj = (IPerson)Activator.GetObject(typeof(RemotingObjects.IPerson), "tcp://localhost:8080/RemotingPersonService");
- if (obj == null)
- {
- Console.WriteLine("Couldn't crate Remoting Object 'Person'.");
- }
- Console.WriteLine("Please enter your name:");
- String name = Console.ReadLine();
- try
- {
- Console.WriteLine(obj.getName(name));
- }
- catch (System.Net.Sockets.SocketException e) {
- Console.WriteLine(e.ToString());
- }
- Console.ReadLine();
- }
- }
- }
Step4:运行编译出的EXE:RemotingServer.exe和RemotingClient.exe,查看运行结果。
NET Remoting 最简单示例的更多相关文章
- Linux下的C Socket编程 -- server端的简单示例
Linux下的C Socket编程(三) server端的简单示例 经过前面的client端的学习,我们已经知道了如何创建socket,所以接下来就是去绑定他到具体的一个端口上面去. 绑定socket ...
- C# 构建XML(简单示例)
C# 构建XML的简单示例: var pars = new Dictionary<string, string> { {"url","https://www. ...
- 根据juery CSS点击一个标签弹出一个遮罩层的简单示例
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- ACEXML解析XML文件——简单示例程序
掌握了ACMXML库解析XML文件的方法后,下面来实现一个比较完整的程序. 定义基本结构 xml文件格式如下 <?xml version="1.0"?> <roo ...
- demo工程的清单文件及activity中api代码简单示例
第一步注册一个账户,并创建一个应用.获取app ID与 app Key. 第二步下载sdk 第三步新建工程,修改清单文件,导入相关的sdk文件及调用相应的api搞定. 3.1 修改清单文件,主要是加入 ...
- spring-servlet.xml简单示例
spring-servlet.xml简单示例 某个项目中的spring-servlet.xml 记下来以后研究用 <!-- springMVC简单配置 --> <?xml versi ...
- SignalR 简单示例
一.什么是 SignalR ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of add ...
- Web API 简单示例
一.RESTful和Web API Representational State Transfer (REST) is a software architecture style consisting ...
- XML引入多scheme文件约束简单示例
XML引入多scheme文件约束简单示例,用company.xsd和department.xsd来约束company.xml: company.xsd <?xml version="1 ...
随机推荐
- openssl将私钥和crt证书合成pfx证书
pfx是什么文件:公钥加密技术12号标准(Public Key Cryptography Standards #12,PKCS#12)为存储和传输用户或服务器私钥.公钥和证书指定了一个可移植的格式.它 ...
- 自动更新GeoIP数据库
#!/bin/bash if [ ! -d /usr/local/share/GeoIP ];then mkdir /usr/local/share/GeoIP fi if [ ! -d /usr/l ...
- Pipeline模式(netty源码死磕6)
精进篇:netty源码死磕6 巧夺天工--Pipeline模式揭秘 1. 巧夺天工--Pipeline模式揭秘 1.1. Pipeline模式简介 管道的发名者叫,Malcolm Douglas M ...
- [note]树链剖分
树链剖分https://www.luogu.org/problemnew/show/P3384 概念 树链剖分,是一种将树剖分成多条不相交的链的算法,并通过其他的数据结构来维护这些链上的信息. 最简单 ...
- [转】IIS:Do not nest virtual directories
原文:https://msdn.microsoft.com/en-us/library/ms178685.aspx#Anchor_6 Configuration settings for virtua ...
- 关于scrollLeft的获取在不同浏览器或相同浏览器的不同版本下的获取
chrome61向w3c规则靠拢,document.body.scrollLeft获取的值一直为0,需要使用document.documentElement.scrollLeft(或document. ...
- Effective java -- 8 异常
第五十七条:只针对异常的情况才使用异常应该都有这个意识吧,就像什么抓索引越界什么的,没有必要. 第五十八条:对可恢复情况使用受检查异常,对编程错误使用运行时异常三种可抛的异常:受检的异常(checke ...
- curl常用指令
curl 发送GET请求获取标准输出 curl -I 显示http请求头 curl -i 显示请求头及输出内容 curl xxx > xxx 将输出重定向到本地文件(本地文件无需已存在,一般不写 ...
- Spring中如何动态注入Bean实例教程
前言 在Spring中提供了非常多的方式注入实例,但是由于在初始化顺序的不同,基于标注的注入方式,容易出现未被正确注入成功的情况. 本文将介绍一种在实际项目中基于动态的方式来提取Spring管理的Be ...
- IE Firefox css 差别
1.单位问题 问题:任何距离的数值ie可以不加单位,ff必须要求写单位(0除外) 解决:写全单位如padding:0px; 2.水平居中 问题:div里的内容,ie默认为center,而ff默认lef ...