0

I've developed and Deployed Rest Enabled WCF Services on the IIS but when I call those Services from the browser or My Asp.Net Mvc Consumer I get a 400 Bad Request Error. I've also tried debugging the services through WCFStorm.REST client in which I get the following error:

    System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: 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)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetResponse()
   at RestSharp.Http.GetRawResponse(HttpWebRequest request)
   at RestSharp.Http.GetResponse(HttpWebRequest request)

Here is my WCF Configuration:

  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpConfig"
                  allowCookies="true"
                  maxReceivedMessageSize="20000000"
                  maxBufferSize="20000000"
                  maxBufferPoolSize="20000000">
          <readerQuotas maxDepth="32"
                        maxArrayLength="200000000"
                        maxStringContentLength="200000000" />
        </binding>
        <binding name="webHttpsConfig"
                  allowCookies="true"
                  maxReceivedMessageSize="20000000"
                  maxBufferSize="20000000"
                  maxBufferPoolSize="20000000">
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
          <readerQuotas maxDepth="32"
                        maxArrayLength="200000000"
                        maxStringContentLength="200000000" />
        </binding>
        <binding name="webHttpBindingXML" />
        <binding name="webHttpBindingJSON" />
      </webHttpBinding>
    </bindings>
    <services>
      <service name="BusinessServices.UserService">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="webHttpsConfig" contract="BusinessServices.IUserService" behaviorConfiguration="web">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <endpoint address="XML" binding="webHttpBinding" bindingConfiguration="webHttpBindingXML" contract="BusinessServices.IUserService" behaviorConfiguration="RestXMLEndpointBehavior" />
        <endpoint address="JSON" binding="webHttpBinding" bindingConfiguration="webHttpBindingJSON" contract="BusinessServices.IUserService" behaviorConfiguration="RestJSONEndpointBehavior" />

        <host>
          <baseAddresses>
            <add baseAddress="https://localhost:7743/UserService.svc" />
          </baseAddresses>
        </host>

      </service>
      <service name="BusinessServices.RoleService">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="webHttpConfig" contract="BusinessServices.IRoleService" behaviorConfiguration="web">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <endpoint address="XML" binding="webHttpBinding" bindingConfiguration="webHttpBindingXML" contract="BusinessServices.IRoleService" behaviorConfiguration="RestXMLEndpointBehavior" />
        <endpoint address="JSON" binding="webHttpBinding" bindingConfiguration="webHttpBindingJSON" contract="BusinessServices.IRoleService" behaviorConfiguration="RestJSONEndpointBehavior" />

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:7742/RoleService.svc" />
          </baseAddresses>
        </host>

      </service>
      <service name="BusinessServices.CriminalService">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="webHttpConfig" contract="BusinessServices.ICriminalService" behaviorConfiguration="web">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <endpoint address="XML" binding="webHttpBinding" bindingConfiguration="webHttpBindingXML" contract="BusinessServices.ICriminalService" behaviorConfiguration="RestXMLEndpointBehavior" />
        <endpoint address="JSON" binding="webHttpBinding" bindingConfiguration="webHttpBindingJSON" contract="BusinessServices.ICriminalService" behaviorConfiguration="RestJSONEndpointBehavior" />

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:7742/CriminalService.svc" />
          </baseAddresses>
        </host>

      </service>
      <service name="BusinessServices.NationalityService">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="webHttpConfig" contract="BusinessServices.INationalityService" behaviorConfiguration="web">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <endpoint address="XML" binding="webHttpBinding" bindingConfiguration="webHttpBindingXML" contract="BusinessServices.INationalityService" behaviorConfiguration="RestXMLEndpointBehavior" />
        <endpoint address="JSON" binding="webHttpBinding" bindingConfiguration="webHttpBindingJSON" contract="BusinessServices.INationalityService" behaviorConfiguration="RestJSONEndpointBehavior" />

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:7742/NationalityService.svc" />
          </baseAddresses>
        </host>

      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
        </behavior>
        <behavior name="RestJSONEndpointBehavior">
          <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" />
        </behavior>
        <behavior name="RestXMLEndpointBehavior">
          <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Xml" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="webHttpBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

ASP.NET MVC Client Endpoints:

  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="WebHttpBinding_ICriminalService" />
        <binding name="WebHttpBinding_IRoleService" />
        <binding name="WebHttpsBinding_IUserService" />
        <binding name="WebHttpBinding_INationalityService" />
      </webHttpBinding>
    </bindings>

    <behaviors>
      <endpointBehaviors>
        <behavior name="webEndpoint">
          <webHttp defaultBodyStyle="Wrapped" helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>

    <client>
      <endpoint address="http://localhost:7742/CriminalService.svc" behaviorConfiguration="webEndpoint" binding="webHttpBinding" bindingConfiguration="WebHttpBinding_ICriminalService" contract="CriminalServiceReference.ICriminalService" name="WebHttpBinding_ICriminalService" />
      <endpoint address="http://localhost:7742/RoleService.svc" behaviorConfiguration="webEndpoint" binding="webHttpBinding" bindingConfiguration="WebHttpBinding_IRoleService" contract="RoleServiceReference.IRoleService" name="WebHttpBinding_IRoleService" />
      <endpoint address="https://localhost:7743/UserService.svc" behaviorConfiguration="webEndpoint" binding="webHttpBinding" bindingConfiguration="WebHttpsBinding_IUserService" contract="UserServiceReference.IUserService" name="WebHttpsBinding_IUserService" />
      <endpoint address="http://localhost:7742/NationalityService.svc" behaviorConfiguration="webEndpoint" binding="webHttpBinding" bindingConfiguration="WebHttpBinding_INationalityService" contract="NationalityServiceReference.INationalityService" name="WebHttpBinding_INationalityService" />
    </client>
 </system.serviceModel>

A Sample Service Contract:

    [ServiceContract]
    [ValidationBehavior]

    public interface INationalityService : IDisposable
    {
        [OperationContract]
        [FaultContract(typeof(ValidationFault))]
        [WebInvoke(UriTemplate = "", Method = "POST")]
        void Create(NationalityDTO Nationality);

        [OperationContract]
        [FaultContract(typeof(ValidationFault))]
        [WebGet(UriTemplate = "")]

        IEnumerable<NationalityDTO> GetNationalities();
     }

P.S.: I've tried almost everything that's possible and available on internet / SO but to no avail. Thanks in advance.

Update:- I've tried to debug the Services Project as a Visual Studio Startup Project but there it runs fine in isolation. The problem occurs only when the one published and hosted on IIS is accessed.

ar27111994
  • 527
  • 1
  • 5
  • 18
  • 1
    *"An existing connection was forcibly closed by the remote host."* -- Find out why that happened, and fix the cause. – Robert Harvey Sep 09 '16 at 18:09
  • @RobertHarvey and how can I find that out? PS I am still a rookie in WCF. Thanks – ar27111994 Sep 10 '16 at 05:08
  • Are you able to access the rest endpoints using the browser? – Joel McBeth Sep 10 '16 at 16:38
  • @jcmcbeth The ones run as Startup Project yes, the ones hosted on IIS, sadly no. – ar27111994 Sep 10 '16 at 16:40
  • I assume when you say running as a Startup Project you are running it in Visual Studio with IIS expess? If it works there but not in IIS is sounds like an IIS issue. Have you looked at the IIS logs? The error code for the request might give you more information. – Joel McBeth Sep 10 '16 at 16:42
  • sorry didn't go there but will tell you tomorrow – ar27111994 Sep 10 '16 at 17:47
  • @jcmcbeth Hi, I didn't get anything in the LogFiles folder (the log file didn't contain anything special). In fact, the log file doesn't record anything when accessing any service method whereas it does record a 200 and 404 when accessing service homepage and an unknown / invalid service method respectively. But I do get `net::ERR_CONNECTION_RESET` Error in my web browser's console in the developer tools upon accessing a valid service method. All this happens when accessing the service hosted on IIS. – ar27111994 Sep 10 '16 at 22:30
  • Some questions: are you publishing the service in another machine? If so, the server configuration should no contains "localhost" in address. If not, which endpoint are trying, http or https? – Ricardo Pontual Sep 11 '16 at 01:04
  • @RicardoPontual No its the same machine. I've tried http endpoints as they contain get methods. Also the https endpoint seems to be working fine in WcfStorm REST Client (as this endpoint contains only POST Methods, hence, cannot be tested via a web browser). – ar27111994 Sep 11 '16 at 09:51
  • @RicardoPontual Ok buddy I was wrong, even Https Endpoint is generating a 400 response code Error. I just didn't get any exception details, when accessing it via WcfStorm REST Client, which is wierd. Anyhow, the problem occurs on both **http and https** Endpoints. Thanks – ar27111994 Sep 11 '16 at 10:04
  • Try to define a template uri for the service, like this: `[WebInvoke(UriTemplate = "/Nationalities")] IEnumerable GetNationalities();` and try to browse using http://localhost:7742/NationalityService.svc/Nationalities – Ricardo Pontual Sep 11 '16 at 16:21
  • @RicardoPontual Nope, this doesn't help either :'-( – ar27111994 Sep 11 '16 at 17:04
  • 1
    Just saying that you tried many things does not help. Explain what _exactly_ you tried. This exception is a blanket statement that occurs when the server closes the connection, generally when an unhandled exception occurs. See [duplicate](http://stackoverflow.com/questions/15858784/wcf-the-underlying-connection-was-closed-an-unexpected-error-occurred-on-a-rece) to learn about tracing and other tools to troubleshoot this issue. – CodeCaster Sep 12 '16 at 13:33
  • 1
    @CodeCaster Thank you so much, the trace files did the trick. The problem was with the SQL Server IIS AppPool .NET Login. I followed [this](http://stackoverflow.com/questions/7698286/login-failed-for-user-iis-apppool-asp-net-v4-0) solution. Thanks again. :-) – ar27111994 Sep 12 '16 at 15:51
  • 1
    @ar2 nice, happy to help and thanks for your feedback. – CodeCaster Sep 12 '16 at 15:55

0 Answers0