由于我无法理解的原因,我开始在我的 WCF 服务中收到此错误:底层连接已关闭:连接意外关闭。
服务器堆栈跟踪:在 System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
在 System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan 超时) 在 System.ServiceModel.Channels.RequestChannel.Request(消息消息,TimeSpan 超时) 在 System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息消息,TimeSpan超时)在 System.ServiceModel.Channels.ServiceChannel.Call(字符串操作,布尔单向,ProxyOperationRuntime 操作,Object[] 输入,Object[] 输出,TimeSpan 超时)在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime操作)在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 消息)在 [0] 处重新抛出异常:在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) 在 IPublicService.GetProduct(字符串条形码)
在 PublicServiceClient.GetProduct(字符串条形码)内部异常:基础连接已关闭:连接意外关闭。在 System.Net.HttpWebRequest.GetResponse() 在 System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(时间跨度超时)
下面是服务方法:
public Product GetProduct(string barcode)
{
DataContext Db = new DataContext();
var p = Db.Products.Find(barcode);
if (p == null)
return null;
return new Product()
{
ProductID = p.ProductID,
Name = p.Name,
Nutrition = p.Nutrition,
Allergen = p.Allergen,
Image = p.Image,
ManufacturerID = p.ManufacturerID,
ReviewIDs = p.ReviewIDs
};
}
这是产品数据合同:
[DataContract]
public class Product
{
[Key]
[Required]
[DataMember]
public string ProductID { get; set; }
[Required]
[DataMember]
public string Name { get; set; }
[Required]
[DataMember]
public string Nutrition { get; set; }
[Required]
[DataMember]
public string Allergen { get; set; }
[Required]
[DataMember]
public string Image { get; set; }
[Required]
[DataMember]
public virtual Manufacturer ManufacturerID { get; set; }
[DataMember]
public virtual ICollection<Review> ReviewIDs { get; set; }
}
但是,如果我将服务方法更改为:
public Product GetProduct(string barcode)
{
DataContext Db = new DataContext();
var p = Db.Products.Find(barcode);
if (p == null)
return null;
//return new Product()
//{
// ProductID = p.ProductID,
// Name = p.Name,
// Nutrition = p.Nutrition,
// Allergen = p.Allergen,
// Image = p.Image,
// ManufacturerID = p.ManufacturerID,
// ReviewIDs = p.ReviewIDs
//};
return new Product();
}
没有抛出异常,它返回并清空产品,所以我不确定是什么导致了我得到的模棱两可的异常,任何人都可以阐明这个问题吗?