如何让wcf服务保持ajax调用之间的数据?

时间:2011-04-23 13:35:39

标签: jquery ajax wcf

假设我有简单的WCF服务:

[ServiceContract]   
public interface ITestService
{
    [OperationContract]
    [WebInvoke(
        Method = "POST",
        BodyStyle = WebMessageBodyStyle.Wrapped,
        ResponseFormat = WebMessageFormat.Json)]
    string GetStatus();

    [OperationContract]
    [WebInvoke(
        Method = "POST",
        BodyStyle = WebMessageBodyStyle.Wrapped,
        ResponseFormat = WebMessageFormat.Json)]
    string SetStatus(string status);    
}

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class TestService : ITestService
{
    private string _status;
    public string GetStatus()
    {
        return _status;
    }
    public string SetStatus(string status)
    {
        _status = status;
        return "completed";
    }
}

以及对该服务执行两次ajax调用的Html页面:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Wcf services test</title>
    <script type="text/javascript" src="Scripts/jquery-1.5.1.min.js"></script>
    <script language="javascript" type="text/javascript">
        function btnSend_onclick() {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Services/TestService.svc/SetStatus",
                data:'{"status":"'+jQuery("#txtSetStatus").val()+'"}',
                processData: false,
                dataType: "json",
                //If the call succeeds
                success:
        function (response) {
                jQuery("#lblInfo").text('Service returned: ' + response.SetStatusResult);
        },
                //If the call fails
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    jQuery("#lblInfo").text(XMLHttpRequest.responseText);
                }
            });

        }
        function btnGetStatus_onclick() {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Services/TestService.svc/GetStatus",
                processData: false,
                dataType: "json",
                //If the call succeeds
                success:
        function (response) {
            jQuery("#lblGetStatus").text('Status is: ' + response.GetStatusResult);
        },
                //If the call fails
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    jQuery("#lblInfo").text(XMLHttpRequest.responseText);
                }
            });

        }

    </script>
</head>
<body>
<input type="text" id="txtSetStatus" />
<button id="btnSend" onclick="return btnSend_onclick()">Send status</button>
<span id="lblStatus"></span>
<span id="lblInfo"></span>
<br />
<button id="btnGetStatus" onclick="return btnGetStatus_onclick()">Get Current Status</button>
<span id="lblGetStatus"></span>
</body>
</html>

如果我先调用SetStatus(通过在txtSetStatus输入中输入内容并单击Send status按钮),然后调用GetStatus(通过单击Get Current Status按钮),则返回的状态为null。我知道这是因为每次html页面执行ajax请求时都会创建WCF服务实例。 但是,将数据保存在两个或更多ajax调用之间的最佳做法是什么?

1 个答案:

答案 0 :(得分:0)

好吧,我找到了答案。似乎最好的方法是为我的WCF服务设置aspNetCompatibilityEnabled = true,然后使用asp.net Session对象来暂时保留值:

public class TestService : ITestService
{
    public string GetStatus(string id)
    {
        var session = System.Web.HttpContext.Current.Session;
        var ret= session[id].ToString();
        session.Remove(id);
        return ret;
    }
    public string SetStatus(string status)
    {
        var guid = Guid.NewGuid().ToString("D");
        var session = System.Web.HttpContext.Current.Session;
        session.Add(guid, status);
        return guid;
    }
}