本示例演示了:
1、如何将一类型的激活方式指定为服务器端激活
2、如何将一类型的激活方式指定为客户端激活
3、如何创建服务器端激活类型的实例
4、如何创建客户端激活类型的实例
5、如何以配置文件方式配置应用程序
6、如何以编程方式配置应用程序
一、以配置文件方式
1、服务器端
配置文件
代码
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、客户端
配置文件
代码
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;
}
}
}
}