0
votes

I am struggling with this WCF error for some time now without any luck. Basically I am tying to fetch an Entity Poco with Navigation Properties and connected objects via WCF Services. My EF v6 code successfully get the poco with all the related entities from the DB

Poco debug view

but as i try to access this Entity via WCF service, i see the following error -

An error occurred while receiving the HTTP response to http://localhost:8734/Design_Time_Addresses/BusinessLogicServicesLayer/userServices/. 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.HttpChannelFactory1.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 IuserServicesQuery.getCompleteUserSnapshot(String emailAddress) at IuserServicesQueryClient.getCompleteUserSnapshot(String emailAddress) Inner Exception: The underlying connection was closed: An unexpected error occurred on a receive. at System.Net.HttpWebRequest.GetResponse() at System.ServiceModel.Channels.HttpChannelFactory1.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)

my AppConfig file looks like this -

<endpoint address="" behaviorConfiguration="MyBehavior" binding="basicHttpBinding"
          bindingConfiguration="IncreasedTimeout" name="BasicHttpEndpoint"
          contract="BusinessLogicServicesLayer.IuserServicesQuery" listenUriMode="Explicit">
          <identity>
            <dns value="localhost" />
          </identity>
</endpoint>

&&

 <bindings>
      <basicHttpBinding>
        <binding name="IncreasedTimeout"
          openTimeout="12:00:00"
          receiveTimeout="12:00:00" closeTimeout="12:00:00"
          sendTimeout="12:00:00">
        </binding>
      </basicHttpBinding>
    </bindings>

. .

<behaviors>
  <endpointBehaviors>
    <behavior name="MyBehavior">
      <dataContractSerializer maxItemsInObjectGraph="2147483646" />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>

can someone please help out or point me to a correct direction

1
are you trying to return the poco class directly?Ricardo Pontual
yes, as in there is no DTO object. sending the POCO object directly to the clientGaurav Minhas
it would be better use a separate model to the output...anyway, some of the navigation properties could not be filled, they can be load lazily, so you can get the data before return the object... try to use for exemple a var result = poco.ToList() or something like that that return all data in you poco objectRicardo Pontual

1 Answers

0
votes

This is because when data is returned, serialization fails, causing the WCF service to stop automatically.

Solution:

We can serialize the proxy class into the entities we need before returning the data.

Here is a demo,The student class contains the navigation properties of other entities:

            public Student Getstu()
    {
        CodeFirstDBContext codeFirstDBContext = new CodeFirstDBContext();
        Student student =codeFirstDBContext.Student.Find(1);
        var serializer = new DataContractSerializer(typeof(Student), new DataContractSerializerSettings()
        {
            DataContractResolver = new ProxyDataContractResolver()
        });
        using (var stream = new MemoryStream())
        {

            serializer.WriteObject(stream, student);
            stream.Seek(0, SeekOrigin.Begin);
            var stu = (Student)serializer.ReadObject(stream);
            return stu;
        }


    }

This is the method that the client will call.

     ServiceReference1.ServiceClient serviceClient = new ServiceReference1.ServiceClient();
        var stu = serviceClient.Getstu();

Client-side will call successfully.

UPDATE Disabling Loading loading also solves this problem:

      public class CodeFirstDBContext : DbContext
{
    public CodeFirstDBContext() : base("name=DBConn")
    {
        this.Configuration.ProxyCreationEnabled = false;
        this.Configuration.LazyLoadingEnabled = false;
        Database.SetInitializer(new CreateDatabaseIfNotExists<CodeFirstDBContext>());
    }
 }