2012-04-18 40 views
7

Tôi là một WCF biginner. Tôi đã thực hiện một Dịch vụ WCF đơn giản và một khách hàng để tải lên tệp. Nhưng tôi nhận được 400 yêu cầu xấu khi tải lên hơn 100KB. Tôi đã tìm kiếm trên internet và tìm thấy một số giải pháp về sửa đổi kích thước tối đa * Chiều dài hoặc tối đa * * kích thước. Nhưng tôi vẫn đang vật lộn.Cách giải quyết 400 lỗi yêu cầu không hợp lệ trong WCF

Vì vậy, tôi muốn hỏi các chuyên gia cách giải quyết vấn đề.

Mã dịch vụ có tại đây.

[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; 
    } 
} 

Cấu hình dịch vụ có tại đây.

<?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> 

Mã khách hàng ở đây.

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 
} 

cấu hình máy khách ở đây.

<?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> 

Cảm ơn.

Trả lời

9

Bạn đang rất gần với giải pháp, nhưng bạn đang trộn các ràng buộc. Dịch vụ của bạn đang sử dụng basicHttpBinding, nhưng bạn đã đặt giới hạn kích thước trên webHttpBinding.

Do đó: Trong web.config của bạn cho dịch vụ, thay thế webHttpBinding với basicHttpBinding và điều này sẽ làm việc, như thế này:

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

Vì vậy coool !! Nó đã làm việc. Tôi đã mắc sai lầm ngớ ngẩn. Cảm ơn lời khuyên của bạn. –

Các vấn đề liên quan