【问题标题】:WCFs WebServiceHostFactory and timersWCF WebServiceHostFactory 和计时器
【发布时间】:2011-08-10 07:12:05
【问题描述】:

我正在使用来自this example 的 JSON 和 XML 创建 AJAX 服务。 在 service.cs 中我进行了更改:

    [ServiceContract(Namespace = "XmlAjaxService")]
        public interface ICalculator
        {
            ...

            [WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
            int GetTimersCallCount();
        }

public class CalculatorService : ICalculator
    {
        private System.Timers.Timer timer = null;
        private int timerCalls = 0;

        public CalculatorService()
        {
            timer = new System.Timers.Timer();
            timer.Interval = 1000;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer.Start();
        }

        public int GetTimersCallCount()
        {
            return this.timerCalls;
        }
}

在页面 javascript 我这样做:

function GetTimersTick() {

        // Create HTTP request
        var xmlHttp;
        try {
            xmlHttp = new XMLHttpRequest();
        } catch (e) {
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    alert("This sample only works in browsers with AJAX support");
                    return false;
                }
            }
        }

        // Create result handler
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                document.getElementById("result").value = xmlHttp.responseText;
                document.getElementById("statustext").value = xmlHttp.getAllResponseHeaders();
            }
        }


        // Build the operation URL
            var url = "service.svc/";
            url = url + "GetTimersCallCount";
            xmlHttp.open("POST", url, true);
            xmlHttp.setRequestHeader("Content-type", "application/json");
            xmlHttp.send();
    }

但是当我按下这个功能的按钮时,我是从服务 0 得到的。出了什么问题?

【问题讨论】:

  • 据我从您的示例代码中可以看出,您永远不会更新 timerCalls
  • 这是更新我想在这里显示此代码

标签: c# ajax wcf json service


【解决方案1】:

原因是您的CalculatorService 类没有一个实例。每次新调用服务时都会重新创建它。

要启用单实例模式,请在您的服务实现类上使用以下属性:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class CalculatorService : ICalculator { ... }

请注意,在使用单实例模式时,您可能需要同步对服务方法的调用。在您的具体示例中,这不是必需的,因为您只是读写一个 32 位整数(这是一个原子操作)。

【讨论】:

  • 谢谢罗纳德提出正确的问题。其次,在我的服务中,我想从 OPC 获取数据并将其发送给客户端?在这个例子中。我想每 1-5 秒从 OPC 服务器接收数据并将数据存储在 IList 中。当客户端请求我的服务方法时,我的服务方法返回 Json 或 Xml 中的数据 IList。谢谢
  • 如果我不更新页面或几分钟(10 或 15)后不发送请求,如果我发送请求服务返回 0。我在哪里更改此时间设置?谢谢。
  • 我不会在服务类中设置这种逻辑,而是在单独的组件中设置。该组件每 5 秒轮询一次 OPC 服务器,并将结果存储在数据库中。然后 WCF 服务从数据库中读取数据并将结果发送给客户端。
  • 以及如何控制这个类在第一次请求中创建的生命时间?
  • 我想您是从 Web 应用程序 (IIS) 中运行它的?您可以为此使用Application_Start
猜你喜欢
  • 1970-01-01
  • 2011-07-09
  • 1970-01-01
  • 2010-10-09
  • 1970-01-01
  • 2012-01-13
  • 1970-01-01
  • 1970-01-01
  • 2011-06-04
相关资源
最近更新 更多