新手,請耐心等待,因爲我昨天剛剛開始使用WCF。WCF與實體框架錯誤第二部分
我正在使用Northwind的數據,只將客戶,訂單,訂單詳細信息和產品添加到模型中,所以沒有什麼奇特。
當我啓動應用程序並調用Test並設置一個斷點時,產品的值就在那裏,並且完成時沒有錯誤。如果我然後嘗試調用GetMaxQuantityByOrderID(10248),我會在底部列出錯誤。爲什麼測試()工作和相同的方法WITHIN測試()不工作?我甚至添加了另一個(Test1(),與Test完全相同,除了它返回字符串:x.ProductName,它正確顯示Queso Cabrales)。似乎很奇怪的是,在另一個作品中被調用的方法,但直接調用它會導致異常。
我遇到的另一個問題是IEnumerable GetOrders()只在添加.ToList()時才起作用。沒有它(或with .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();
}
}
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;
}
}
}
錯誤:
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)
如果我更改爲IQueryable的,我得到了錯誤的ObjectContext的實例已設置,並且不能再被用於需要連接的操作。什麼是WCF服務的正確方法?我不明白什麼配置跟蹤將允許我做。正如我所說的,我剛剛在2天前開始使用WCF。 – 2010-08-19 13:03:41
我添加了一些關於跟蹤和EF的信息。 – 2010-08-19 13:34:35
該問題只出現在WCF測試客戶端中。如果我不使用客戶端並使用asp.net頁面來獲取結果,它就會起作用。 – 2010-08-24 02:35:37