0
votes

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

In my VitalSign model I have this code:

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

In my VitalSignValue model:

public Guid IdVitalSign { get; set; }

And I got a default manager with basic functions like 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.

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

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.

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

ASP.Net hosted compilation

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)

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

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)

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)

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.

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)

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

1
can you show us service function, and code where you consume that service?Robert
I Added some extra information. Does anyone think it might have something to do with serialization?Stefan
when you call the service for the second time, do you request same information as the first time, and is the information in database changed in between those requests? Does this happen in other functions. Try to add some dummy function, which returns only string, and see does that happen. Also, check if this happen, if you do not include navigation properties.Robert
I do call the same function, nothing changed, just pressing the "Invoke" button in the WCF Test Client, and pressing it again after it got the data. That's when the error happens. This doesn't happen in all functions, like when I would have a function called getAllVitalSignValues() which only gets the rows from the VitalSignValues model, it does work properlyStefan
I suggest you to turn on WCF tracing then, and see which exception does service raise. You can find more hereRobert

1 Answers

0
votes

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.

Try:

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

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