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

funstar888 2006-04-30 09:34:39
请问remoting中代理类能否在构造函数中加参数,又如何调用呢,谢谢
...全文
89 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
funstar888 2006-05-08
  • 打赏
  • 举报
回复
实体类: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.cs

using 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
ReinhardCao 2006-04-30
  • 打赏
  • 举报
回复
只要Person类可以序列化就可以
funstar888 2006-04-30
  • 打赏
  • 举报
回复
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;
}
}


这样能行吗如何调用呢,谢谢高手给指点

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧