2015-05-15 50 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

相关问题