使用Visual Studio 2010和.NET framework 4.0我已根据此处的示例构建了一个基于AJAX的简单HelloWorld WCF服务: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
HelloWorld.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.config文件遇到很多麻烦,并尝试在网络上建议的不同设置。一些没有明显的效果,而另一些则导致Javascript中的“服务未定义”错误,因此将配置“恰到好处”证明是一个问题。
如何配置服务以接受大量数据?其次,对于提供的答案,特定设置在现实世界中实际有什么影响?
请注意,jquery不是一个选项。
由于
答案 0 :(得分:1)
我最终通过从头开始重做配置来解决这个问题。