4-23 https://www.cnblogs.com/zhengyun_ustc/archive/2006/06/09/remoting_InvalidCredentialException.html

今天看了一篇C# Remoting的一个简单例子,博主简单的介绍了remoting,目前所开发的系统也用到了remoting,特意重温了下系统,发觉其实也就那么回事情,服务器端定义远程对象,配置配置文件,客户端调用远程对象,其实也没有那么高大上(可能本人还未理解其中精髓)。

先展示代码,再说明这其中的机制(以C# Remoting的一个简单例子为基础)

1 创建RemoteSample项目,将其编译成lib文件。

在这个项目中创建三个接口(RemoteArrayInterface,RemoteStringInterface,RemoteCacuteInterface,这三个接口有不同的用途,分别处理字符串、数组、计算。

1
2
3
4
5
6
7
8
9
10
11
12
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace RemoteSample
{
  public  interface RemoteArrayInterface
    {
      int SumArray(int[] a);
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace RemoteSample
{
  public  interface RemoteCalcuteInterface
    {
         int sum(int a, int b);
    }
}

 

1
2
3
4
5
6
7
8
9
10
11
12
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace RemoteSample
{
  public  interface RemoteStringInterface
    {
     string CombineString(string str1, string str2);
    }
}

 2,  server端

1)创建一个远程类,继承MarshalByRefObject,实现上面的接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RemoteSample;
namespace RemoteSampleServer
{
    public class RemoteObject : MarshalByRefObject,RemoteArrayInterface,RemoteCalcuteInterface,RemoteStringInterface
    {
        public RemoteObject()
        {
            Console.WriteLine("New Reference Added");
        }
        public int sum(int a, int b)
        {
            return a + b;
        }
        public  int SumArray(int[] array)
        {
            int sum = 0;
            for (int i = 0; i < array.Length; i++)
            {
                sum += array[i];
            }
            return sum;
        }
      public string CombineString(string str1, string str2)
      {
          return  str1 + " is not equal " + str2;
      }
    }
}

  

2)server主体部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RemoteSample;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace RemoteSampleServer
{
    class RemoteServer
    {
        static void Main(string[] args)
        {
            try
            {
                TcpServerChannel tcpserverchannel = new TcpServerChannel(8888);
                ChannelServices.RegisterChannel(tcpserverchannel,false);
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "RemoteObject", WellKnownObjectMode.SingleCall);
                Console.WriteLine("press any key");
                Console.ReadKey();
            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString());
            }
 
        }
    }
}

 原博客中注册信道只用了一个参数,该方法已经废弃了,这里有两个参数,如果启用安群,第二个参数设置为true,否则设置为false,如果设置为false,将不会使在tcp或Ipc信道上所做的安全设置无效。

这里采用了服务器端激活(Wellknow),使用singlecall方式。

 3  客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RemoteSample;
namespace RemoteSampleClient
{
    class RemoteClient
    {
        static void Main(string[] args)
        {
            RemoteCalcuteInterface calcuteRemote = (RemoteSample.RemoteCalcuteInterface)Activator.GetObject(typeof(RemoteSample.RemoteCalcuteInterface), "tcp://localhost:8888/RemoteObject");
            Console.WriteLine("a +b = {0}", calcuteRemote.sum(1, 5).ToString());
            RemoteStringInterface stringRemote = (RemoteSample.RemoteStringInterface)Activator.GetObject(typeof(RemoteSample.RemoteStringInterface), "tcp://localhost:8888/RemoteObject");
            Console.WriteLine(" {0}", stringRemote.CombineString("wei xiao bao","kang xi"));
            int[] array = new int[4] { 1, 3, 4, 5 };
            RemoteArrayInterface arrayRemote = (RemoteSample.RemoteArrayInterface)Activator.GetObject(typeof(RemoteSample.RemoteArrayInterface), "tcp://localhost:8888/RemoteObject");
            Console.WriteLine("The sum is  {0}", arrayRemote.SumArray(array));      
            Console.ReadKey();
        }
    }
}

  原来客户端要注册信道,貌似不用注册信道,也能正常运行,不知道是啥原因。

个人理解:服务器端可以定义多种服务,将每种类型的服务设计成为借口,定义一个远程对象实现这些服务。客户端按需使用,要哪种类型的就通过activation获取。

这里很多信息在代码当中设置,在实际项目中,这样做肯定是不好的。目前我所接触的系统是通过配置文件设置,配置文件的好处的不需要修改代码,不需要编译。

一些概念性的知识,可以查阅百度百科。

C# Remoting例子的更多相关文章

  1. .net remoting(1)简单例子

    1.例子(程序间的通讯) class Program { static void Main(string[] args) { HttpChannel _channel = ); ChannelServ ...

  2. C# Remoting的一个简单例子

    .Net对于远程调用提供了两种方法:Remoting和WebService.WebService现在是如火如荼,特别是有一种比较流行的架构:Winform+WebService(Java..Net), ...

  3. 【DWR系列01】-DWR简介及入门例子

    .literal { background-color: #f2f2f2; border: 1px solid #cccccc; padding: 1px 3px 0; white-space: no ...

  4. .Net中Remoting通信机制简单实例

    .Net中Remoting通信机制 前言: 本程序例子实现一个简单的Remoting通信案例 本程序采用语言:c# 编译工具:vs2013工程文件 编译环境:.net 4.0 程序模块: Test测试 ...

  5. .Net中Remoting通信机制

    Remoting通信机制 Remoting介绍 主要元素 通道类型 激活方式 对象定义 Remoting介绍 什么是Remoting,简而言之,我们可以将其看作是一种分布式处理方式. 从微软的产品角度 ...

  6. WCF分布式开发必备知识(2):.Net Remoting

    .Net Remoting技术,我们可以将其看作是一种分布式处理方式.作为应用程序之间通信的一种机制,.Net Remoting与MSMQ消息队列不同,它不支持离线脱机消息,另外只适合.Net平台间程 ...

  7. .NET Remoting原理及应用实例:

    Remoting:(本文摘自百度百科) 简介:        什么是Remoting,简而言之,我们可以将其看作是一种分布式处理方 式.从微软的产品角度来看,可以说Remoting就是DCOM的一种升 ...

  8. .NET Remoting学习笔记(二)激活方式

    目录 .NET Remoting学习笔记(一)概念 .NET Remoting学习笔记(二)激活方式 .NET Remoting学习笔记(三)信道 参考:百度百科  ♂风车车.Net 激活方式概念 在 ...

  9. spring源码分析之spring-web remoting模块概况及基本概念

    spring-web总体分为三部分:caucho.httpinvoker.jaxws,其总体构造图如下: uml结构: 先看看网上搜索到的上述实现的原理吧:Spring RMI,Hessian/Bur ...

随机推荐

  1. 怎样写一个PC端使用的操盘手软件(用来买卖股票,查看报表,行情)

    我们想写一个操盘手软件,对于操盘而言,首先是快,然后是资料尽可能丰富,最好能看到行情,报表什么的.只是windows上写软件看似基础,实际上都不怎么好弄,用C++开发确实可以实现所有功能,估计光研发费 ...

  2. yii2的数据库读写分离配置

    简介 数据库读写分离是在网站遇到性能瓶颈的时候最先考虑优化的步骤,那么yii2是如何做数据库读写分离的呢?本节教程来给大家普及一下yii2的数据库读写分离配置. 两个服务器的数据同步是读写分离的前提条 ...

  3. 使用SMART监控Ubuntu

    参考:完全用 GNU/Linux 工作 - 29. 檢測硬碟 S.M.A.R.T. 健康狀態 1.安装 sudo apt-get install smartmontools 2.查看硬盘的参数,需要获 ...

  4. Non-zero exit code (1)

    刚报了这个错Non-zero exit code (1) 经排查执行这个命令就好了  python -m pip install --upgrade pip 一定要多看报错,报错中有提示的

  5. VS2017 配置QT5

    QT安装 1. QT下载 2. 安装过程中,组件的选择(图自https://blog.csdn.net/gaojixu/article/details/82185694) 3. 安装完成 VS2017 ...

  6. 解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)错误

    我调这个bug调了一天多,在网上搜索的检查namespace,package等,都没有错.错误提示是没有找到xml文件,我就纳闷了,为什么找不到呢?后来才发现,原来是resource中奇怪的目录为题, ...

  7. I - 昂贵的聘礼

    来源poj1062 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低要求 ...

  8. linux的基本操作(RPM包或者安装源码包)

    RPM包或者安装源码包 在windows下安装一个软件很轻松,只要双击.exe的文件,安装提示连续“下一步”即可,然而linux系统下安装一个软件似乎并不那么轻松了,因为我们不是在图形界面下.所以你要 ...

  9. 腾讯地图key秘钥

     腾讯地图key:5WNBZ-2JYR6-SPUSL-M3WGH-U4KDT-K2FYV 

  10. PHP版本MS17-010检测小脚本

    内网渗透的时候有点用处,可以检测MS17-010的漏洞并获取操作系统信息,配合BURP可批量检测,纯socket发包,无需其他扩展. <?php //根据巡风python代码翻译成PHP代码 / ...