那里有很多类似的问题,但我已经尝试过每一个解决方案都无济于事.
我们有一个使用WebServiceHostFactory初始化的Web服务,但是如果它被抛出超过64k,我们会收到'400 Bad Request'.通常情况下,这可以通过提升MaxReceivedMessageSize,MaxBufferSize和MaxBufferPoolSize来解决.问题是使用WebServiceHostFactory,Web.Config被完全忽略.我在ServiceModel部分中所做的任何更改都不会反映在服务中.
完全抛弃WebServiceHostFactory并从头开始设置web.config会很不错,但如果没有它,我们的服务将无法运行.其中一个方法有一个stream参数以及一些其他字符串参数.没有工厂,我们得到
System.InvalidOperationException: For request in operation Test to be a stream the operation must have a single parameter whose type is Stream
Run Code Online (Sandbox Code Playgroud)
所以不能选择拆除工厂.我无法确切地知道工厂正在做什么修复了这个错误,但我花了4天时间就没有了.
我也试过以编程方式覆盖MaxReceivedMessageSize,我在附近发现了一些例子:
protected override void OnOpening()
{
base.OnOpening();
foreach (var endpoint in Description.Endpoints)
{
//var binding = endpoint.Binding as WebHttpBinding;
//if (binding != null)
//{
// binding.MaxReceivedMessageSize = 20000000;
// binding.MaxBufferSize = 20000000;
// binding.MaxBufferPoolSize = 20000000;
// binding.ReaderQuotas.MaxArrayLength = 200000000;
// binding.ReaderQuotas.MaxStringContentLength = 200000000;
// binding.ReaderQuotas.MaxDepth = 32;
//}
//var transport = endpoint.Binding.CreateBindingElements().Find<HttpTransportBindingElement>(); …Run Code Online (Sandbox Code Playgroud) 所以,我一直在搞 webservices 一段时间,我一直回到一些基础知识,我似乎永远不会正确。
在 .NET/C# 中使用 WebServiceHost 时,您可以使用 GET/POST/etc 定义方法/端点。设置一个 GET 方法很容易,而且它的工作方式非常直接,而且很容易理解它是如何工作的。例如:
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/PutMessage/{jsonString}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
string PutMessage(string jsonString);
Run Code Online (Sandbox Code Playgroud)
如果我调用 http:///MyWebService/PutMessage/{MyJsonString} 我会通过该方法,并且一切都很好(或多或少)。
但是,当我将其定义为POST时,这意味着什么?
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/PutMessage/{jsonString}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
string PutMessage(string jsonString);
Run Code Online (Sandbox Code Playgroud)
UriTemplate 在这里做什么?如果我执行 POST,我希望数据不包含在 URI 中,而是包含在帖子的“数据部分”中。但是我是否在数据部分定义了变量名?WebServiceHost/.NET 如何知道帖子的“数据部分”中包含的内容要放入变量 jsonString 中?我如何从客户端(不是 C#,我们说 JQuery)发布数据,以便在服务器端正确解释它?
(WebMessageFormat 是如何影响事物的?我到处都读过这方面的内容(MSDN、Stackoverflow 等),但还没有找到明确而好的答案。)
在我试图理解这一点时,我想我会制作一个非常简单的 POST 方法,如下所示:
[OperationContract]
[WebInvoke]
string PutJSONRequest(string …Run Code Online (Sandbox Code Playgroud)