惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
小众软件
小众软件
J
Java Code Geeks
I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
L
LangChain Blog
博客园 - 司徒正美
量子位
Y
Y Combinator Blog
C
Check Point Blog
T
Tailwind CSS Blog
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Fortinet All Blogs
云风的 BLOG
云风的 BLOG
A
About on SuperTechFans
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
V
V2EX
阮一峰的网络日志
阮一峰的网络日志

博客园 - 编程山人

品牌大全 控制只生成一个子窗体(简单) C#类中的get 和set 函数的具体用法 What's TM? 在C#中利用SharpZipLib进行文件的压缩和解压缩 在C#中运用SQLDMO备份和恢复Microsoft SQL Server数据库 WinForm实现SQLServer存储图片 通过C#调用CHM帮助文件 [Remoting专题系列] 一:.NET Remoting [Remoting专题系列] 二:远程对象 [Remoting专题系列] 三:激活模式 [Remoting专题系列] 四:生存期租约 [Remoting专题系列] 五:信道 [Remoting专题系列] 七:调用上下文 [Remoting专题系列] 八:元数据 [Remoting专题系列] 九:动态发布 [Remoting专题系列] 十:追踪服务 [Remoting专题系列] 十一:事件 [Remoting专题系列] 十二:配置文件
[Remoting专题系列] 六:异步调用
编程山人 · 2007-06-05 · via 博客园 - 编程山人

Remoting 的异步调用和单个应用程序域异步编程基本相同。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Security.Permissions;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.CompilerServices;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Lifetime;
using System.Runtime.Remoting.Services;

namespace Learn.Library.Remoting
{
  
public class RemotingTest2
  
{
    
public delegate int AddHandler(int a, int b);

    
/// <summary>
    
/// 远程类型
    
/// </summary>

    public class Data : MarshalByRefObject
    
{
      
public int Add(int a, int b)
      
{
        
return a + b;
      }

    }


    
/// <summary>
    
/// 服务器端代码
    
/// </summary>

    static void Server()
    
{
      AppDomain server 
= AppDomain.CreateDomain("server");
      server.DoCallBack(
delegate
      
{
        TcpServerChannel channel 
= new TcpServerChannel(801);
        ChannelServices.RegisterChannel(channel, 
false);

        RemotingConfiguration.ApplicationName 
= "test";
        RemotingConfiguration.RegisterActivatedServiceType(
typeof(Data));
      }
);
    }


    
/// <summary>
    
/// 客户端代码
    
/// </summary>

    static void Client()
    
{
      TcpClientChannel channel 
= new TcpClientChannel();
      ChannelServices.RegisterChannel(channel, 
false);
      RemotingConfiguration.RegisterActivatedClientType(
typeof(Data), "tcp://localhost:801/test");

      Data data 
= new Data();

      AddHandler add 
= new AddHandler(data.Add);
      IAsyncResult ar 
= add.BeginInvoke(12nullnull);
      ar.AsyncWaitHandle.WaitOne();
      Console.WriteLine(add.EndInvoke(ar));
    }


    
static void Main()
    
{
      Server();
      Client();
    }

  }

}


我们还可以为方法添加 OneWayAttribute 特性使其成为单向方法来完成类似的异步方法调用。

不过 [OneWary] 有一些条件限制:
1. 该方法无返回值和 out 或 ref 参数。 
2. 该方法不能引发任何异常

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Security.Permissions;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.CompilerServices;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Lifetime;
using System.Runtime.Remoting.Services;

namespace Learn.Library.Remoting
{
  
public class RemotingTest2
  
{
    
public delegate void TestHandler();

    
/// <summary>
    
/// 远程类型
    
/// </summary>

    public class Data : MarshalByRefObject
    
{
      [OneWay]
      
public void Test()
      
{
        Thread.Sleep(
5000);
        Console.WriteLine(
"Server:{0}", DateTime.Now);
      }

    }


    
/// <summary>
    
/// 服务器端代码
    
/// </summary>

    static void Server()
    
{
      AppDomain server 
= AppDomain.CreateDomain("server");
      server.DoCallBack(
delegate
      
{
        TcpServerChannel channel 
= new TcpServerChannel(801);
        ChannelServices.RegisterChannel(channel, 
false);

        RemotingConfiguration.ApplicationName 
= "test";
        RemotingConfiguration.RegisterActivatedServiceType(
typeof(Data));
      }
);
    }


    
/// <summary>
    
/// 客户端代码
    
/// </summary>

    static void Client()
    
{
      TcpClientChannel channel 
= new TcpClientChannel();
      ChannelServices.RegisterChannel(channel, 
false);
      RemotingConfiguration.RegisterActivatedClientType(
typeof(Data), "tcp://localhost:801/test");

      Data data 
= new Data();
      data.Test();
      Console.WriteLine(
"Client:{0}", DateTime.Now);
    }


    
static void Main()
    
{
      Server();
      Client();
    }

  }

}