简体   繁体   中英

Configure AJAX enabled WCF Service in WebApp to receive large data

Using Visual Studio 2010 and .NET framework 4.0 I have built a simple HelloWorld WCF service that is AJAX enabled based on the sample here: http://msdn.microsoft.com/en-us/library/bb924552.asp

However I need to pass a large amount of data to the service so I added a text area and another button to do that. Unfortunately I get a 400 error (bad request) from the server when I post more than a few hundred lines of text.

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>

I've removed the entire entry from the web config file and it works as described above. I've been having a lot of trouble with the web.config file and trying out different settings suggested on the web. some have no apparent effect, while others cause a 'service is undefined' error in the Javascript, so getting the config 'just right' is proving to be a problem.

How can I configure the service to accept large amounts of data? Secondly, for provided answers, what effect does a particular setting actually have in the real world?

Note that jquery is not an option.

Thanks

我最终通过从头开始重做配置解决了这个问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM