日期:2014-05-20  浏览次数:21054 次

.NET Remoting问题,如何测试
我在学习C#高级编程.NET Remoting的时候,书中一开始有个例子:
①创建一个程序集:RemoteHello.dll,以下为代码:
using System;

namespace Wrox.ProCSharp.Remoting
{
    public class Hello: System.MarshalByRefObject
    {
        public Hello()
        {
            Console.WriteLine("构造函数启动了!");
        }

        ~Hello()
        {
            Console.WriteLine("析构函数启动了!");
        }

        public string Greeting(string name)
        {
            Console.WriteLine("问候启动了!");
            return "你好," + name;
        }
    }
}


②建立简单的服务器:HelloServer.exe控制台应用程序,引用上面的RemoteHello.dll程序集,以下为代码:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace Wrox.ProCSharp.Remoting
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpServerChannel channel = new TcpServerChannel(8086);
            ChannelServices.RegisterChannel(channel, false);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello), "Hi", WellKnownObjectMode.SingleCall);

            System.Console.WriteLine("press return to exit");
            System.Console.ReadLine();
        }
    }
}

③建立简单的客户机HelloClient.exe控制台应用程序,引用上面的RemoteHello.dll程序集,以下为代码:
using System;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace Wrox.ProCSharp.Remoting
{
    class Program
    {
        static void Main(string[] args)
        {
            ChannelServices.RegisterChannel(new TcpClientChannel(), false);

            Hello obj = (Hello)Activator.GetObject(typeof(Hello), "tcp://localhost:8086/Hi");

            if (obj == null)
            {
                Console.WriteLine("could not locate server");
                return;