Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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>);
        }
    }


As you can see, there is an EntityRef<designation> object. Here is the code for Designation:

C#
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
    }


I retrieve the user object from a SQL Server Database within my Service Contract's Definition like so:

C#
public Class MyService : IService
{
        public User GetRandomUser()
        {   
            User user;
            
            user = GetFromDB();

            //user.Detach();

            return user;
        }
}


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)
Posted

1 solution

Nevermind. I solved this myself.

I looked in the Service Trace View and saw this line under the entry of where the fault was thrown: The InnerException message was 'Object graph for type 'System.Data.Linq.EntitySet`1[[User, User, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' contains cycles and cannot be serialized if reference tracking is disabled.'.

And when I removed all references to User from Designation, it worked.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900