您的位置:首页 > 其它

.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;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: