Problem with getting data from a SQL table with foreign keys in a WCF project in Visual Studio

48 Views Asked by At

I have a problem with my WCF project in Visual Studio. I have connected a SQL database with ADO.NET Entity Data Model to the project.

So far everything works. For example when I want to get data from a view in my database there is no problem or error. For that I use the following code:

public interface IWCF
{
    [OperationContract]
    List<view_table1> GettingData();
}

public class Service_WCF : IWCF
{
    public Database_Entity db = new Database_Entity();
   
    public List<view_table1> GettingData();
    {
        return db.view_table1.ToList();
    }
}

When I debug the WCF_Service.svc and call the GettingData() method in the WCF test client, I get the data from the view. So that works with no problem and all other [OperationContracts] too.

But when I want to load data from a normal table and not a view I get an error every time. The table I want to load data from in the test client has a connection to two other tables in the SQL database. The connections are all loaded by the model when creating it so I thing they should be OK.

The error I am getting in the WCF test client is:

Fehler beim Empfangen der HTTP-Antwort für http://localhost:65373/WCF_Service.svc. Die Ursache kann sein, dass die Dienstendpunktbindung kein HTTP-Protokoll verwendet. Eine andere mögliche Ursache ist, dass der HTTP-Anforderungskontext vom Server abgebrochen wird (vermutlich auf das Herunterfahren des Diensts zurückzuführen). Weitere Informationen finden Sie in den Serverprotokollen.

Server stack trace:

bei System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
bei System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) bei System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
bei System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
bei System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
bei System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) bei System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:

bei System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
bei System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
bei IWCF.GettingData()
bei WCFClient.GettingData()

Inner Exception:

Die zugrunde liegende Verbindung wurde geschlossen: Unbekannter Fehler beim Empfangen..

bei System.Net.HttpWebRequest.GetResponse()
bei System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)

Inner Exception:

Von der Übertragungsverbindung können keine Daten gelesen werden: Eine vorhandene Verbindung wurde vom Remotehost geschlossen. bei System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

bei System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
bei System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)

Inner Exception:
Eine vorhandene Verbindung wurde vom Remotehost geschlossen

bei System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
bei System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

However the strange thing is when I debug the WCF and set a break point in the code and use a var like this:

public List<table1> GettingDataFromTableNotView();
{
    var b = db.table1.ToList();
    return b;
}

Then the var b has the value that is in the "table1" and I can view it in the code while debugging. But when I click the button that the program should go on after the breakpoint, I get the error again. So the WCF can get the data but it can't return it and that is strange to me.

I also tried things like:

var b = db.table1.Include("table2").Include("table3").ToList();
return b;

that did not work.

I also tried in the Model.Context.cs to turn on LazyLoadingEnabled = true.

0

There are 0 best solutions below