一个 Remoting 例子

本文介绍如何使用.NET实现远程处理,使不同应用域间直接通信。通过创建可远程处理的对象、配置监听及客户端应用程序域,实现跨域调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

若要使用 .NET 远程处理生成让两个组件在其中跨越应用程序域边界直接通信的应用程序,只需生成以下对象:
·可远程处理的对象。

using System;

namespace Remotable
{
    
    
public class RemotableType : MarshalByRefObject
    
{
        
private string _internalString = "This is the RemotableType.";
        
public string StringMethod()
        
{
            
return _internalString;
        }

    }


}

 
·侦听对该对象的请求的宿主应用程序域。

using System;
using System.Runtime.Remoting;


namespace RemotingFirst
{

    
public class Listener
    
{
        
public static void Main()
        
{
            RemotingConfiguration.Configure(
"Listener.exe.config");
            Console.WriteLine(
"Listening for requests. Press Enter to exit");
            Console.ReadLine();
        }

    }


}

 

Listener.exe.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    
<system.runtime.remoting>
      
<application>
         
<service>
            
<wellknown 
               
mode="Singleton" 
               type
="Remotable.RemotableType, RemotableType" 
               objectUri
="RemotableType.rem"
            
/>
         
</service>
         
<channels>
            
<channel ref="http" port="8989"/>
         
</channels>
      
</application>
    
</system.runtime.remoting>
</configuration>


·对该对象发出请求的客户端应用程序域。

using System;
using System.Runtime.Remoting;


namespace Client
{


    
public class Client
    
{

        
public static void Main()
        
{
            RemotingConfiguration.Configure(
"Client.exe.config");
            Remotable.RemotableType remoteObject 
= new Remotable.RemotableType();
            Console.WriteLine(remoteObject.StringMethod());
        }

    }


}

Client.exe.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
      
<application>
         
<client>
            
<wellknown 
               
type="Remotable.RemotableType, RemotableType"
               url
="http://localhost:8989/RemotableType.rem"
            
/>
         
</client>
      
</application>
   
</system.runtime.remoting>
</configuration>

运行
1、拷贝文件:
Listener 文件夹:Listener.exe、RemotableType.dll、Listener.exe.config。
Client 文件夹:Client.exe、RemotableType.dll、Client.exe.config。
2、在 Listener 文件夹运行:Listener。
3、在 Client 文件夹 运行:Client;
     输出:This is the RemotableType.

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值