[Remoting] 六:异步调用

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(1, 2, null, null);
      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();
    }
  }
}

输出:
Client:2007-2-26 15:00:57
Server:2007-2-26 15:01:02  
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值