WCF服务中具有子项的实体框架模型仅工作一次

时间:2021-04-25 02:20:36

To start off explaining, I have a model called VitalSign and one called VitalSignValues.

为了开始解释,我有一个名为VitalSign的模型和一个名为VitalSignValues的模型。

In my VitalSign model I have this code:

在我的VitalSign模型中,我有以下代码:

[ForeignKey("IdVitalSign")]
public virtual ICollection<VitalSignValue> VitalSignValues { get; set; }

In my VitalSignValue model:

在我的VitalSignValue模型中:

public Guid IdVitalSign { get; set; }

And I got a default manager with basic functions like getAll(), ...

我得到了一个默认管理器,其基本功能如getAll(),...

The project contains multiple web services and they all work fine, except for this one (VitalSignService). When I run the service, even with the WCF Test Client and I test the getAll function, it works.

该项目包含多个Web服务,它们都可以正常工作,除了这个(VitalSignService)。当我运行该服务时,即使使用WCF测试客户端并且我测试了getAll函数,它仍然有效。

The problem is that the getAll function only works like once, when I try to call the function again I suddenly get this error:

问题是getAll函数只能工作一次,当我尝试再次调用该函数时,我突然得到这个错误:

An error occurred while receiving the HTTP response to http://localhost/webservice/VitalSignService.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.

接收到http://localhost/webservice/VitalSignService.svc的HTTP响应时发生错误。这可能是由于服务端点绑定不使用HTTP协议。这也可能是由于服务器中止HTTP请求上下文(可能是由于服务关闭)。请参阅服务器日志以获取更多详

I tried to check the trace logs, but for some reason the only information it gives me is:

我试图检查跟踪日志,但由于某种原因,它给我的唯一信息是:

ASP.Net hosted compilation

ASP.Net托管编译

AppDomain unloading

Here is the error log (although it doesn't really contain good information for me)

这是错误日志(虽然它对我来说并不包含好的信息)

Server stack trace: at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason) at System.ServiceModel.Channels.HttpChannelFactory`1.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)

服务器堆栈跟踪:位于System.ServiceModel.Channels的System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException,HttpWebRequest请求,HttpAbortReason abortReason)在System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)处System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息消息,TimeSpan超时)处于System.ServiceModel.Channels.ServiceChannel.Call(String action,Boolean oneway,ProxyOperationRuntime operation,Object [。 System,Service.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)中的System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime操作)中的ins,Object []出,TimeSpan超时)

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 IVitalSignService.GetAllVitalSigns() at VitalSignServiceClient.GetAllVitalSigns()

在[0]处重新抛出异常:在System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg)处于System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&msgData,Int32类型)的IVitalSignService.GetAllVitalSigns( )在VitalSignServiceClient.GetAllVitalSigns()

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

内部异常:底层连接已关闭:接收上发生意外错误。 System.Service.Net.HttpWebRequest.GetResponse()at System.ServiceModel.Channels.HttpChannelFactory`1.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)

内部异常:无法从传输连接读取数据:远程主机强制关闭现有连接。 System.Net.Connection.SyncRead上的System.Net.PooledStream.Read(Byte []缓冲区,Int32偏移量,Int32大小)处的System.Net.Sockets.NetworkStream.Read(Byte []缓冲区,Int32偏移量,Int32大小) (HttpWebRequest请求,布尔userRetrievedStream,布尔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)

内部异常:System.Net.Sockets.NetworkStream.Read(字节)处的System.Net.Sockets.Socket.Receive(Byte []缓冲区,Int32偏移量,Int32大小,SocketFlags socketFlags)远程主机强制关闭现有连接。 []缓冲区,Int32偏移量,Int32大小)

EDIT:

Here is some more information:

以下是一些更多信息:

In my application, I call this function:

在我的应用程序中,我调用此函数:

var client = new VitalSignServiceClient();
_vitalSigns = client.GetAllVitalSignsWithValues().ToList();
client.Close();

Inside of my service, I got this function:

在我的服务中,我得到了这个功能:

public ICollection<VitalSign> GetAllVitalSignsWithValues()
{
    return _vitalSignManager.GetAll("VitalSignValues");
}

In my generic manager, this happens:

在我的通用经理中,会发生这种情况

public ICollection<TObject> GetAll(params string[] navigationProperties)
{
    var query = Context.Set<TObject>().AsQueryable();
    foreach (string navigationProperty in navigationProperties)
    query = query.Include(navigationProperty);
    var list = query.ToList<TObject>();
    return list;
}

When I try to find the problem through debugging, it does go into the service, into the generic manager and it does get the VitalSigns, it's just the moment where the client retrieves the data from the web service that the error occurs and everything fails.

当我试图通过调试找到问题时,它会进入服务,进入通用管理器,它确实获得了VitalSigns,它就是客户端从Web服务检索数据时发生错误并且一切都失败的时刻。

Also remember that all of this actually works (but only 1 out of 10 times, so probably only when the server refreshed it or something)

还要记住所有这些实际上都有效(但是只有10次中有1次,所以可能只有当服务器刷新它或其他东西时)

SOLVED: Removed the "virtual" in front of the IList

已解决:删除了IList前面的“虚拟”

1 个解决方案

#1


0  

I don't like the way you're closing/disposing your WCF proxies and EF contexts. Looking for trouble there. Look into "using" like below. I would also recommend you don't use a static EF context. Again looking for trouble by doing this. (LinqToSql declare and instantiate DataContext best practice?). Third, I know it's tempting to make a generic repository that does everything but this ends up being more work than it's worth. For testing, performance and maintainability purposes, make your API calls specific.

我不喜欢你关闭/处理你的WCF代理和EF上下文的方式。在那里寻找麻烦。请查看下面的“使用”。我还建议您不要使用静态EF上下文。再次通过这样做寻找麻烦。 (LinqToSql声明并实例化DataContext的最佳实践?)。第三,我知道制作一个能够完成所有工作的通用存储库是很诱人的,但这最终会带来更多的工作而不是它的价值。出于测试,性能和可维护性的目的,请使您的API调用具体。

Try:

using(var client = new VitalSignServiceClient()
{
    _vitalSigns = client.GetAllVitalSignsWithValues().ToList();
}

public ICollection<VitalSign> GetAllVitalSigns()
{
    using(ctx = new YourContext())
    {
       // do stuff
    }
}

#1


0  

I don't like the way you're closing/disposing your WCF proxies and EF contexts. Looking for trouble there. Look into "using" like below. I would also recommend you don't use a static EF context. Again looking for trouble by doing this. (LinqToSql declare and instantiate DataContext best practice?). Third, I know it's tempting to make a generic repository that does everything but this ends up being more work than it's worth. For testing, performance and maintainability purposes, make your API calls specific.

我不喜欢你关闭/处理你的WCF代理和EF上下文的方式。在那里寻找麻烦。请查看下面的“使用”。我还建议您不要使用静态EF上下文。再次通过这样做寻找麻烦。 (LinqToSql声明并实例化DataContext的最佳实践?)。第三,我知道制作一个能够完成所有工作的通用存储库是很诱人的,但这最终会带来更多的工作而不是它的价值。出于测试,性能和可维护性的目的,请使您的API调用具体。

Try:

using(var client = new VitalSignServiceClient()
{
    _vitalSigns = client.GetAllVitalSignsWithValues().ToList();
}

public ICollection<VitalSign> GetAllVitalSigns()
{
    using(ctx = new YourContext())
    {
       // do stuff
    }
}