2015-05-15 68 views
0

我試圖使用wcf服務上傳文件。發送文件到WCF時出錯:請求失敗,HTTP狀態爲400:錯誤的請求

ds = WcfSpend.RegisterSupplier("blah", new SpendWcfRef.FileData 
{ 
    FileName = myFile.FileName.ToString(), 
    BufferData = file, 
    FilePosition = 1 
}); 

WCF:

public DataSet RegisterSupplier(string SupplierId, FileData file) 
{ 
    DataSet ds = new DataSet(); 
    return ds; 
} 

[ServiceContract] 
public interface ISPEND 
{ 

    [OperationContract] 
    DataSet executeProcedure(string procedurename, string[] paramsName, string[] paramsValue, int num); 

    [OperationContract] 
    DataSet RegisterSupplier(string SupplierId, FileData file); 

    //[OperationContract] 
    //bool UploadFileData(FileData fileData); 
} 

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

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

    [DataMember] 
    public int FilePosition { get; set; } 
} 

應用:

ds = WcfSpend.RegisterSupplier("blah", new SpendWcfRef.FileData 
{ 
    FileName = myFile.FileName.ToString(), 
    BufferData = file, FilePosition = 1 
}); 

Apllication配置文件:

<system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
      <binding name="BasicHttpBinding_ISPEND" 
       closeTimeout="00:01:00"  
       openTimeout="00:01:00" 
       receiveTimeout="00:10:00" 
       sendTimeout="00:01:00" 
       allowCookies="false" 
       bypassProxyOnLocal="false" 
       hostNameComparisonMode="StrongWildcard" 
       maxBufferSize="2147483647" 
       maxBufferPoolSize="2147483647" 
       maxReceivedMessageSize="2147483647" 
       messageEncoding="Text" 
       textEncoding="utf-8" 
       transferMode="Buffered" 
       useDefaultWebProxy="true"> 
       <readerQuotas maxDepth="2147483647" 
        maxStringContentLength="2147483647" 
        maxArrayLength="2147483647" 
        maxBytesPerRead="2147483647" 
        maxNameTableCharCount="2147483647" /> 
       <security mode="None"> 
        <transport clientCredentialType="None" 
         proxyCredentialType="None" 
         realm="" /> 
        <message clientCredentialType="UserName" 
         algorithmSuite="Default" /> 
       </security> 
      </binding> 
     </basicHttpBinding> 
    </bindings> 
    <client/> 
</system.serviceModel> 

WCF的web配置:

我在得到 The request failed with HTTP status 400: Bad Request
<system.serviceModel> 
    <bindings> 
     <basicHttpBinding> 
      <binding name="myBindingForBigArrays" 
       openTimeout="00:10:00" 
       closeTimeout="00:10:00" 
       receiveTimeout="00:10:00" 
       sendTimeout="00:10:00" 
       maxReceivedMessageSize="2147483647" 
       maxBufferPoolSize="2147483647" 
       maxBufferSize="2147483647"> 
       <readerQuotas maxDepth="64" 
        maxStringContentLength="2147483647" 
        maxArrayLength="2147483647" 
        maxBytesPerRead="4096" 
        maxNameTableCharCount="16384"/> 
      </binding> 
     </basicHttpBinding> 
    </bindings> 
    <behaviors> 
     <serviceBehaviors> 
      <behavior> 
       <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
       <serviceMetadata httpGetEnabled="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="false"/> 
      </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
</system.serviceModel> 
+0

請添加一個實際的問題,一些更詳細的解釋你想要做什麼。 – Aaron

+0

@Aaron我得到'請求失敗,HTTP狀態400:錯誤的請求'在哪裏我在我的應用程序中調用wcf方法。我正在嘗試將文件發送給wcf。 – Arbaaz

+0

雖然你似乎要返回一個空的'DataSet',它可能與大小有關。您已經爲客戶端和具有較大值的服務定義了綁定,但是它們沒有被使用,因爲它們沒有通過'endpoint'元素上的'bindingConfiguration'屬性分配給端點,除非您正在執行此操作在代碼中你沒有顯示。 – Tim

回答

0

可能有幾件事情導致您看到的錯誤。我建議的第一件事是將您創建的綁定配置分配給明確定義的端點。類似這樣的:

<!-- This goes in the <system.serviceModel> section --> 
<services> 
    <service name="MyService"> 
    <endpoint address="" 
       binding="wsHttpBinding" 
       bindingConfiguration="myBindingForBigArrays" 
       contract="<namespace>.ISpend" /> 
    </service> 
</services> 

以上是服務配置。確保您完全符合名稱空間的合同名稱,並且服務名稱需要與.svc文件標記中的名稱相同。

你會爲客戶做類似的事情,除了它是<client>標記,而不是<service>

如果您沒有指定要在端點中使用的綁定配置,那麼WCF將使用您選擇的綁定類型的默認(較低)值。另一種方法是通過省略name屬性將綁定配置設置爲該類綁定的默認設置。

如果這不能解決問題,您還可以嘗試調整<system.web>中的<httpRuntime>元素的maxRequestLength值。 <system.web><configuration>孩子:

<system.web> 
    <httpRuntime maxRequestLength="2147483647" /> 
</system.web> 
+0

我的應用程序配置看起來像現在'<系統。serviceModel> <客戶機名稱= 「爲MyService」> <端點地址= 「S」 結合= 「的wsHttpBinding」 bindingConfiguration = 「myBindingForBigArrays」 合同= 「SpendWcfRef.ISpend」/> ' 它說元素服務具有無效子元素 – Arbaaz

+0

@Arbaaz - ''是'',就像''一個孩子。換句話說,你不要把''部分放在''部分下面。 – Tim

+0

我覺得很愚蠢。我在地址=「」中輸入了什麼?這是我的服務路徑:'http:// localhost:60537/SPEND_WCF/Service.svc' – Arbaaz

相關問題