科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网网络频道远程处理Remoting

远程处理Remoting

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

操作系统和运行库环境通常会在应用程序间提供某种形式的隔离。例如,Microsoft Windows 使用进程来隔离应用程序。

作者:51cto 来源:51cto 2009年7月21日

关键字: 网络管理 Remoting

  • 评论
  • 分享微博
  • 分享邮件

在本页阅读全文(共2页)

  编程式信道注册-客户端激活

  远程对象

  public class MyClass:MarshalByRefObject

  {

  public MyClass()

  {

  string con = "时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "远程对象构造函数被调用。";

  File.AppendAllText(@"F:/a.txt", con + "\r\n");

  }

  public void WriteFile(string content)

  {

  string con = "时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " 内容:" + content;

  File.AppendAllText(@"F:/a.txt", con+"\r\n");

  }

  }

  宿主

  using RemoteServer;

  using System.Runtime.Remoting.Channels;

  using System.Runtime.Remoting.Channels.Tcp;

  using System.Runtime.Remoting.Channels.Http;

  using System.Runtime.Remoting.Channels.Ipc;

  using System.Runtime.Remoting;

  private void ServerHostDialog_Load(object sender, EventArgs e)

  {

  IChannel tcpChannel = new TcpChannel(12345);

  ChannelServices.RegisterChannel(tcpChannel,false);

  IChannel httpChannel = new HttpChannel(45678);

  ChannelServices.RegisterChannel(httpChannel,false );

  IChannel ipcChannel = new IpcChannel("MyIPC");

  ChannelServices.RegisterChannel(ipcChannel,true );

  }

  private void button1_Click(object sender, EventArgs e)

  {

  Type serverType = typeof(MyClass);

  RemotingConfiguration.RegisterActivatedServiceType(serverType);

  }

  客户端

  using RemoteServer;

  using System.Runtime.Remoting;

  private void button1_Click(object sender, EventArgs e)

  {

  MyClass obj= new MyClass();

  obj.WriteFile("名柄为"+this.Handle .ToString ());

  }

  Type serverType = typeof(MyClass);

  private void Form1_Load(object sender, EventArgs e)

  {

  RemotingConfiguration.RegisterActivatedClientType(serverType, “tcp://localhost:12345”);//第二个参数可换成: http://localhost:45678或ipc://MyIPC

  }

  编程式信道注册-Single Call激活

  远程对象不变

  宿主

  using RemoteServer;

  using System.Runtime.Remoting.Channels;

  using System.Runtime.Remoting.Channels.Tcp;

  using System.Runtime.Remoting.Channels.Http;

  using System.Runtime.Remoting.Channels.Ipc;

  using System.Runtime.Remoting;

  private void ServerHostDialog_Load(object sender, EventArgs e)

  {

  IChannel tcpChannel = new TcpChannel(12345);

  ChannelServices.RegisterChannel(tcpChannel,false);

  IChannel httpChannel = new HttpChannel(45678);

  ChannelServices.RegisterChannel(httpChannel,false );

  IChannel ipcChannel = new IpcChannel("MyIPC");

  ChannelServices.RegisterChannel(ipcChannel,true );

  }

  private void button1_Click(object sender, EventArgs e)

  {

  Type serverType = typeof(MyClass); RemotingConfiguration.RegisterWellKnownServiceType(serverType,“RemoteServer”, WellKnownObjectMode.SingleCall);//RemoteServer为远程对象的名称空间

  }

  客户端

  using RemoteServer;

  using System.Runtime.Remoting;

  private void button1_Click(object sender, EventArgs e)

  {

  MyClass obj= new MyClass();

  obj.WriteFile("名柄为"+this.Handle .ToString ());

  }

  Type serverType = typeof(MyClass);

  private void Form1_Load(object sender, EventArgs e)

  {

  RemotingConfiguration.RegisterWellKnownClientType(serverType, "tcp://localhost:12345/RemoteServer");

  //第二个参数可换成: http://localhost:45678/RemoteServer或ipc://MyIPC/RemoteServer

  }

  编程式信道注册-Singleton激活

  远程对象不变

  宿主

  using RemoteServer;

  using System.Runtime.Remoting.Channels;

  using System.Runtime.Remoting.Channels.Tcp;

  using System.Runtime.Remoting.Channels.Http;

  using System.Runtime.Remoting.Channels.Ipc;

  using System.Runtime.Remoting;

  private void ServerHostDialog_Load(object sender, EventArgs e)

  {

  IChannel tcpChannel = new TcpChannel(12345);

  ChannelServices.RegisterChannel(tcpChannel,false);

  IChannel httpChannel = new HttpChannel(45678);

  ChannelServices.RegisterChannel(httpChannel,false );

  IChannel ipcChannel = new IpcChannel("MyIPC");

  ChannelServices.RegisterChannel(ipcChannel,true );

  }

  private void button1_Click(object sender, EventArgs e)

  {

  Type serverType = typeof(MyClass); RemotingConfiguration.RegisterWellKnownServiceType(serverType,“RemoteServer”, WellKnownObjectMode. Singleton);//RemoteServer为远程对象的名称空间

  }

  客户端

  using RemoteServer;

  using System.Runtime.Remoting;

  private void button1_Click(object sender, EventArgs e)

  {

  MyClass obj= new MyClass();

  obj.WriteFile("名柄为"+this.Handle .ToString ());

  }

  Type serverType = typeof(MyClass);

  private void Form1_Load(object sender, EventArgs e)

  {

  RemotingConfiguration.RegisterWellKnownClientType(serverType, "tcp://localhost:12345/RemoteServer");

  //第二个参数可换成: http://localhost:45678/RemoteServer或ipc://MyIPC/RemoteServer

  }

  管理式配置

  依照上面的例子,远程对象不变,只用在宿主端和客户端的项目中添加“应用程序配置文件”(App.config)文件。

  配置文件保存后的名称为:应用程序名.exe.config

  比如宿主的配置文件名为RemoteServerHost.exe.config

  客户商配置文件名为Client.exe.config

  在宿主中和客户端,读取配置文件

  在Main函数中

  using System.Runtime.Remoting;

  [STAThread]

  static void Main()

  {

  RemotingConfiguration.Configure(AppDomain.CurrentDomain.FriendlyName + ".config",false);

  Application.EnableVisualStyles();

  Application.SetCompatibleTextRenderingDefault(false);

  Application.Run(new Form1());

  }

  远程对象依然不变

  宿主端配置文件:

  

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<system.runtime.remoting >

<application>

<service>

<!--&lt;activated type="RemoteServer.MyClass,RemoteServer"></activated>--&gt;

<!--&lt;wellknown type="RemoteServer.MyClass,RemoteServer" mode="Singleton" objectUri="RemoteServer"></wellknown>--&gt;

<wellknown type="RemoteServer.MyClass,RemoteServer" mode="SingleCall" objectUri="RemoteServer"></wellknown>

</service>

<channels>

<channel ref="tcp" port="8005"></channel>

<channel ref="http" port="8006"></channel>

<channel ref="ipc" portName="MyIPC"></channel>

</channels>

</application>

</system.runtime.remoting>

</configuration>

客户端配置文件:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<system.runtime.remoting >

<application>

<!--&lt;client url="tcp://localhost:8005">--&gt;

<!--&lt;activated type="RemoteSrerver.MyClass,ServerAssembly"></activated>--&gt;

<!--&lt;/client>--&gt;

<client>

<wellknown type="RemoteServer.MyClass,RemoteServer" url="tcp://localhost:8005/RemoteServer"></wellknown>

</client>

</application>

</system.runtime.remoting>

</configuration>

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章