remoting客户端激活之硬编码

remoting的三元素:远程对象,宿主程序(服务器程序),客户端程序;

远程对象代码:

using System;

namespace RemoteObject

{

   public class MyObject:MarshalByRefObject

   {

       public string SayHello()

        {

         Console.WriteLine("Hello EveryOne");

        return "HelloEveryOne";

        }

      public void ShowDomain()

      {

        Console.WriteLine(AppDomain.CurrentDomain.FriendlyName);

      }

   }

}

远程对象的激活方式分为客户端激活类型的和服务器端激活的两种;

 1、客户端激活:不管是对象的创建还是对象方法的执行都是在服务器端,服务器为每个客户创建一个对象实例,并且在租约时间到期并且垃圾回收器发挥作用之前对象将一直处于激活状态,客户端可以从远程获取方法的返回值;

         1.1 硬编码(在配置文件中注册将要被激活的远程对象)

              服务器端配置文件:

 

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

<configuration>
<system.runtime.remoting>
<application>

<service>

<activated type="RemoteObject.MyObject,RemoteObject"/>

</service>

<channels>

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

</channels>

</application>

</system.runtime.remoting>

</configuration>

 

 

            服务器端代码:

 

using System;

using System.Runtime.Remoting;

namespace MyServer>/p>

{ class Program

  {

    static void Main(string[] args)

   {

RemotingConfiguration.Configure("MyServer.exe.config");

Console.ReadLine();

    }

   }

}

 

           客户端配置文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<client url="tcp://localhost:9090">
<activated type="RemoteObject.MyObject,RemoteObject"/>
</client>
<channels>
<channel ref="tcp" port="0"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>

      客户端代码:

using System;
using System.Runtime.Remoting;
using RemoteObject;
namespace MyClient
{
class Program
{
static void Main(string[] args)
{
RemotingConfiguration.Configure("MyClient.exe.config");
MyObject m = new MyObject();
m.ShowDomain();
Console .WriteLine ( m.SayHello());
Console.ReadLine();
}
}
}

转载于:https://www.cnblogs.com/LittleFeiHu/archive/2010/10/25/little_man.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值