WCF proxy ProcessGetResponseWebException on HttpChannelRequest WaitForReply

10,769

You need to update server config file to see exception details in client:

<serviceBehaviors>
   <behavior name="EmployeeManager_Behavior">
      <serviceDebug includeExceptionDetailInFaults="true"/>
      <serviceMetadata httpGetEnabled="true" />
   </behavior>
</serviceBehaviors>

Please post updated exception details with that option enabled.

Looking at your code I may give you one suggestion. Do not store DbContext is static variable. EntityFramework does a lot of caching so context instantiating costs nothing. Static var can be disposed by somebody and Null check will not catch that.

Share:
10,769
Cel
Author by

Cel

Contact: http://www.linkedin.com/pub/kristjan-laane/40/9b0/698

Updated on June 04, 2022

Comments

  • Cel
    Cel almost 2 years

    ServiceContract

    [OperationContract]
    List<Campaign> AttainCampaigns();
    

    Works when Instantiating Objects Directly

    public List<Campaign> AttainCampaigns()
    {
        var campaign1 = new Campaign { Id = "x", Name = "namex" };
        var campaign2 = new Campaign { Id = "y", Name = "namey" };
        var campaigns = new List<Campaign> { campaign1, campaign2 };
        return campaigns;
    }
    

    If Equivalent Objects from Entity Framework, Error thrown on Client

    // Different implementation getting the campaign objects from database
    private List<Campaign> RetrieveCampaigns()
    {
        return Global.Repositor.Campaigns.Select(c => c).ToList();;
    }
    
    public static EntityRepositor Repositor
    {
        get
        {
            if (_entityRepositor == null)
                _entityRepositor = new EntityRepositor();
            return _entityRepositor;
        }
    }
    
    public class EntityRepositor : DbContext
    {
        public DbSet<Campaign> Campaigns { get; set; }
    }
    

    No Errors Evident on Server

    • Strangely, when I attach the debugger to the process on the server-side I see no exceptions - Entity Framework seems to nicely instantiate the objects from the underlying SQL Express database
    • I do not even see any First Chance Exceptions in Output

    WCF Test Client Exception after About 15 Seconds, Then Immediately

    • First time I invoke AttainCampaigns() the client waits/processes for about 15 seconds and then shows the exception below
    • All subsequent invocations show error immediately, even if I remove service and reconnect

    _My guess is that the database call itself is fine, but it takes too long, so I need to increase certain timeout values in either the server or client configuration? If that is the correct tree to bark under, which timeouts might they be?

    An error occurred while receiving the HTTP response to http://example.com/Service.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.
    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 IBacker.AttainCampaigns()
       at BackerClient.AttainCampaigns()
    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)
    

    Server - perSession ServiceContract & Web.config

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, 
    ConcurrencyMode = ConcurrencyMode.Single)]
    

    &

      <system.web>
        <compilation targetFramework="4.5" />
      </system.web>
      <system.serviceModel>     
        <bindings>
          <wsHttpBinding>
            <binding name="wsHttp" allowCookies="true">
              <security mode="None" />
            </binding>
          </wsHttpBinding>
        </bindings>
        <services>
          <service name="MyNameSpace.MyImplementation">
            <endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttp"
              name="wsHttpEp" contract="MyNameSpace.IServiceContract" />
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
        <directoryBrowse enabled="true" />
      </system.webServer>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
      </entityFramework>
    

    Client - ChannelFactory & App.config

    _channelFactory = new ChannelFactory<T>("wsHttpEp");
    Service = _channelFactory.CreateChannel();
    

    &

      <system.serviceModel>
        <bindings>
          <wsHttpBinding>
            <binding name="wsHttp" allowCookies="true">
              <security mode="None" />
            </binding>
          </wsHttpBinding>
        </bindings>
        <client>
          <endpoint address="http://example.com/Service.svc" contract="MyNameSpace.IServiceContract"
                    binding="wsHttpBinding" bindingConfiguration="wsHttp" name="wsHttpEp" />
        </client>
      </system.serviceModel>
    
  • Cel
    Cel almost 12 years
    Thanks for the reply! I have these set already i believe? I have edited the question with the config...
  • Cel
    Cel almost 12 years
    Also, tried instantiating new EntityRepositor() every time - and in the service implementation - rather than reusing the shown static reference, but that did not change the symptoms..
  • Dmitry Harnitski
    Dmitry Harnitski almost 12 years
    Everything looks fine in your code. Call stack does not give much info. As far as issue happens when EF involved - can you create simple ASPX page in server and call RetrieveCampaigns() from that page without wcf?
  • Cel
    Cel almost 12 years
    After inspecting the object graph with the aid of the debugger I saw a dynamic proxy as member of the Campaign instance if generated with ObjectContext / DbContext (rather than directly). Turns out dynamic proxies have to be disabled for serialization to work for WCF! I.e. adding public EntityRepositor() { Configuration.ProxyCreationEnabled = false; } fixed the issue!