Remoting使用中序列化的问题,麻烦大家帮忙看看

bingdian37 2009-03-10 04:25:27
在remoting使用过程中
从客户端创建一个对象
传递到服务端
服务端有个持久化的功能,将对象进行二进制序列化到文件,保存起来
这个时候,从客户端传递过来的对象会无法序列化
提示:System.Runtime.Remoting.ServerIdentity未标记为可序列化

网上查了查,似乎是因为从客户端传递过来的对象存在一个透明代理
这个是无法序列化的

不知道应该如何解决。。。。
...全文
251 25 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
bingdian37 2009-03-26
  • 打赏
  • 举报
回复
问题先放放,结贴
wanabe 2009-03-20
  • 打赏
  • 举报
回复
jf
连风 2009-03-12
  • 打赏
  • 举报
回复
if(ServerIdentity 是否是你自己创建的对象?)
{
如果是的话,你注意下错误信息(提示:System.Runtime.Remoting.ServerIdentity未标记为可序列化)
这个对象的命名空间怎么在System.Runtime.Remoting下?

是否是服务端没有引用ServerIdentity这个程序集呢?
或设置你在服务端再次序列化的时候是否有特殊的地方,例如:不在一个宿主中导致找不到该程序集?
最好能在服务端调试一下,看看客户端创建对象的.GetType()是否和服务端接收到对象的.GetType()一致?
}
else
{
那就是不能被序列化了!
}
bingdian37 2009-03-12
  • 打赏
  • 举报
回复
xiaoyuzi
我的对象是标记过的,

并且已经完成了传递,所以这个无需怀疑,否则连从客户端传递到服务端都是问题

现在的问题是,服务端保存客户端传递过来的对象时,无法序列化
xiaoyuzi 2009-03-12
  • 打赏
  • 举报
回复
你这个对象类定义标记了[Serializable] 么,还有你这个对象是简单对象(仅包括.net framework基本类型)还是组合有其它不能串行化的对象
bingdian37 2009-03-12
  • 打赏
  • 举报
回复
我顶一个,,
bingdian37 2009-03-12
  • 打赏
  • 举报
回复
感谢xiaoyuzi,wuyq11的回复

但是还是没有解决问题

因为我的问题不是在客户端与服务端的传参问题上发生的

而是服务端已经接收到客户端对象了,并且可以正常使用

但是我想将这个来自客户端的对象,手工进行二进制序列化的话就会有问题...

提示:System.Runtime.Remoting.ServerIdentity未标记为可序列化
wonder888888 2009-03-12
  • 打赏
  • 举报
回复
如果试图序列化以下类实例,将会显示一个 SerializationException,说明 MyStuff 类型未标记为可序列化。

public class MyStuff : MyObject
{
public int n3;
}
使用序列化属性非常方便,但是它存在上述的一些限制。有关何时标记类以进行序列化(因为类编译后就无法再序列化),请参考有关说明(请参阅下面的序列化规则)。

选择性序列化
类通常包含不应被序列化的字段。例如,假设某个类用一个成员变量来存储线程 ID。当此类被反序列化时,序列化此类时所存储的 ID 对应的线程可能不再运行,所以对这个值进行序列化没有意义。可以通过使用 NonSerialized 属性标记成员变量来防止它们被序列化,如下所示:

[Serializable]
public class MyObject
{
public int n1;
[NonSerialized] public int n2;
public String str;
}

xiaoyuzi 2009-03-12
  • 打赏
  • 举报
回复
注意把ip改成自己的阿
xiaoyuzi 2009-03-12
  • 打赏
  • 举报
回复
我做的例子。没有问题的。如果需要完整的工程,我可以发给你。

要传输的对象和remoting对象类


[Serializable]
public class Student:ISerializable
{
private string name;

public string Name
{
get { return name; }
set { name = value; }
}

private int age;

public int Age
{
get { return age; }
set { age = value; }
}

public Student()
{ }

// For deserialize
public Student(SerializationInfo info, StreamingContext ctxt)
{
this.name = (string)info.GetValue("Name", typeof(string));
this.age = (int)info.GetValue("Age", typeof(int));
}

public override string ToString()
{
return "Age is:" + this.age.ToString() + ",Name is:" + this.name;
}

#region ISerializable Members

public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Name", this.name);
info.AddValue("Age", this.age);
}

#endregion
}

public class RemotingObj:MarshalByRefObject
{
public void SendAndSerialize(Student stu)
{
Stream stream = File.Open("c:\\Student.ser", FileMode.Create);
BinaryFormatter bformatter = new BinaryFormatter();

bformatter.Serialize(stream, stu);
stream.Close();
}
}



Remoting server


static void Main(string[] args)
{
try
{
BinaryClientFormatterSinkProvider clientProvider = null;

BinaryServerFormatterSinkProvider serverProvider =
new BinaryServerFormatterSinkProvider();

serverProvider.TypeFilterLevel =
System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;

System.Collections.IDictionary props = new System.Collections.Hashtable();


props["port"] = 5008;
props["typeFilterLevel"] = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
props["CustomErrorsEnabled"] = false;
TcpChannel chan = new TcpChannel(props, clientProvider, serverProvider);
ChannelServices.RegisterChannel(chan, false);

RemotingConfiguration.RegisterWellKnownServiceType(typeof(ObjLib.RemotingObj),
"ObjLib.RemotingObj", WellKnownObjectMode.Singleton);

Console.WriteLine("Remoting server is started \r\n");
}
catch (RemotingException re)
{
Console.WriteLine(re.Message);
}


// Wait for Quit to terminate program
while (!(Console.In.ReadLine().Equals("Quit")))
{
;
}
}




client


static void Main(string[] args)
{
try
{
BinaryClientFormatterSinkProvider clientProvider =
new BinaryClientFormatterSinkProvider();

BinaryServerFormatterSinkProvider serverProvider =
new BinaryServerFormatterSinkProvider();

serverProvider.TypeFilterLevel =
System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;

System.Collections.IDictionary props = new System.Collections.Hashtable();
props["port"] = 0;
props["name"] = System.Guid.NewGuid().ToString();
props["typeFilterLevel"] = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
props["CustomErrorsEnabled"] = false;
TcpChannel chan =
new TcpChannel(props, clientProvider, serverProvider);
ChannelServices.RegisterChannel(chan, false);

string strURL = String.Format("{0}{1}:{2}/{3}", "tcp://", "9.123.74.36", 5008, "ObjLib.RemotingObj");
ObjLib.RemotingObj rmtObj = (ObjLib.RemotingObj)Activator.GetObject(typeof(ObjLib.RemotingObj), strURL);

ObjLib.Student stu = new ObjLib.Student();
stu.Age = 30;
stu.Name = "Tommy";
rmtObj.SendAndSerialize(stu);
Console.WriteLine("Send object finish.\r\n");
}
catch (RemotingException re)
{
Console.WriteLine(re.Message);
}


// Wait for Quit to terminate program
while (!(Console.In.ReadLine().Equals("Quit")))
{
;
}
}


xiaoyuzi 2009-03-11
  • 打赏
  • 举报
回复
要作为参数或是函数方法返回值的必须是可串行化的,加属性[Serializable],remoting对象必须继承自MarshalByRefObject,看下面的例子
[Serializable]
public class YDInfoObj
{
private string m_strGoodsName;

public string GoodsName
{
get
{
return m_strGoodsName;
}
set
{
m_strGoodsName = value;
}
}


}

[Serializable]
public class SendVehicleInfoObj
{
// Property-ID
private int m_nID = 0;
public int ID
{
get { return m_nID; }
set { m_nID = value; }
}

private int m_nOrganizationID;

public int OrganizationID
{
get { return m_nOrganizationID; }
set { m_nOrganizationID = value; }
}

private int m_nTransportPathID;

public int TransportPathID
{
get { return m_nTransportPathID; }
set { m_nTransportPathID = value; }
}
}

public class DCCheckinRemotingObj:MarshalByRefObject
{
/*public override object InitializeLifetimeService()
{
ILease lease = (ILease)base.InitializeLifetimeService();
lease.InitialLeaseTime = TimeSpan.FromMinutes(10);
lease.RenewOnCallTime = TimeSpan.FromMinutes(40);

return lease;
}*/

public YDInfoObj GetYDInfo(int nKey)
{
...
return ydInfo;
}

public SendVehicleInfoObj GetSendVehicleInfo(int nKey)
{
...
return svInfoObj;
}
}
xiaoyuzi 2009-03-11
  • 打赏
  • 举报
回复
要作为参数或是函数方法返回值的必须是可串行化的,加属性[Serializable],remoting对象必须继承自MarshalByRefObject,看下面的例子
[Serializable]
public class YDInfoObj
{
private string m_strGoodsName;

public string GoodsName
{
get
{
return m_strGoodsName;
}
set
{
m_strGoodsName = value;
}
}


}

[Serializable]
public class SendVehicleInfoObj
{
// Property-ID
private int m_nID = 0;
public int ID
{
get { return m_nID; }
set { m_nID = value; }
}

private int m_nOrganizationID;

public int OrganizationID
{
get { return m_nOrganizationID; }
set { m_nOrganizationID = value; }
}

private int m_nTransportPathID;

public int TransportPathID
{
get { return m_nTransportPathID; }
set { m_nTransportPathID = value; }
}
}

public class DCCheckinRemotingObj:MarshalByRefObject
{
/*public override object InitializeLifetimeService()
{
ILease lease = (ILease)base.InitializeLifetimeService();
lease.InitialLeaseTime = TimeSpan.FromMinutes(10);
lease.RenewOnCallTime = TimeSpan.FromMinutes(40);

return lease;
}*/

public YDInfoObj GetYDInfo(int nKey)
{
...
return ydInfo;
}

public SendVehicleInfoObj GetSendVehicleInfo(int nKey)
{
...
return svInfoObj;
}
}
bingdian37 2009-03-11
  • 打赏
  • 举报
回复
有人知道么?
bingdian37 2009-03-11
  • 打赏
  • 举报
回复
加分100,盼望有人能够解答
bingdian37 2009-03-10
  • 打赏
  • 举报
回复
盼望着,盼望着……
bingdian37 2009-03-10
  • 打赏
  • 举报
回复
有人遇到过这问题么?
bingdian37 2009-03-10
  • 打赏
  • 举报
回复
自己顶下..
bingdian37 2009-03-10
  • 打赏
  • 举报
回复
各位朋友,我在传输过程中并没有问题

有位朋友说要继承MarshalByRefObject

这个...继承了该对象之后,默认是无法将对象来回传递的,只能传引用.
bingdian37 2009-03-10
  • 打赏
  • 举报
回复
呵呵,我是需要将一个对象,序列化为一个文件,保存起来,

并不是传输过程中的序列化,那个不需要管的

我的问题是,在客户端创建的对象,传递到服务端之后,无法直接序列化为一个文件保存

我奇怪的是,既然MS可以将这个对象从客户端先序列化传递到服务端.....
加载更多回复(5)

17,748

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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