项目1:生成永不过期的远程对象(共享组件 share.dll)
namespace share
{
public  class shared:System.MarshalByRefObject
{
public int count=0;
public shared()
{
Console.WriteLine("shared 实例已创建");          }
public override object InitializeLifetimeService()
{
return null;//设为永不过期
}
}
}项目2:服务器(控制台程序),注册Singleton类型的远程对象share.shared,并且生成其实例share1
using share;
namespace CDL
{
public class Sample 
{
public static void Main()
{
System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType(typeof(share.shared),"cdl",WellKnownObjectMode.Singleton);
HttpServerChannel channel1=new HttpServerChannel(80);
ChannelServices.RegisterChannel(channel1);
Console.ReadLine();
share.shared share1=new shared();
share1.count++;
Console.WriteLine("引用计数:"+share1.count); Console.ReadLine();
}
} // End class
}项目3:客户端(控制台程序),调用服务器中的远程对象的实例
using share;
namespace Client1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
HttpClientChannel channel=new HttpClientChannel();
ChannelServices.RegisterChannel(channel);
RemotingConfiguration.RegisterWellKnownClientType(typeof(share.shared),"http://localhost:80/cdl");
share.shared share2=new shared();
share2.count++;
Console.WriteLine("引用计数:"+share2.count); Console.ReadLine();
}
}
}1、先运行服务器程序,服务器控制台输出:shared 实例已创建
                                       引用计数:1
2、运行客户端程序(客户端1),客户端控制台输出:引用计数:1 ,服务器控制台又输出:shared 实例已创建
3、再运行一个客户端程序(客户端2),客户端控制台输出:引用计数:2  ,服务器控制台无输出问题:客户端1和客户端2的运行结果说明两者引用的是同一个shared远程对象的实例,却不是服务器程序中创建的shared远程对象的实例share1,说明在服务器中生成了shared远程对象的两个实例,如何才能让客户端1和2引用到服务器程序生成的实例share1?