vendredi 8 mai 2015

400 Bad Request when calling wcf from html using XDomainRequest

I'm on this problem since more than one week and all I have tried haven't work so well. I started with Jquery to find out that Ie9 was different from the other browsers and requires its own way for calling wcf. It is simple enough and I did all the required, but it still doesn't work! Both adresses are http://localhost on IIS. I also renamed My html file so it becomes an aspx file. I added a Custom ContentTypeMapper so I dont get conflicts with the content types. I won't add all I have tried here, because I could write a book with it. You got the best I have found so far. I got the error message from Fiddler. Here goes the code:

Javascript:

    function CallService() {
        var xdr;

        function err() {
            alert('Error');
        }
        function timeo() {
            alert('Time off');
        }
        function loadd() {
            alert('Response: ' +xdr.responseText);
        }
        function stopdata() {
            xdr.abort();
        }   
        xdr = new XDomainRequest();
        if (xdr) {
            xdr.onerror = err;
            xdr.ontimeout = timeo;
            xdr.onload = loadd;
            xdr.timeout = 10000;
            xdr.open('POST','http://localhost/Path/Service.svc/AjaxEndpoint' + '/FunctionName');
            xdr.send()
        } else {
            alert('XDR undefined');
        }             
    }

Service (vb.net)

<ServiceContract()>
Public Interface IService

    <OperationContract()> _
    <WebInvoke(Method:="POST")> _
    Function FunctionName() As String

End Interface

Public Class Service
    Implements IService

    Public Function FunctionName() As String Implements IService.FunctionName
        Return "test"
    End Function
End Class

Public Class CustomContentTypeMapper
    Inherits WebContentTypeMapper
    Public Overrides Function GetMessageFormatForContentType(contentType As String) As WebContentFormat
        If contentType = "application/octet-stream" Then
            Return WebContentFormat.Json
        Else
            Return WebContentFormat.Default
        End If
    End Function
End Class

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="AppName.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    <!--Generate via 16 caractères http://ift.tt/1G9b55h -->
    <add key="envRnd" value="w6pgpNTDke3fpAd1" />
  </appSettings>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="CustomMapper">
          <webMessageEncoding webContentTypeMapperType="AppName.CustomContentTypeMapper, AppName" />
          <httpTransport manualAddressing="true" />
        </binding>
      </customBinding>
      <webHttpBinding>
        <binding name="AjaxBinding"/>
      </webHttpBinding>
    </bindings>
    <client>
    <behaviors>
      <endpointBehaviors>
        <behavior name="AjaxBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
          <behavior name="ServiceBehaviors">
            <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
            <serviceDebug includeExceptionDetailInFaults="true"/>
          </behavior>
        </serviceBehaviors>
    </behaviors>
    <services>
      <service name="AppName.Service" behaviorConfiguration="ServiceBehaviors" >
        <endpoint address="AjaxEndpoint" behaviorConfiguration="AjaxBehavior" binding="customBinding"
            contract="AppName.IService" bindingConfiguration="CustomMapper"/>
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <!--<httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*"/>
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
      </customHeaders>
    </httpProtocol>-->
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>

  <location path="Service.svc">
    <system.web>
      <authorization>
        <allow roles="prd" />
        <deny users="*" />
      </authorization>
    </system.web>
  </location>
  <applicationSettings>    
  </applicationSettings>
</configuration>

EDIT : I looked at both httperr1.log and files in inetpub\logs\LogFiles\W3SVC1 but none would give more informations than Fiddler that have been the best tool so far. I still don't have a clue.

Aucun commentaire:

Enregistrer un commentaire