[Remoting] 六:异步调用(收藏转帖http://www.rainsts.net/article.asp?id=413)
2008-03-26 14:13
441 查看
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();
}
}
}
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();
}
}
}
相关文章推荐
- [Remoting] 七:调用上下文(收藏转帖http://www.rainsts.net/article.asp?id=414)
- [Remoting] 四:生存期租约(收藏转帖http://www.rainsts.net/article.asp?id=411)
- NET Remoting一,二(收藏转帖http://www.rainsts.net/article.asp?id=408)
- [Remoting] 十二:配置文件(收藏转帖http://www.rainsts.net/article.asp?id=420)
- [Remoting] 五:信道(收藏转帖http://www.rainsts.net/article.asp?id=412)
- [Remoting] 三:激活模式(收藏转帖http://www.rainsts.net/article.asp?id=410)
- [Remoting] 八:元数据(收藏转帖http://www.rainsts.net/article.asp?id=415)
- [Remoting] 九:动态发布(收藏转帖http://www.rainsts.net/article.asp?id=416)
- [Remoting] 十:追踪服务(收藏转帖http://www.rainsts.net/article.asp?id=417)
- [Remoting] 十一:事件(收藏转帖http://www.rainsts.net/article.asp?id=419)
- 动态调用 WebService (收藏转贴http://www.rainsts.net/article.asp?id=304)
- Message Queuing(收藏转帖http://www.rainsts.net/article.asp?id=418)
- 从雨痕老大的博客上转来的 http://www.rainsts.net/article.asp?id=1066 读书人偷书不算窃 :)
- [ASP.NET 优化] IIS6 Gziphttp://www.rainsts.net/article.asp?id=531
- 使用C#调用外部序或是执行DOS命令http://www.yaosansi.com/blog/article.asp?id=810
- winsock学习笔记(一)(http://www.vccode.net/article_view.asp?id=737)
- asp.net控件开发基础 来源:http://www.clingingboy.com/blog/article.asp?id=258
- Asp.NET 2.0学习资源(reference from http://www.eyeya.com/article.asp?id=197)
- ASP.NET 2.0 URL映射技巧http://onewww.net/blog/article.asp?id=83
- winsock学习笔记(二)(http://www.vccode.net/article_view.asp?id=738)