有人见过吗?在看《C#高级编程》这本书中介绍的,一字不漏的抄进去居然有异常。不知道这个错在哪里?引发异常代码:
Hello obj = (Hello)Activator.GetObject(typeof(Hello),"tcp://localhost:8016/Hi");

解决方案 »

  1.   

    你的服务器端还没有注册这个类或服务器端时注册的类指向的类路径不正确,这个都是刚开始学Remoting都会遇到问题,这个要小心多研究才行,因为服务器那里就算配置有问题也不会报错,而是客户端连上去就出错,所以会第一时间认为是客户端的问题而没有去想到是服务端的问题向命运发出战书,誓将命运踩在脚下!!
      

  2.   

    检查了几遍,没发现什么。还是帖代码吧:////////////////////////  公共库端代码 //////////////////////////
    using System;namespace RemotingTest_Prj.RemoteTest
    {
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    public class Hello
    {
    public Hello()
    {
    Console.WriteLine("Hello 被构造");
    }
    ~Hello()
    {
    Console.WriteLine("Hello 被析构");
    } public string Greeting(string name)
    {
    Console.WriteLine("Greeting called");
    return "Hello," + name;
    }
    }
    }////////////////////////  客户端代码 //////////////////////////using System;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using RemotingTest_Prj.RemoteTest;namespace Remoting_Client.Client
    {
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    class HelloClient
    {
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    ChannelServices.RegisterChannel(new TcpClientChannel()); //注册一个通道
    Hello obj = (Hello)Activator.GetObject(typeof(Hello),"tcp://localhost:8016/Hi");
    if (obj == null)
    {
    Console.WriteLine("Could not locate server");
    }
    for(int i=0;i<5;i++)
    {
    Console.WriteLine(obj.Greeting("Ray"));
    }
    }
    }
    }////////////////////////  服务器端代码 //////////////////////////
    using System;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;namespace RemotingTest_Prj.RemoteTest
    {
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    class HelloServer
    {
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    TcpServerChannel channel = new TcpServerChannel(8016);
    ChannelServices.RegisterChannel(channel);
    RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello),"Hi",WellKnownObjectMode.SingleCall);

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

  3.   

    所有Remoting的中间传送类必须继承MarshalByRefObject类!!
    ////////////////////////  公共库端代码 //////////////////////////
    using System;namespace RemotingTest_Prj.RemoteTest
    {
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    public class Hello : MarshalByRefObject //这里加上
    {
    public Hello()
    {
    Console.WriteLine("Hello 被构造");
    }
    ~Hello()
    {
    Console.WriteLine("Hello 被析构");
    } public string Greeting(string name)
    {
    Console.WriteLine("Greeting called");
    return "Hello," + name;
    }
    }
    }这个改成这样看看向命运发出战书,誓将命运踩在脚下!!