WCF with Entity Framework Error Part II

Newbie, please carry me as I just started yesterday with WCF.

I use Northwind for data and add only customers, orders, order details and products to the model, so there’s nothing fantastic.

When I launch the application and call Test, and set a breakpoint, there is a value for the products, and it ends without errors. If I then try to call GetMaxQuantityByOrderID (10248), I will get the error shown below. Why does the work of Test () and the same WITHIN Test () method NOT work? I even added one more (Test1 (), exactly the same as Test, except that it returns a string: x.ProductName, which correctly displays Queso Cabrales ). It seems strange that a method that is called inside another works, but calling it directly raises an exception.

Another problem I have is that IEnumerable GetOrders () only works if I add .ToList (). Without it (or with .AsEnumerable ()) I get an error (the ObjectContext instance has been deleted and can no longer be used for operations that require a connection.), Although lazy loading is set to False . What is the logic behind this?

IServiceTest.cs

using System.Collections.Generic;
using System.ServiceModel;

namespace WcfTestServiceLibrary
{
    [ServiceContract]
    public interface IServiceTest
    {

        [OperationContract]
        IEnumerable<Orders> GetOrders();

        [OperationContract]
        IEnumerable<Customers> GetCustomers();

        [OperationContract]
        Customers GetCustomerByID(string customerID);

        [OperationContract]
        Orders GetOrderByID(int id);

        [OperationContract]
        IEnumerable<Order_Details> GetOrderDetailsByOrderID(int id);

        [OperationContract]
        Order_Details GetMaxQuantityByOrderID(int id);

        [OperationContract]
        void Test();
    }
}

ServiceTest.cs

using System.Collections.Generic;
using System.Linq;

namespace WcfTestServiceLibrary
{

    public class ServiceTest : IServiceTest
    {
        public IEnumerable<Orders> GetOrders()
        {
            using (var ctx = new NWEntities())
            {
                return (from o in ctx.Orders.Include("Order_Details.Products").Include("Customers")
                        select o).ToList();
            }
        }

        public IEnumerable<Customers> GetCustomers()
        {
            using (var ctx = new NWEntities())
            {
                return (from c in ctx.Customers
                        select c);
            }
        }

        public Customers GetCustomerByID(string customerID)
        {
            return (from c in GetCustomers()
                    where c.CustomerID == customerID
                    select c).FirstOrDefault();
        }

        public Orders GetOrderByID(int id)
        {
            IEnumerable<Orders> orders = GetOrders();
            return (from o in orders
                    where o.OrderID == id
                    select o).FirstOrDefault();
        }

        public IEnumerable<Order_Details> GetOrderDetailsByOrderID(int id)
        {
            return GetOrderByID(id).Order_Details;
        }

        public Order_Details GetMaxQuantityByOrderID(int id)
        {
            Orders order = GetOrderByID(id);
            return order == null ? null : order.Order_Details.OrderByDescending(x => x.Quantity).FirstOrDefault();

        }

        public void Test()
        {
            const int orderID = 10248;
            var oq = GetMaxQuantityByOrderID(orderID);
            var x = oq.Products;
        }
    }
}

Error:

An error occurred while receiving the HTTP response to http://localhost:8732/Design_Time_Addresses/WcfTestServiceLibrary/Service1/. 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.Channels.ClientReliableChannelBinder`1.RequestClientReliableChannelBinder`1.OnRequest(TRequestChannel channel, Message message, TimeSpan timeout, MaskingMode maskingMode)
   at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, TimeSpan timeout, MaskingMode maskingMode)
   at System.ServiceModel.Channels.ClientReliableChannelBinder`1.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Security.SecuritySessionClientSettings`1.SecurityRequestSessionChannel.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 IServiceTest.GetMaxQuantityByOrderID(Int32 id)
   at ServiceTestClient.GetMaxQuantityByOrderID(Int32 id)

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)
+5
source share
3 answers

, . , - Entity Framework, POCO, WCF, , EF--. . -, ContextOptions.ProxyCreationEnabled false. EF - POCO. -, WCF DataContractSerializer ProxyDataContractResolver POCO.

http://msdn.microsoft.com/en-us/library/ee705457.aspx. , , , , EF , - EF. , , , , .

+8

.Inculde LINQ-Statement.

, , "" "" . "

, , .Include ...

: → , → → ..

, wcf .

, ignoreDataMember

... ....

[IgnoreDataMember] public Order OrderFatherElement {get; ; }

, WCF

@microsoft.... ?

,

+3

WCF . IEnumerable . Btw. IQueryable <T> IEnumerable <T> ? , GetOrders , ? , .

:

WCF , - ​​ WCF WCF. WCF .

IQueryable builds an expression tree that is compiled into a database query, so you cannot execute this query (differentiated execution) out of context (the context is responsible for connecting to the database). You must rewrite your methods. In your case, each method should create a complete request and execute this request within the context. The query is executed by selecting a single record (for example, FirstOrDefault ()) or by converting to a list.

+1
source

All Articles