如何在WCF中解决400错误请求错误

时间:2012-04-18 16:14:00

标签: wcf

我是一名WCF的重要人物。 我制作了一个简单的WCF服务和一个上传文件的客户端。但是当上传超过100KB时,我得到了400个错误的请求。 我在互联网上搜索并找到了一些关于修改max * 长度或最大* *大小的决议。但我还在苦苦挣扎。

所以,我想问专家如何解决问题。

服务代码在这里。

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

服务配置就在这里。

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

客户端代码在这里。

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
}

客户端配置就在这里。

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

感谢。

1 个答案:

答案 0 :(得分:9)

您非常接近解决方案,但是您正在混合绑定。您的服务使用basicHttpBinding,但您已在webHttpBinding上设置了大小限制。

因此:在您的web.config服务中,将webHttpBinding替换为basicHttpBinding,这将起作用,如下所示:

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