.net remoting应用程序建立-示例

本示例演示了:

1、如何将一类型的激活方式指定为服务器端激活
2、如何将一类型的激活方式指定为客户端激活
3、如何创建服务器端激活类型的实例
4、如何创建客户端激活类型的实例
5、如何以配置文件方式配置应用程序
6、如何以编程方式配置应用程序

一、以配置文件方式

1、服务器端

配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
    <application applicationName="remotingExample">
      <service>
         <!--ServerMathine为服务器端激活-->
         <wellknown mode="Singleton" type="MyRemoting.Type.ServerMathine,RemotingType" objectUri="ServerMachine"/>
         <!--ClientMathine为客户端激活-->
         <activated type="MyRemoting.Type.ClientMathine,RemotingType"/>
      </service>
      <channels>
        <channel port="8001" ref="http"/>
      </channels>
      <lifetime leaseTime="7M" sponsorshipTimeout="7M" renewOnCallTime="7M" />
    </application>
</system.runtime.remoting>
</configuration>

代码

using System.IO;
using System.Collections;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using MyRemoting.Type;

namespace RemotingServer
{
    class Program
    {
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile,false);
            Console.WriteLine("Server started!Press any key to exit...");
            Console.ReadLine();
            return;
        }
    }
}

2、客户端

配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
    <application>
      <client url="http://IP adress:8001">
        <activated type="MyRemoting.Type.ClientMathine,RemotingType"/>
        <wellknown type="MyRemoting.Type.ServerMathine,RemotingType" url=">
      </client>
      <channels>
        <channel port="0" ref="http"/>
      </channels>
    </application>
</system.runtime.remoting>
</configuration>

代码

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using MyRemoting.Type;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, false);
            
            ServerMathine Server = new ServerMathine();
            ClientMathine Client = new ClientMathine();

            Console.WriteLine("服务器端激活:" + Server.MathineName);
            Console.WriteLine("客户端激活:" + Client.MathineName);
            Console.ReadLine();
        }
    }
}

二、以编程方式

1、服务器端

using System;
using System.IO;
using System.Collections;
using System.Reflection;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using MyRemoting.Type;

namespace RemotingServer
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpChannel Tcp = new TcpChannel(8000);
            HttpChannel Http = new HttpChannel(8001);
            ChannelServices.RegisterChannel(Tcp, false);
            ChannelServices.RegisterChannel(Http, false);

            RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServerMathine), "ServerMathine", WellKnownObjectMode.Singleton);
            RemotingConfiguration.RegisterActivatedServiceType(typeof(ClientMathine));
            Console.WriteLine("Server started!Press any key to exit...");
            Console.ReadLine();
            return;
        }
    }
}

2、客户端

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Activation;
using MyRemoting.Type;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpChannel Tcp = new TcpChannel();
            HttpChannel Http = new HttpChannel();
            ChannelServices.RegisterChannel(Tcp, false);
            ChannelServices.RegisterChannel(Http, false);
            ServerMathine Server = (ServerMathine)Activator.GetObject(typeof(ServerMathine), "http://IP adress:8001/ServerMathine");
            object [] obj = {new UrlAttribute("tcp://IP adress:8000/")};
            ClientMathine Client = (ClientMathine)Activator.CreateInstance(typeof(ClientMathine), null,obj);

            Console.WriteLine("服务器端激活:" + Server.MathineName);
            Console.WriteLine("客户端激活:" + Client.MathineName);
            Console.ReadLine();
        }
    }
}

三、可远程处理的类(名称为RemotingType类库)

1、ServerMathine类

using System;
using System.Collections.Generic;
using System.Text;

namespace MyRemoting.Type
{
    public class ServerMathine : MarshalByRefObject
    {
        public ServerMathine()
        { 
            
        }

        public string MathineName
        {
            get 
            {
                return Environment.MachineName;
            }
        }

        public string MathineOS
        {
            get 
            {
                return Environment.OSVersion.Version.ToString();
            }
        }

        public string MathineDomain
        {
            get 
            {
                return Environment.UserDomainName;
            }
        }
    }
}

2、ClientMathine类

using System;
using System.Runtime.Remoting.Lifetime;

namespace MyRemoting.Type
{
    public class ClientMathine : MarshalByRefObject
    {
        public ClientMathine()
        {

        }

        public string MathineName
        {
            get
            {
                return Environment.MachineName;
            }
        }

        public string MathineOS
        {
            get
            {
                return Environment.OSVersion.Version.ToString();
            }
        }

        public string MathineDomain
        {
            get
            {
                return Environment.UserDomainName;
            }
        }
    }
}

转载于:https://www.cnblogs.com/inguiq/p/3520185.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值