问题描述
大家好,
我有一个让我烦恼的小问题,我不知道如何解决.
我有一个服务合同设置来返回一个用户对象.以下是用户的定义:
Hi All,
I''ve got a small issue that is annoying me and I don''t know how to fix it.
I have a Service Contract setup to return an User object. Below is the definition of User:
public class User
{
#region Fields
private int _UserID;
private string _Name;
private string _Surname;
private int _DesignationID;
private EntityRef<Designation> _Designation;
#endregion
#region Constructor
public User()
{
this._Designation = default(EntityRef<Designation>);
}
#endregion
#region Properties
public int UserID
{
get { return this._UserID; }
set { this._UserID = value; }
}
public string Name
{
get { return this._Name; }
set { this._Name = value; }
}
public string Surname
{
get { return this._Surname; }
set { this._Surname = value; }
}
public int DesignationID
{
get
{
return this._DesignationID;
}
set
{
if ((this._DesignationID == value))
return;
this._DesignationID = value;
}
}
public Designation Designation
{
get
{
return this._Designation.Entity;
}
set
{
Designation previousValue = this._Designation.Entity;
if (previousValue == value)
return;
this._Designation.Entity = value;
}
}
#endregion
#region Methods
public override string ToString()
{
return _Name + " " + _Surname;
}
#endregion
public void Detach()
{
this._Designation = default(EntityRef<Designation>);
}
}
如您所见,这里有一个EntityRef< designation>.目的.以下是指定代码:
As you can see, there is an EntityRef<designation> object. Here is the code for Designation:
public class Designation
{
#region Fields
private int _DesignationID;
private string _Description;
private EntitySet<User> _User;
#endregion // Fields
#region Constructor
public Designation()
{
this._User = new EntitySet<User>(
new Action<User>(this.Attach_User),
new Action<User>(this.Detach_User));
}
#endregion //Constructor
#region Properties
public int DesignationID
{
get { return this._DesignationID; }
set
{
if (this._DesignationID == value)
return;
this._DesignationID = value;
}
}
public string Description
{
get { return this._Description; }
set
{
if (this._Description == value)
return;
this._Description = value;
}
}
public EntitySet<User> User
{
get { return this._User; }
set
{
this._User.Assign(value);
}
}
#endregion //Properties
#region Methods
private void Attach_User(User entity)
{
entity.Designation = this;
}
private void Detach_User(User entity)
{
entity.Designation = null;
}
public override string ToString()
{
return _Description;
}
#endregion // Methods
}
我在服务合同的定义内从SQL Server数据库中检索用户对象,如下所示:
I retrieve the user object from a SQL Server Database within my Service Contract''s Definition like so:
public Class MyService : IService
{
public User GetRandomUser()
{
User user;
user = GetFromDB();
//user.Detach();
return user;
}
}
当我取消注释user.Detach();
行时,用户已成功传递回我的客户端,但显然User.Designation
为空.当我注释掉user.Detach();
时,出现以下错误(来自WCF测试客户端):
套接字连接已中止.这可能是由于处理您的消息时出错或远程主机超出了接收超时,或者是潜在的网络资源问题引起的.本地套接字超时为``00:00:59.9740000''.
我究竟做错了什么?我知道这与对象序列化的方式有关.任何帮助将不胜感激.以下是堆栈跟踪:
服务器堆栈跟踪:
在System.ServiceModel.Channels.StreamConnection.Read处(字节[]缓冲区,Int32偏移量,Int32大小,TimeSpan超时)
在System.ServiceModel.Channels.SessionConnectionReader.Receive(TimeSpan超时)
在System.ServiceModel.Channels.SynchronizedMessageSource.Receive(TimeSpan超时)处
在System.ServiceModel.Channels.FramingDuplexSessionChannel.Receive(TimeSpan超时)处
在System.ServiceModel.Channels.FramingDuplexSessionChannel.TryReceive(TimeSpan超时,消息和消息)处
在System.ServiceModel.Dispatcher.DuplexChannelBinder.Request中(消息消息,TimeSpan超时)
在System.ServiceModel.Channels.ServiceChannel.Call处(字符串操作,布尔型单向,ProxyOperationRuntime操作,Object [] ins,Object [] outs,TimeSpan超时)
在System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime操作)中
在System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage消息)
异常重新引发为[0]:
在System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg)
在System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke处(MessageData& msgData,Int32类型)
在AssessorService.GetRandomUser()
在AssessorServiceClient.GetRandomUser()
内部例外:
读取操作失败,请参阅内部异常.
在System.Net.Security.NegotiateStream.ProcessRead(字节[]缓冲区,Int32偏移量,Int32计数,AsyncProtocolRequest asyncRequest)处
在System.Net.Security.NegotiateStream.Read处(字节[]缓冲区,Int32偏移量,Int32计数)
在System.ServiceModel.Channels.StreamConnection.Read处(字节[]缓冲区,Int32偏移量,Int32大小,TimeSpan超时)
内部例外:
套接字连接已中止.这可能是由于处理您的消息时出错或远程主机超出了接收超时,或者是潜在的网络资源问题引起的.本地套接字超时为``00:00:59.9740000''.
在System.ServiceModel.Channels.SocketConnection.ReadCore(Byte []缓冲区,Int32偏移量,Int32大小,TimeSpan超时,布尔值关闭)
在System.ServiceModel.Channels.SocketConnection.Read处(字节[]缓冲区,Int32偏移量,Int32大小,TimeSpan超时)
在System.ServiceModel.Channels.DelegatingConnection.Read处(字节[]缓冲区,Int32偏移量,Int32大小,TimeSpan超时)
在System.ServiceModel.Channels.ConnectionStream.Read处(字节[]缓冲区,Int32偏移量,Int32计数)
在System.Net.FixedSizeReader.ReadPacket(Byte []缓冲区,Int32偏移量,Int32计数)
在System.Net.Security.NegotiateStream.StartFrameHeader处(字节[]缓冲区,Int32偏移量,Int32计数,AsyncProtocolRequest asyncRequest)
在System.Net.Security.NegotiateStream.ProcessRead处(字节[]缓冲区,Int32偏移量,Int32计数,AsyncProtocolRequest asyncRequest)
内部例外:
远程主机强行关闭了现有连接
在System.Net.Sockets.Socket.Receive(Byte []缓冲区,Int32偏移量,Int32大小,SocketFlags socketFlags)
在System.ServiceModel.Channels.SocketConnection.ReadCore处(字节[]缓冲区,Int32偏移量,Int32大小,TimeSpan超时,布尔值关闭)
when I uncomment the user.Detach();
line, the user is successfully passed back to my client, BUT obviously User.Designation
is null. When I comment out user.Detach();
I get the following error (from the WCF Test Client):
The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was ''00:00:59.9740000''.
What am I doing wrong? I know this has something to do with the way the objects are serialized. Any help will be appreciated. Below is the Stack Trace:
Server stack trace:
at System.ServiceModel.Channels.StreamConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)
at System.ServiceModel.Channels.SessionConnectionReader.Receive(TimeSpan timeout)
at System.ServiceModel.Channels.SynchronizedMessageSource.Receive(TimeSpan timeout)
at System.ServiceModel.Channels.FramingDuplexSessionChannel.Receive(TimeSpan timeout)
at System.ServiceModel.Channels.FramingDuplexSessionChannel.TryReceive(TimeSpan timeout, Message& message)
at System.ServiceModel.Dispatcher.DuplexChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at AssessorService.GetRandomUser()
at AssessorServiceClient.GetRandomUser()
Inner Exception:
The read operation failed, see inner exception.
at System.Net.Security.NegotiateStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.NegotiateStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.ServiceModel.Channels.StreamConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)
Inner Exception:
The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was ''00:00:59.9740000''.
at System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout, Boolean closing)
at System.ServiceModel.Channels.SocketConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)
at System.ServiceModel.Channels.DelegatingConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)
at System.ServiceModel.Channels.ConnectionStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
at System.Net.Security.NegotiateStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
at System.Net.Security.NegotiateStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
Inner Exception:
An existing connection was forcibly closed by the remote host
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout, Boolean closing)
推荐答案
这篇关于WCF序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!