2012-04-18 40 views
7

Soy un gran pensador WCF. Hice un servicio WCF simple y un cliente para subir archivos. Pero recibí 400 solicitudes incorrectas cuando subía más de 100 KB. Busqué en Internet y encontré alguna resolución sobre la modificación de max * Longitud o max * * tamaño. Pero todavía estoy luchando.Cómo resolver 400 error de solicitud incorrecta en WCF

Entonces, me gustaría preguntar a los expertos cómo resolver el problema.

El código de servicio está aquí.

[ServiceContract] 
public interface IService1 
{ 

    [OperationContract] 
    void SaveFile(UploadFile uploadFile); 
} 


[DataContract] 
public class UploadFile 
{ 
    [DataMember] 
    public string FileName { get; set; } 

    [DataMember] 
    public byte[] File { get; set; } 
} 


public class Service1 : IService1 
{ 

    public void SaveFile(UploadFile uploadFile) 
    { 
     string str = uploadFile.FileName; 
     byte[] data = uploadFile.File; 
    } 
} 

La configuración del servicio está aquí.

<?xml version="1.0"?> 
<configuration> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    <httpRuntime maxRequestLength="64000000"/> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceMetadata httpGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    <bindings> 
     <webHttpBinding> 
     <binding maxBufferSize="64000000" maxReceivedMessageSize="64000000" maxBufferPoolSize="64000000"> 
      <readerQuotas maxDepth="64000000" maxStringContentLength="64000000" maxArrayLength="64000000" maxBytesPerRead="64000000" /> 
      <security mode="None"/> 
     </binding> 
     </webHttpBinding> 
    </bindings> 
    </system.serviceModel> 

<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 
</configuration> 

Código de cliente está aquí.

private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    FileInfo info = new FileInfo(@"C:\Users\shingotada\Desktop\4.png"); 

    byte[] buf = new byte[32768]; 
    Stream stream = info.OpenRead(); 
    byte[] result; 

    using (MemoryStream ms = new MemoryStream()) 
    { 
     while (true) 
     { 
      int read = stream.Read(buf, 0, buf.Length); 
      if (read > 0) 
      { 
       ms.Write(buf, 0, read); 
      } 
      else 
      { 
       break; 
      } 
     } 
     result = ms.ToArray(); 
    } 

     UploadFile file = new UploadFile(); 
     file.File = result; 
     file.FileName = "test"; 

     ServiceReference2.Service1Client proxy2 = new ServiceReference2.Service1Client(); 
     proxy2.SaveFile(file); //400 bad request 
} 

configuración del cliente está aquí.

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
     <bindings> 
      <basicHttpBinding> 
       <binding name="BasicHttpBinding_IService1" closeTimeout="00:01:00" 
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" 
        allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
        maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
        messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
        useDefaultWebProxy="true"> 
        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="64000000" 
         maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
        <security mode="None"> 
         <transport clientCredentialType="None" proxyCredentialType="None" 
          realm="" /> 
         <message clientCredentialType="UserName" algorithmSuite="Default" /> 
        </security> 
       </binding> 
      </basicHttpBinding> 
     </bindings> 
     <client> 
      <endpoint address="http://localhost:53635/Service1.svc" binding="basicHttpBinding" 
       bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference2.IService1" 
       name="BasicHttpBinding_IService1" /> 
     </client> 
    </system.serviceModel> 
</configuration> 

Thanks.

Respuesta

9

Estás muy cerca de la solución, pero estás mezclando las fijaciones. Su servicio está utilizando basicHttpBinding, pero ha establecido los límites de tamaño en webHttpBinding.

Por lo tanto: En el web.config para el servicio, cambie webHttpBinding con basicHttpBinding y esto va a funcionar, así:

<basicHttpBinding> 
    <binding maxBufferSize="64000000" maxReceivedMessageSize="64000000" maxBufferPoolSize="64000000"> 
     <readerQuotas maxDepth="64000000" maxStringContentLength="64000000" maxArrayLength="64000000" maxBytesPerRead="64000000" /> 
     <security mode="None"/> 
    </binding> 
    </basicHttpBinding> 
+0

Así coool !! Funcionó. Hice un tonto error. Gracias tu consejo –

Cuestiones relacionadas