1

服务器代码:

WebServiceHost w = new WebServiceHost(typeof(WebHost), new Uri("http://localhost/Host");
BasicHttpBinding binding = new BasicHttpBinding();
binding.MaxBufferPoolSize = 2147483647;
binding.MaxBufferSize = 2147483647;
binding.MaxReceivedMessageSize = 2147483647;
binding.ReaderQuotas = new XmlDictionaryReaderQuotas { MaxStringContentLength = 2147483647 };
w.AddServiceEndpoint(typeof(WebHost), binding, "http://localhost/Host");
w.Open();


[ServiceContract]
public class HEWebHost
{
    [OperationContract]
    [WebInvoke(UriTemplate = "Host")]
    public string Host(string largeRequest)
    {
    // ... Some code
    }
}

客户代码:

HttpWebRequest request = HttpWebRequest.Create("http://localhost/Host") as HttpWebRequest;
request.Method = "POST";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(largeRequestString);
writer.Flush();
writer.Close();
writer.Dispose();

HttpWebResponse response = request.GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(response.GetResponseStream());
string output = reader.ReadToEnd();

即使我设置了绑定对象的 MaxReceivedMessageSize,我仍然得到“400 Bad Request”。完全相同的代码,只有一个小字符串输入才能很好地工作,所以......我怎样才能让这个代码在更大的输入字符串中工作?

4

1 回答 1

1

你的代码有问题。您正在使用WebServiceHost(WCF REST 编程模型)和 basicHttpbinding(WCF SOAP 编程模型)。您不能混合使用这两种方法。

使用 ServiceHostWebHttpBinding 更正此问题。

另外,请注意,对于 WCF REST 样式绑定,您需要确保 IIS 可以支持更大的传输 - 默认情况下为 4096 (4 MB)

检查你的 web.config - 你有这样的条目吗?

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

即使我设置了绑定对象的 MaxReceivedMessageSize,我仍然得到“400 Bad Request”

这只是一个“糟糕的要求”。检查 WCF 服务器日志以获取确切的错误消息。

于 2013-06-10T12:25:02.807 回答