WebServiceHost大输入\输出字符串

时间:2013-06-10 11:12:55

标签: c# wcf web-services

服务器代码:

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”。 完全相同的代码,只有一个小的字符串输入工作很好,所以...... 如何使这个代码与更大的输入字符串一起工作?

1 个答案:

答案 0 :(得分:0)

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

使用ServiceHost WebHttpBinding来解决问题。

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

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

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

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

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