开发者

How to make wcf service keep the data between ajax calls?

开发者 https://www.devze.com 2023-02-28 19:57 出处:网络
Suppose that I have simple WCF Service: [ServiceContract] public interface ITestService { [OperationContract]

Suppose that I have simple WCF Service:

[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";
    }
}

and the Html page that performs two ajax calls to that service:

<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>

If I call SetStatus first(by typing something in txtSetStatus input and clicking Send status button), then call GetStatus(by clicking Get Current Status button), then the returned status is null. I understand that this happens because WCF service instance is created each time when a html page performs ajax request. But what is the best practice to keep the data between 2 or more ajax calls?


Well, I've found the answer. It seems that the best way is setting aspNetCompatibilityEnabled=true for my WCF service and then making use of asp.net Session object in order to keep the values temporarily:

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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消