.Net对于远程调用提供了两种方法:Remoting和WebService。
WebService现在是如火如荼,特别是有一种比较流行的架构:Winform+WebService(Java、.Net),
我曾经做过的一个项目就是这样子的,分布式、跨平台、极佳的用户体验,这三者结合起来是不是很诱人?
不过,这里我只说Remoting,Remoting具有以下特点:
1、Tcp通道的Remoting速度非常快
2、虽然是远程的,但是非常接近于本地调用对象
3、可以做到保持对象的状态
4、没有应用程序限制,可以是控制台,winform,iis,windows服务承载远程对象
缺点:
1、不是标准的应用,因此有平台限制
2、脱离iis的话需要有自己的安全机制
可以看出来,比起WebService,Remoting更适合于中小型局域网应用,而不适用于企业级的应用。
下面给出一个极其简单的Sample:
Remoting用的对象:

namespace RemoteSample
 2{
 3    public class RemoteObject : System.MarshalByRefObject
 4    {
 5        public RemoteObject()
 6        {
 7            System.Console.WriteLine("New Referance Added!");
 8        }
 9
10        public int sum(int a, int b)
11        {
12            return a + b;
13        }
14    }
15}

将其编译为一个lib文件:csc /t:library RemoteObject.cs

Server端:

 1using System;
 2using System.Runtime;
 3using System.Runtime.Remoting;
 4using System.Runtime.Remoting.Channels;
 5using System.Runtime.Remoting.Channels.Tcp;
 6using RemoteSample;
 7
 8namespace RemoteSampleServer
 9{
10    public class RemoteServer
11    {
12        public static void Main(String[] args)
13        {
14             TcpServerChannel channel = new TcpServerChannel(6666);
15             ChannelServices.RegisterChannel(channel);
16             RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject),
17                 "RemoteObject", WellKnownObjectMode.SingleCall);
18             System.Console.WriteLine("Press Any Key");
19             System.Console.ReadLine();
20         }
21    }
22}

将其编译为一个exe文件:csc /r:System.Runtime.Remoting.dll /r:RemoteObject.dll RemoteServer.cs

Client端:

 1using System;
 2using System.Runtime.Remoting;
 3using System.Runtime.Remoting.Channels;
 4using System.Runtime.Remoting.Channels.Tcp;
 5using RemoteSample;
 6
 7namespace RemoteSampleClient
 8{
 9    public class RemoteClient
10    {
11        public static void Main(string[] args)
12        {
13            ChannelServices.RegisterChannel(new TcpClientChannel());
14            RemoteObject remoteobj = (RemoteObject)Activator.GetObject(typeof(RemoteObject),
15            "tcp://localhost:6666/RemoteObject");
16            Console.WriteLine("1 + 2 = " + remoteobj.sum(1,2).ToString());
17            Console.ReadLine();
18        }
19    }
20}

同样的,将其编译为exe文件:csc /r:System.Runtime.Remoting.dll /r:RemoteObject.dll RemoteClient.cs

好了,一次运行生成的RemoteServer.exe和RemoteClient.exe,你就会发现原来Remoting是这样简单。

C# Remoting的一个简单例子的更多相关文章

  1. 一个简单例子:贫血模型or领域模型

    转:一个简单例子:贫血模型or领域模型 贫血模型 我们首先用贫血模型来实现.所谓贫血模型就是模型对象之间存在完整的关联(可能存在多余的关联),但是对象除了get和set方外外几乎就没有其它的方法,整个 ...

  2. (转)Java中使用正则表达式的一个简单例子及常用正则分享

    转自:http://www.jb51.net/article/67724.htm 这篇文章主要介绍了Java中使用正则表达式的一个简单例子及常用正则分享,本文用一个验证Email的例子讲解JAVA中如 ...

  3. C语言多线程的一个简单例子

    多线程的一个简单例子: #include <stdio.h> #include <stdlib.h> #include <string.h> #include &l ...

  4. quartz---的一个简单例子

    quartz---的一个简单例子 首先建立一个maven项目.jar工程即可.(提示:我前面有如何建立一个maven工程的总结以及maven环境的配置.) 1.建立好后点击到app中运行,--> ...

  5. 用socket.io实现websocket的一个简单例子

    socket.io 是基于 webSocket 构建的跨浏览器的实时应用. 逛博客发现几个比较好的 一.用socket.io实现websocket的一个简单例子 http://biyeah.iteye ...

  6. 词法分析程序 LEX和VC6整合使用的一个简单例子

    词法分析的理论知识不少,包括了正规式.正规文法.它们之间的转换以及确定的有穷自动机和不确定的有穷自动机等等... 要自己写一个词法分析器也不会很难,只要给出了最简的有穷自动机,就能很方便实现了,用if ...

  7. spring集合类型的setter注入的一个简单例子

    在项目中我们有时候会为集合类型设定一些默认的值,使用spring后,我们可以通过配置文件的配置,用setter方式为对象的集合属性提供一些默认值,下面就是一个简单的例子. 首先我们创建了一个名为Col ...

  8. netsh interface portproxy的一个简单例子

    netsh interface portproxy的微软帮助文档地址: https://technet.microsoft.com/zh-cn/library/cc776297(WS.10).aspx ...

  9. js 排列 组合 的一个简单例子

    最近工作项目需要用到js排列组合,于是就写了一个简单的demo. 前几天在网上找到一个写全排列A(n,n)的code感觉还可以,于是贴出来了, 排列的实现方式: 全排列主要用到的是递归和数组的插入 比 ...

随机推荐

  1. Clover(资源管理器增强)

    Clover(资源管理器增强) 下载地址:http://www.orsoon.com/Soft/13157.html 功能: Windows Explorer 资源管理器的一个扩展,为其增加类似谷歌  ...

  2. HDOJ 1524 A Chess Game

    A Chess Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  3. Win10走红背后,最开心的人却是谷歌

    导读 微软在不惜余力推进Windows10普及的同时,也有一些让自己小小郁闷的事儿发生,在Win10系统当中,微软用新的Edge浏览器取代了用户熟悉的IE浏览器,以求改写在浏览器市场上的被动局面,不过 ...

  4. Redis使用介绍

    Redis 是一个高性能的key-value数据库. redis的出现,很大程度补偿了memcached这类keyvalue存储的不足,在部 分场合可以对关系数据库起到很好的补充作用.它提供了Pyth ...

  5. Fast Power

    Calculate the a^n % b where a, b and n are all 32bit integers. Example For 2^31 % 3 = 2 For 100^1000 ...

  6. 【leetcode】Palindrome Partitioning II

    Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...

  7. 【leetcode】4Sum

    4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d  ...

  8. Java动态加载类在功能模块开发中的作用

    Java中我们一般会使用new关键字实例化对象然后调用该对象所属类提供的方法来实现相应的功能,比如我们现在有个主类叫Web类这个类中能实现各种方法,比如用户注册.发送邮件等功能,代码如下: /* * ...

  9. Java for LeetCode 169 Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  10. 【JAVA、C++】LeetCode 022 Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...