简体   繁体   English

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

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

I am trying to upload file using wcf service. 我正在尝试使用wcf服务上传文件。 I am getting The request failed with HTTP status 400: Bad Request at: 我正在获取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: 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; }
}

APPLICATION: 应用:

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

Apllication config file: 应用配置文件:

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

There could be a few things that are resulting in the error you are seeing. 可能有些情况导致了您所看到的错误。 The first thing I would recommend is that you assign the binding configurations you created to an explicitly defined endpoint. 我建议的第一件事是将创建的绑定配置分配给显式定义的端点。 Something like this: 像这样:

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

The above is the configuration for the service. 以上是该服务的配置。 Make sure you fully qualify the contract name with the namespace, and the service name needs to be the same as the name in the markup of the .svc file. 确保使用名称空间完全限定合同名称,并且服务名称必须与.svc文件标记中的名称相同。

You would do something similar for the client, except it would be the <client> tag, rather than <service> . 您将为客户端执行类似的操作,除了它是<client>标记而不是<service>

If you don't specify the binding configuration to use in the endpoint, then WCF will use the default (lower) values for the binding type you selected. 如果未指定在端点中使用的绑定配置,则WCF将为您选择的绑定类型使用默认(较低)值。 An alternative is to make the binding configuration the default for that kind of binding, by omitting the name attribute. 另一种选择是通过省略name属性,使绑定配置成为该绑定的默认配置。

If this doesn't solve the issue, you can also try adjusting the maxRequestLength value for the <httpRuntime> element in <system.web> . 如果这不能解决问题,您还可以尝试调整<system.web> <httpRuntime>元素的maxRequestLength值。 <system.web> is a child of <configuration> : <system.web><configuration>的子代:

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

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

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