在学习remoting时遇到了一些问题。主要是在做复杂对象时,好像建立双向通道时不行。贴出代码(拷贝下可运行)
首先是远程对象代码:
using System;namespace Wrox.Samples
{
/// <summary>
/// MyRemoteObject 的摘要说明。
/// </summary>
public class MyRemoteObject:System.MarshalByRefObject
{
public MyRemoteObject(int state)
{
Console.WriteLine("Constructor called");
this.state=state;
}
private int state;
public int State
{
get
{
return state;
}
set
{
state=value;
}
}
public string Hello()
{
state++;
Console.WriteLine("Hello called");
return "Hell,.Net Client"+state.ToString()+"!";
}
public A GetA()
{
return new A(11);
}
public B GetB()
{
return new B(22);
}
private B b;
public void SetB(B b)
{
Console.WriteLine("SetB called");
this.b=b;
}
public int UseB()
{
Console.WriteLine("UseB called");
return b.Data;
}
}
[Serializable]
public class A
{
private int data;
public A(int data)
{
Console.WriteLine("Constructor of serializable calss A called");
this.data=data;
}
public int Data
{
get
{
Console.WriteLine("A.Data called");
return data;
}
}
}
public class B:MarshalByRefObject
{
private int data;
public B(int data)
{
Console.WriteLine("Constructor of remotable class B called");
this.data=data;
}
public int Data
{
get
{
Console.WriteLine("B.Data called");
return data;
}
}
}
}
首先是远程对象代码:
using System;namespace Wrox.Samples
{
/// <summary>
/// MyRemoteObject 的摘要说明。
/// </summary>
public class MyRemoteObject:System.MarshalByRefObject
{
public MyRemoteObject(int state)
{
Console.WriteLine("Constructor called");
this.state=state;
}
private int state;
public int State
{
get
{
return state;
}
set
{
state=value;
}
}
public string Hello()
{
state++;
Console.WriteLine("Hello called");
return "Hell,.Net Client"+state.ToString()+"!";
}
public A GetA()
{
return new A(11);
}
public B GetB()
{
return new B(22);
}
private B b;
public void SetB(B b)
{
Console.WriteLine("SetB called");
this.b=b;
}
public int UseB()
{
Console.WriteLine("UseB called");
return b.Data;
}
}
[Serializable]
public class A
{
private int data;
public A(int data)
{
Console.WriteLine("Constructor of serializable calss A called");
this.data=data;
}
public int Data
{
get
{
Console.WriteLine("A.Data called");
return data;
}
}
}
public class B:MarshalByRefObject
{
private int data;
public B(int data)
{
Console.WriteLine("Constructor of remotable class B called");
this.data=data;
}
public int Data
{
get
{
Console.WriteLine("B.Data called");
return data;
}
}
}
}
配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application name="SimpleServer">
<service>
<activated type="Wrox.Samples.MyRemoteObject,MyRemoteObject"></activated>
</service>
<channels>
<channel ref="tcp" port="9000"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
代码:
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;namespace SimpleServer
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
RemotingConfiguration.Configure(@"C:\test\RemotingTest\第六章\传递对象\SimpleServer\SimpleServer.exe.config");
Console.WriteLine("Press return to exit");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application name="SimpleClient">
<client url="tcp://192.168.8.122:9000/SimpleServer">
<activated type="Wrox.Samples.MyRemoteObject,MyRemoteObject"/>
</client>
<channels>
<channel ref="tcp" port="9002"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
客户端代码:
using System;
using Wrox.Samples;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;namespace SimpleClient
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
RemotingConfiguration.Configure(@"C:\test\RemotingTest\第六章\传递对象\SimpleClient\SimpleClient.exe.config");
try
{
MyRemoteObject obj=new MyRemoteObject(333);
// int x=obj.State;
// for(int i=0;i<5;i++)
// {
// Console.WriteLine(obj.Hello());
// }
obj.Hello();
A a=obj.GetA();
int a1=a.Data;
B b=obj.GetB();
int b1=b.Data; B bs=new B(44);
int bs1=bs.Data; obj.SetB(bs);
bs1=obj.UseB(); }
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
{
Console.WriteLine("SetB called");
this.b=b;
}
中this.b的b在哪声明的?好像序列化的问题
public void SetB(B b)
{
Console.WriteLine("SetB called");
this.b=b;
}
我也查过了,好像有说序列化,但不知道应该怎么改。