请问remoting中代理类能否在构造函数中加参数,又如何调用呢,谢谢

解决方案 »

  1.   

    public class ServerObject:MarshalByRefObject
    {public ServerObject(string dint)
    {
    //其它功能
    }
            public Person GetPersonInfo(string name,string sex,int age)
            {
                Person person = new Person();
                person.Name = name;
                person.Sex = sex;
                person.Age = age;
                return person;
            }
    }
    这样能行吗如何调用呢,谢谢高手给指点
      

  2.   

    只要Person类可以序列化就可以
      

  3.   

    实体类:HelloServiceClass.cs
    using System;public class HelloServiceClass : MarshalByRefObject 
    { static int n_instance; public HelloServiceClass(string teststr)
    {
    Console.WriteLine(teststr);
    }
    public HelloServiceClass() 
    {
    n_instance++;
    Console.WriteLine(this.GetType().Name + " has been created.  Instance # = {0}", n_instance);
    }
    ~HelloServiceClass() 
    {
    Console.WriteLine("Destroyed instance {0} of HelloServiceClass.", n_instance);
    n_instance --;
    }
    public String HelloMethod(String name) 
    { // Reports that the method was called.
    Console.WriteLine();
    Console.WriteLine("Called HelloMethod on instance {0} with the '{1}' parameter.", 
    n_instance, name); // Calculates and returns the result to the client.
    return "Hi there " + name + ".";
    }
    }服务器端ServerClass.csusing System;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using System.Collections;
    public class ServerClass 
    { public static void Main()  
    {
    try
    { TcpChannel [] TcpChannels = new TcpChannel[2]; for( int i =0;i<2;i++)
    {
    IDictionary props = new Hashtable();
    props["name"] = "tcpChannel"+i.ToString();
    props["port"] = 5000+i; 
    TcpChannel channel = new TcpChannel(
    props, 
    null, 
    new BinaryServerFormatterSinkProvider()
    );
    ChannelServices.RegisterChannel(channel);  RemotingConfiguration.RegisterActivatedServiceType(typeof(HelloServiceClass)); props.Clear();

    }
    }
    catch(Exception ex)
    {
    Console.WriteLine(ex.Message+ex.StackTrace);
    } Console.WriteLine("Press enter to stop this process.");
    Console.ReadLine();
    }
    }客户端调用:
    using System;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;public class ClientClass 
    { public static void Main() 

    try
    {
    for( int i =0;i<2;i++)
    { ChannelServices.RegisterChannel(new TcpChannel()); RemotingConfiguration.RegisterActivatedClientType(typeof(HelloServiceClass),
    //"tcp://localhost:8082");
    "tcp://localhost:"+(5000+i).ToString()); HelloServiceClass service = new HelloServiceClass("testaaa"+(5000+i).ToString()); if(service == null) 
    {
    Console.WriteLine("Could not locate server.");
    return;
    } // Calls the remote method.
    Console.WriteLine();
    Console.WriteLine("Calling remote object"+i.ToString());
    Console.WriteLine(service.HelloMethod("Caveman"));
    Console.WriteLine(service.HelloMethod("Spaceman"));
    Console.WriteLine(service.HelloMethod("Client Man"));
    Console.WriteLine("Finished remote object call");
    //Console.WriteLine();
    }
    }
    catch(Exception ex)
    {
    Console.WriteLine(ex.Message+ex.StackTrace);
    }
    }
    }
    目标:我想使用多通道的客户端激活模式,能实在代理类构造传参,但在服务器端运行时出现以下错误,本人感到注册的事,但报错是字典的事,所以不理解肯请高手指点或大伙帮找错,谢谢!
    其中服务器端运行出错如下:ex.Message
    已添加项。字典中的键: “HelloServiceClass, helloserviceclass”  所添加的键: “HelloServiceClass, helloserviceclass