使用 Visual Studio 2010 和 .NET 框架 4.0,我构建了一个简单的 HelloWorld WCF 服务,该服务基于此处的示例启用了 AJAX:http: //msdn.microsoft.com/en-us/library/bb924552.asp
但是我需要将大量数据传递给服务,所以我添加了一个文本区域和另一个按钮来执行此操作。不幸的是,当我发布超过几百行文本时,我从服务器收到 400 错误(错误请求)。
HelloWorldService.svc:
<%@ ServiceHost Language="VB" Debug="true" Service="HelloWorldAjaxWCFServiceWebApp.HelloWorldService"
CodeBehind="HelloWorldService.svc.vb" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>
HelloWorldService.svc.vb:
Imports System.ServiceModel
Imports System.ServiceModel.Activation
Imports System.ServiceModel.Web
<ServiceContract(Namespace:="HelloWorldAjaxWCFServiceWebApp")>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class HelloWorldService
<OperationContract()>
Public Function Echo(ByVal data As String) As String
Return data
End Function
<OperationContract()>
<WebInvoke(Method:="POST")>
Public Function HelloWorld(ByVal Name As String) As String
If Name.Length > 100 Then
Return "Hello " & Name.Substring(0, 100)
Else
Return "Hello " & Name
End If
End Function
End Class
你好世界.aspx:
<%@ Page Language="VB" AutoEventWireup="true" CodeBehind="HelloWorld.aspx.vb" Inherits="HelloWorldAjaxWCFServiceWebApp.HelloWorld" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="HelloWorldService.svc" />
</Services>
</asp:ScriptManager>
</form>
<p>
<input id="txtHello" type="text" value="Your Name Here" />
<input id="btnHello" type="button" value="Hello World" onclick="return btnHello_onclick()" /></p>
<p>
<textarea id="txtLong" name="S1" rows="50" cols="50"></textarea>
<input id="btnLong" type="button" value="Send Long Text"
onclick="return btnLong_onclick()" /></p>
<div id="Results"></div>
<script language="javascript" type="text/javascript">
// <!CDATA[
function btnHello_onclick() {
var txt = document.getElementById('txtHello');
var service = new HelloWorldAjaxWCFServiceWebApp.HelloWorldService();
//service.Echo('echo', onSuccess, FailedCallback, null);
service.HelloWorld(txt.value, onSuccess, FailedCallback, null);
}
function btnLong_onclick() {
var txt = document.getElementById('txtLong');
var service = new HelloWorldAjaxWCFServiceWebApp.HelloWorldService();
//service.Echo('echo', onSuccess, FailedCallback, null);
service.HelloWorld(txt.value, onSuccess, FailedCallback, null);
}
function onSuccess(result) {
alert(result);
}
// This is the failed callback function.
function FailedCallback(error) {
var stackTrace = error.get_stackTrace();
var message = error.get_message();
var statusCode = error.get_statusCode();
var exceptionType = error.get_exceptionType();
var timedout = error.get_timedOut();
// Display the error.
var results = document.getElementById("Results");
results.innerHTML =
"Stack Trace: " + stackTrace + "<br/>" +
"Service Error: " + message + "<br/>" +
"Status Code: " + statusCode + "<br/>" +
"Exception Type: " + exceptionType + "<br/>" +
"Timedout: " + timedout;
}
// ]]>
</script>
</body>
</html>
我已经从 web 配置文件中删除了整个条目,它的工作原理如上所述。我在使用 web.config 文件时遇到了很多麻烦,并尝试了网络上建议的不同设置。有些没有明显的效果,而另一些则在 Javascript 中导致“服务未定义”错误,因此使配置“恰到好处”被证明是一个问题。
如何配置服务以接受大量数据?其次,对于提供的答案,特定设置在现实世界中实际上有什么影响?
请注意,jquery 不是一个选项。
谢谢