Remoting给远程对象属性赋值.....望大家来帮个忙,严究两天未解
类
public class wr:MarshalByRefObject
{
public ftpname;
public wr()
{
ftpname= "aaa ";
}
public string addnew()
{
return ftpname;
}
}
服务端:
TcpServerChannel channel = new TcpServerChannel(8086);
ChannelServices.RegisterChannel(channel,false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hosts), "hi ", WellKnownObjectMode.SingleCall);
客户端:
wrwr= (Hosts)Activator.GetObject(typeof(wr), "tcp://127.0.0.1:8046/hi ");
wr.ftpname = "22222 ";
Response.Write(wr.addnew());
使用这种方法却实可以调用远程对象,但是这种方法调用远程对象的时候就实例化构造函数.所以ftpname的值永远都是默认构造函数的aaa,没有办法在客户端给值.
在网上找了很久发现可以用客户端激活方式,但是总是出错不知道为什么
服务端:
TcpServerChannel channel = new TcpServerChannel(8086);
ChannelServices.RegisterChannel(channel,false);
//客户激活模式
RemotingConfiguration.ApplicationName = "hi ";
RemotingConfiguration.RegisterActivatedServiceType(typeof(wr));
客户端:
RemotingConfiguration.RegisterActivatedClientType(typeof(wr), "tcp://127.0.0.1:8086/hi ");
wr obj = new wr();
wr.ftpname= "2222 ";
Response.Write(wr.addnew());
------解决方案--------------------RemotingConfiguration.RegisterActivatedClientType(typeof(wr), "tcp://127.0.0.1:8086/hi ");这行代码只能被执行一次,你应该把它放在一个静态构函数里面。以保证它只被执行一次!
------解决方案--------------------远程对象激活模式不对.
下面是我写的一个简单例子:
类:
[Serializable]
public class Calculator : MarshalByRefObject
{
public Calculator()
{
}
public int Sum(int x, int y)
{
return x + y;
}
}
服务器端:
class Server
{
static void Main(string[] args)
{
ChannelServices.RegisterChannel(new TcpChannel(4006), false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Calculator)
, "Calculator.rem ", WellKnownObjectMode.Singleton); // 重点地方
Console.WriteLine( "运行中... ");
Console.