Search code examples
wcfaxioswcf-binding

WCF Method returns 400 Bad Request on Production


Methods of WCF REST Service are working with C# WCFClient against Production-Server. They are also working in Dev-Environment on localhost. The Service and the wsdl are ok from browser access on production also.

Access to svc

Access to wsdl

But when I access any method from the service on Production-Server, 400 Bad Request will be returned. IIS-log shows also 400: GET /WCD/WCF.svc/GetNextGuestNumber - 443 - Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/83.0.4103.61+Safari/537.36 https://www.DS.XXX.com/qa/dLobby 400 0 0 46

Here is my web.config from Production:

<?xml version="1.0"?>
  <configuration>  
    <appSettings>
      <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
      <add key="WBCCNN" value="PQkSAXkRXNsanbA5xK1KBZZqu+oxrJbssQlEiM1Fi+N2Vnz0F5QfAetqILb0QeLwlF7jMZ57k9J8sIlAJ1TRjtfgwh0V88Q2Kl/20Cny2WCTawWyDSECKg=="/>
    </appSettings>
    <system.web>  
      <compilation debug="false" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5"/>
    </system.web>  
    <system.serviceModel>  
      <services>  
        <service name="DS.XXX.Services.WCF" behaviorConfiguration="DSServiceBehavior">  
          <endpoint address=""  
              binding="basicHttpBinding"  
              bindingConfiguration="secureHttpBinding"  
              contract="DS.XXX.Services.IWCF"/>  
          <endpoint address="mex"  
              binding="mexHttpsBinding"  
              contract="IMetadataExchange" />  
        </service>  
      </services>  
      <bindings>  
        <basicHttpBinding>  
          <binding name="secureHttpBinding">  
            <security mode="Transport"> 
            </security>  
          </binding>  
        </basicHttpBinding>  
      </bindings>  
      <behaviors>  
        <serviceBehaviors>
          <behavior name="DSServiceBehavior">
            <serviceMetadata httpsGetEnabled="true" httpsGetUrl="" />
            <serviceDebug includeExceptionDetailInFaults="false"/>
          </behavior> 
        </serviceBehaviors>  
      </behaviors>  
      <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />  
    </system.serviceModel>  
    <system.webServer>  
      <modules runAllManagedModulesForAllRequests="true"/>  
    </system.webServer>  
  </configuration>

The methods are called from a REACT JS App with Axios

const res = await API.get("/GetNextGuestNumber");

The API from Axios is configured like so:

import axios from "axios";
import global from "../config";

export default axios.create({
  baseURL: global.webServicesUrl,
  headers: { "Content-Type": "application/json" }
});

As the service is working on dev and the svc and the wsdl is reachable on prod, it must be something with the binding and endpoint configuration?

I am using a Certificate from a third party vendor which is working fine also. I don't see it. Any help is appreciated. If you need any more details, pleas let me know.

Env.: Windows Server 2016, IIS


Solution

  • There is an error in the binding. You should use webhttpbinding to build the WCF REST Service, but I see in the configuration file that you used basichttpbinding.In this case, when you use JS to access the service, you will receive 400 errors.

            <services>
            <service name="Demo_rest_IIS.Service1"
                     behaviorConfiguration="UserServiceBehavior">
                <endpoint address=""
                          binding="webHttpBinding"
                          contract="Demo_rest_IIS.IService1"
                          behaviorConfiguration="ESEndPointBehavior" bindingConfiguration="bind"/>
            </service>
        </services>
        <behaviors>
            <endpointBehaviors>
                <behavior name="ESEndPointBehavior">
                    <webHttp helpEnabled="true"/>
                </behavior>
            </endpointBehaviors>
            <serviceBehaviors>
                <behavior name="UserServiceBehavior">
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
    

    This is the configuration file of WCF rest service that I deployed in IIS.

        [OperationContract]
        [WebGet(UriTemplate = "user/?name={name}",RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Result GetUserData(string name);
        [OperationContract]
        [WebInvoke(UriTemplate = "user", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Result PostUserData(UserData user);
        [OperationContract]
        [WebInvoke(Method = "PUT", UriTemplate = "user", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Result PutUserData(UserData user);
        [OperationContract]
        [WebInvoke(Method = "DELETE", UriTemplate = "user", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Result DeleteUserData(UserData user);
    

    This is the interface of the service.In the configuration file, I set the value of helpenabled to true. After deploying the service, we use the help document to see how to access it.

    enter image description here

    This is the help document for the service.

    For more information about WCF REST Service, please refer to the following link:

    https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/wcf-web-http-programming-model