如何解决 WCF 中的 400 错误请求错误 [英] How to resolve 400 bad request error in WCF

查看:36
本文介绍了如何解决 WCF 中的 400 错误请求错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 WCF 的大佬.我做了一个简单的 WCF 服务和一个客户端来上传文件.但是当我上传超过 100KB 的文件时,我收到了 400 个错误的请求.我在互联网上搜索并找到了一些关于修改 max*Length 或 max**size 的解决方案.但我还在挣扎.

I'm a WCF biginner. I made a simple WCF Service and a client to upload file. But I got 400 bad request when uploading more than around 100KB. I searched in internet and found some resolution about modifying max*Length or max**size. But I'm still struggling.

所以,我想请教专家如何解决这个问题.

So, I'd like to ask experts how to resolve the problem.

服务代码在这里.

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

谢谢.

推荐答案

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

You are very close to the solution, but you are mixing the bindings. Your service is using basicHttpBinding, but you have set the size limits on webHttpBinding.

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

Therefore: In your web.config for the service, replace webHttpBinding with basicHttpBinding and this will work, like this:

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

这篇关于如何解决 WCF 中的 400 错误请求错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆