.net remoting应用程序建立-示例
2014-01-15 09:26
465 查看
本示例演示了:
1、如何将一类型的激活方式指定为服务器端激活
2、如何将一类型的激活方式指定为客户端激活
3、如何创建服务器端激活类型的实例
4、如何创建客户端激活类型的实例
5、如何以配置文件方式配置应用程序
6、如何以编程方式配置应用程序
一、以配置文件方式
1、服务器端
配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application applicationName="remotingExample">
<service>
<!--ServerMathine为服务器端激活-->
<wellknown mode="Singleton" type="MyRemoting.Type.ServerMathine,RemotingType" objectUri="ServerMachine"/>
<!--ClientMathine为客户端激活-->
<activated type="MyRemoting.Type.ClientMathine,RemotingType"/>
</service>
<channels>
<channel port="8001" ref="http"/>
</channels>
<lifetime leaseTime="7M" sponsorshipTimeout="7M" renewOnCallTime="7M" />
</application>
</system.runtime.remoting>
</configuration>
代码
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、客户端
配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<client url="http://IP adress:8001">
<activated type="MyRemoting.Type.ClientMathine,RemotingType"/>
<wellknown type="MyRemoting.Type.ServerMathine,RemotingType" url=">
</client>
<channels>
<channel port="0" ref="http"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
代码
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;
}
}
}
}
1、如何将一类型的激活方式指定为服务器端激活
2、如何将一类型的激活方式指定为客户端激活
3、如何创建服务器端激活类型的实例
4、如何创建客户端激活类型的实例
5、如何以配置文件方式配置应用程序
6、如何以编程方式配置应用程序
一、以配置文件方式
1、服务器端
配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application applicationName="remotingExample">
<service>
<!--ServerMathine为服务器端激活-->
<wellknown mode="Singleton" type="MyRemoting.Type.ServerMathine,RemotingType" objectUri="ServerMachine"/>
<!--ClientMathine为客户端激活-->
<activated type="MyRemoting.Type.ClientMathine,RemotingType"/>
</service>
<channels>
<channel port="8001" ref="http"/>
</channels>
<lifetime leaseTime="7M" sponsorshipTimeout="7M" renewOnCallTime="7M" />
</application>
</system.runtime.remoting>
</configuration>
代码
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、客户端
配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<client url="http://IP adress:8001">
<activated type="MyRemoting.Type.ClientMathine,RemotingType"/>
<wellknown type="MyRemoting.Type.ServerMathine,RemotingType" url=">
</client>
<channels>
<channel port="0" ref="http"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
代码
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;
}
}
}
}
相关文章推荐
- 使用.NET Remoting 建立分布式应用程序
- 今日学习 (使用.NET Remoting 建立分布式应用程序(一))(4月6日)
- 今日学习 (使用.NET Remoting 建立分布式应用程序(二))(4月7日) ——对象生存周期管理
- 使用.NET Remoting 建立分布式应用程序(视频课程讲师:任旻)
- .net 简单图表控件 (介绍测试示例使用部分) [b/s应用程序控件] I
- .Net Remoting 技术示例
- .NET Remoting 进阶之二——应用程序域例程
- .Net与Java的互操作(.NET StockTrader微软官方示例应用程序)
- 翻译:改进.NET应用程序的性能和可伸缩性(三)-Remoting性能
- .Net与Java的互操作(.NET StockTrader微软官方示例应用程序)
- 翻译:改进.NET应用程序的性能和可伸缩性(三)-Remoting性能
- .net 简单图表控件 (介绍测试示例使用部分) [c/s桌面应用程序控件] II
- .NET Remoting应用程序
- .net remoting小结(二)-简单示例
- 关于.Net Remoting 的一个简单示例
- 今日学习 (使用.NET Remoting 建立分布式应用程序(三))(4月8日) ——Remoting服务发布方式
- .Net/C#控制台应用程序实现磁盘目录文件搜索的工具类 【本示例简体转繁体】
- ZZ:使用.NET语言开发Silverlight应用程序入门(一):了解项目结构
- 在 Intranet 环境中保护 .NET Web 应用程序
- .Net环境下使用基于Completion Port的网络通信模块示例(源)