这个例子我也做了。我这里没有问题的。
我把程序端写成是winform了。
不是原来的控制台程序。

解决方案 »

  1.   

    我也是改成winform了.
    server:
    /// <summary>
    /// 远程调用对象
    /// </summary>
    public class RemotingClass:MarshalByRefObject
    {
    public RemotingClass() 
    {
    Console.WriteLine("HelloServer activated");
    } public string HelloMethod(string name) 
    {
    Console.WriteLine("Hello.HelloMethod : {0}", name);
    return "Hi there " + name;
    }
    }                   //改方法是由窗体load时调用
    private void GetRemotObj()
    {
    try
    {

    TcpServerChannel channel=new TcpServerChannel(8085); ChannelServices.RegisterChannel(channel);
    RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingClass),"TestHello",WellKnownObjectMode.SingleCall);
    Console.WriteLine("hit to exit");
                    
    }
    catch(Exception w)
    { Console.WriteLine(w.Message);
    }
    }
    client:首先引用了封装RemotingClass的DLL文件,然后在按钮事件中:
    private void button1_Click(object sender, System.EventArgs e)
    {
    try
    {

    TcpClientChannel chan = new TcpClientChannel();
    ChannelServices.RegisterChannel(chan);
    RemotingClass obj = (RemotingClass)Activator.GetObject(
    typeof(RemotingClass),
    "tcp://localhost:8085/TestHello");
    string str="";
    if (obj == null)
    System.Console.WriteLine("Could not locate server");
    else 
                                                 //这是出错的地方,报的错误消息见上面
    str=obj.HelloMethod("ni hao");
    //Console.WriteLine(c); textBox1.Text=str;
    return;
    }
    catch(Exception w)
    { Console.WriteLine(w.Message);
    } }
     
      

  2.   

    找到原因了,是因为我在服务器没有添加封装类的DLL.我想问一问,难道要远程调用的对象,必须用DLL封装才能被服务器和客户端调用,为啥???
      

  3.   

    我以前在服务器端是直接在命名空间声明的,远程调用是不行.我刚才将服务器的声明去掉,然后引用封装远程调用类的DLL,就好了.想知道原因何在?谢谢各位.