user1620696
user1620696

Reputation: 11375

WCF Service TargetInvocationException - Possible Cyclic Reference

I need a little help with an Exception being thrown at my WCF service. The point is: I need to develop a WPF application and I've heard that to treat data WCF is the best choice so I used it only with basic knowledge and now I have this problem.

The point is, I have a Customer class, a Supplier class and an Order class. I have then two services: the first one has all methods to add, read and edit customers and suppliers from database. This service works perfectly fine. The second service has methods to add new orders, edit orders and read them from database, and this service has a problem.

The class Customer has a list of Suppliers (the suppliers from which the customer buys), the class Supplier has a list of Customers (the customers that buy from that supplier). The class order has a Customer, a Supplier and a list of Itens (and the itens are added to the database together with the order).

The method for adding an order works fine, however, once an order has been added, the method to list the orders throws the TargetInvocationException. I searched a lot on the internet and the best answer I've found is that it's possibly a "Ciclyc Reference".

The method to list the orders is inside the repository class and is simply:

public IQueryable<Order> ListOrders()
{
    return Context.Set<Order>().Include(x => x.Customer).Include(x => x.Supplier);
}

Where Context is the entity framework context. I tried to remove the includes and load the customer and supplier separately, but the result was the same exception.

Can someone please help me ? If I need to post more information just tell me. And sorry if this matter is trivial, I'm new to WPF and WCF so I'm still a little confused.

Thanks a lot in advance.

EDIT Exception below

An error occurred while receiving the HTTP response to http://SomeService.com/OrdersService.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being

aborted by the server (possibly due to the service shutting down). See server logs for more details.

Server stack trace: at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout) at System.ServiceModel.Dispatcher.RequestChannelBinder.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 IOrdersService.ReturnOrders() at OrdersServiceClient.ReturnOrders()

Inner Exception: The underlying connection was closed: An unexpected error occurred on a receive. at System.Net.HttpWebRequest.GetResponse() at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)

Inner Exception: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)

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.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

Upvotes: 1

Views: 1277

Answers (1)

Fede
Fede

Reputation: 44038

Mark your data objects with the [DataContract(IsReference = true)] attribute. This should resolve the issue.

Upvotes: 1

Related Questions