繁体   English   中英

将文件发送到WCF时出错:请求失败,HTTP状态为400:错误的请求

[英]Error while sending file to WCF: The request failed with HTTP status 400: Bad Request

我正在尝试使用wcf服务上传文件。 我正在获取The request failed with HTTP status 400: Bad Request位于:

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

应用配置文件:

<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配置:

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

可能有些情况导致了您所看到的错误。 我建议的第一件事是将创建的绑定配置分配给显式定义的端点。 像这样:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM