Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
实体框架错误的WCF第二部分_Wcf_Entity Framework - Fatal编程技术网

实体框架错误的WCF第二部分

实体框架错误的WCF第二部分,wcf,entity-framework,Wcf,Entity Framework,新手,请容忍我,因为我昨天刚开始使用WCF 我使用Northwind作为数据,只在模型中添加了客户、订单、订单详细信息和产品,所以没有什么特别之处 当我启动应用程序并调用测试并设置断点时,products的值就在那里,并且完成时不会出错。如果然后尝试调用GetMaxQuantityByOrderID(10248),则会在底部列出错误。为什么Test()可以工作,而Test()中的相同方法不工作?我甚至添加了另一个(Test1(),与Test完全相同,只是它返回字符串:x.ProductName,

新手,请容忍我,因为我昨天刚开始使用WCF

我使用Northwind作为数据,只在模型中添加了客户、订单、订单详细信息和产品,所以没有什么特别之处

当我启动应用程序并调用测试并设置断点时,products的值就在那里,并且完成时不会出错。如果然后尝试调用GetMaxQuantityByOrderID(10248),则会在底部列出错误。为什么Test()可以工作,而Test()中的相同方法不工作?我甚至添加了另一个(Test1(),与Test完全相同,只是它返回字符串:x.ProductName,它正确地显示了Queso Cabrales)。奇怪的是,在另一个方法中调用的方法可以工作,但直接调用它会导致异常

我遇到的另一个问题是,IEnumerable GetOrders()仅在我添加.ToList()时才起作用。如果没有它(或使用.AsEnumerable()),我会得到一个错误(ObjectContext实例已被释放,不能再用于需要连接的操作),即使延迟加载设置为False。这背后的逻辑是什么

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();
    }
}
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;
        }
    }
}

对于第一个问题,请尝试打开您的服务。IEnumerable的第二个问题是由不同的执行引起的。顺便问一下,你们了解IQueryable和IEnumerable的区别吗?您知道GetOrders方法会将所有订单、相关客户和产品加载到内存中吗?即使您想要选择单个订单,您仍然可以在服务中加载所有订单

编辑:

WCF跟踪将向您展示服务或客户机执行期间发生的情况—它跟踪WCF内部,是WCF开发的基本工具。WCF跟踪和跟踪教程


IQueryable构建表达式树,该树被编译为数据库查询,所以您不能在上下文范围外执行该查询(差异执行)(上下文负责数据库连接)。你必须重写你的方法。在您的情况下,每个方法都必须创建完整的查询并在上下文范围内执行该查询。通过选择单个记录(如FirstOrDefault())或转换为列表来执行查询

我和你有一个非常相似的问题。我发现封装在POCO类周围的实体框架代理在默认情况下由WCF序列化程序序列化,该序列化程序无法在客户端反序列化,因为客户端不知道EF代理包装器。我找到了两种解决方案。第一个是将ContextOptions.ProxyCreationEnabled设置为false。这会阻止EF为POCO对象创建代理。第二个是指示服务器端的WCF DataContractSerializer使用ProxyDataContractResolver作为POCO进行序列化


第二个选项位于。由于我刚刚发现了这些解决方案,我不能说我会推荐哪一种作为一般做法,尽管我倾向于后者,因为EF查询可能会不时被其他调用方重复使用,这些调用方可能希望使用适当的EF代理返回对象。无论如何,我知道这个问题已经晚了,但我希望这能帮助其他遇到这个问题的人

这是你的LINQ声明中的.Inculde

我相信这是因为基本上产生了无限的数据结构,指向你们关系的“一”面和“多”面

通过展开数据结构,在调试器中的.Include查询语句的结果处设置断点,可以观察到这一点

观察: 订单-->多个产品,每个产品-->订单-->多个产品等等

我想wcf会陷入其中,并因此而咳嗽

要打破这条链,您可以简单地避免数据传输对象中的回指 通过添加ignoreDataMember属性

在你们关系的许多方面。。。在你的情况下,产品

[IgnoreDataMember] 公共秩序OrderFatherElement{get;set;}

这样,WCF将在到达该子节点时停止serializingattempt

@微软。。。。有可能修复吗

问候


Kieredin Garbaa

如果我更改为IQueryable,我会得到一个错误:ObjectContext实例已被释放,无法再用于需要连接的操作。WCF服务的正确方式是什么?我不明白配置跟踪将允许我做什么。正如我所说,我2天前才开始使用WCF。问题只发生在WCF测试客户端。如果我不使用客户端并使用asp.net页面查看结果,它会工作。然后打开WCF跟踪并检查它在测试客户端失败的原因。谢谢,使用第一个解决方案,帮助很大感谢您提供的信息,非常有帮助
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)