热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

.netremoting应用程序建立示例

本示例演示了:1、如何将一类型的激活方式指定为服务器端激活2、如何将一类型的激活方式指定为客户端激活3、如何创建服务器端激活类型的实例4、如何创建客户端激活类型的实例

本示例演示了:

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

一、以配置文件方式

1、服务器端

配置文件




   
     
        
        
        
        
     

     
       
     

     
   


代码

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、客户端

配置文件




   
     
       
       
     

     
       
     

   


代码

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



推荐阅读
author-avatar
Martha829
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有