ASP.NET静态变量的生命周期(Lifetime of ASP.NET Static Variable)
我拿在页面类中定义(未在Global.asax中)静态变量的一些信息。 我只在代码中声明变量,如:
protected static int SomeGlobalUnsecureID; protected static string SomeGlobalUnsecureString;
并在PageLoad事件中定义变量。 例如,我从数据库中检查ID,如果与SomeGlobalUnsecureID不同,我从其他地方更新SomeGlobalUnsecureID和String,否则保留原样。 这在我的应用程序是完全安全的 逻辑(即这些数据不安全,每个人都可以访问它们,没有问题); 只有我想要完成的是
保持相同的内存量,无论用户连接
更改当且仅当持久性信息与“内存”中的信息不同时(因为实际读取字符串对我而言是耗时的。
现在,由于我在PageLoad中进行检查,所以在重新加载的页面中没有问题。 但是我的页面已经满了WebMethods,有时我看到静态变量被归零。 奇怪的部分是 即使静态变量为零,会话仍然处于活动状态(so-> no server或app。pool restart等)
这对我来说很奇怪 我假设静态变量将保持其值,直到应用程序(以某种方式)结束。 但即使Session没有到期,静态变量也被清零。 你建议什么 使用应用程序变量更好的选择? 我在网上阅读的所有文档都提供静态变量而不是应用程序变量,我需要声明它们有什么不同吗?
I am holding some information in static variables defined in page class (not in Global.asax). I only declare variable in code like:
protected static int SomeGlobalUnsecureID; protected static string SomeGlobalUnsecureString;
and define the variable in PageLoad event. For example, I check ID from the database, if it's different from SomeGlobalUnsecureID, I update SomeGlobalUnsecureID and String from somewhere else, otherwise leave them as is. This is perfectly safe in my app. logic (i.e. those data are not secure, everybody can access them, no problem); only thing I want to accomplish is
Hold the same amount of memory regardless of users connected
Change if and only if persistent info is different from the one in 'memory' (because actually reading the string is time consuming for me.
Now, since I make the check in PageLoad, I have no problems in reloaded pages. However my page is full of WebMethods, and sometimes I see that the static variables are zeroed. And the strange part is; the session is still active even when the static variables are zeroed (so-> no server or app. pool restart etc.)
This is really strange for me. I assume that static variable will hold its value until the application (somehow) ends. But even the Session did not expire, the static variable is zeroed. What do you suggest? Is using application variables a better choice? All documents I've read on web suggest static variables instead of application variables, do I need to declare them somehow different?
原文:https://stackoverflow.com/questions/8919095
最满意答案
你在客户端和服务器上都使用192.xxx吗? 我记得之前看到一个问题,在这个问题中,TCP需要匹配客户端和服务器名称(与某个消息属性相关的东西),所以如果您使用“localhost”定义服务并且客户端有问题。
Do you use 192.x.x.x on both client and server? I remember seeing an issue a while back in which for TCP the client and server names needed to match (something related to one of the message properties), so if you define the service with "localhost" and the client with there would be a problem.
相关问答
更多-
WCF无法连接,因为目标机器主动拒绝(WCF No connection could be made because the target machine actively refused)[2023-03-13]
你在客户端和服务器上都使用192.xxx吗? 我记得之前看到一个问题,在这个问题中,TCP需要匹配客户端和服务器名称(与某个消息属性相关的东西),所以如果您使用“localhost”定义服务并且客户端有问题。 Do you use 192.x.x.x on both client and server? I remember seeing an issue a while back in which for TCP the client and server names needed to match (s ... -
“主动拒绝”意味着当您尝试连接时,主机发送了重置而不是ack。 因此,您的代码不是问题。 阻塞连接的防火墙或主机服务的进程没有在该端口上侦听。 这可能是因为它根本没有运行,或因为正在监听不同的端口。 一旦您开始托管服务的过程,请尝试使用netstat -anb (需要管理员权限)来验证它是否正在运行并在预期端口上侦听。 更新:在Linux上,您可能需要执行netstat -anp 。 "Actively refused it" means that the host sent a reset instead ...
-
无法建立连接,因为目标计算机主动拒绝它(No connection could be made because the target machine actively refused it)[2022-03-22]
这是因为你没有打开你想要连接的端口,没有什么在那里听。 如果您尝试连接到Web或ftp服务器,请先启动它。 如果您尝试连接到另一个端口,则还需要编写服务器应用程序。 It's because you haven't opened the port you are trying to connect to, nothing is listening there. If you're trying to connect to a web or ftp server, start it first. If you ... -
最后我发现了这个问题。 IIS在用户“网络服务”或“应用程序池标识”下运行我的WCF。 这些只是窗口内置用户,访问受限。 当我尝试从控制台应用程序访问第三方API时,它工作正常,因为它在我的用户凭据下运行,该凭据具有管理员凭据。 所以我得出结论,当一个应用程序进行跨域调用时,它应该在具有足够凭据的用户下运行以进行此类调用。 解决方案是将我的应用程序池标识更改为管理员用户标识( 我将其更改为我的登录用户 )。 它工作并从WCF服务调用第三方API Finally I found the issue. IIS ...
-
使用SmtpClient,让“目标机器主动拒绝它”(Using SmtpClient, and getting “the target machine actively refused it”)[2022-05-24]
因此,正如我们所说,您的服务器可能配置为拒绝来自每台计算机的中继,这是推荐的安全设置。 在开发计算机上,打开提示符( command )并键入telnet SMTP_SERVER_IP_ADDRESS 25 。 此命令将尝试在端口25上建立基本套接字连接,这是默认的SMTP端口。 如果成功,您仍然需要发现服务器是否需要身份验证。 但是,如果它不成功,则必须暂停代码,直到获得系统管理员的帮助为止。 另一个尝试是从应用服务器重复相同的测试,因为SMTP服务器可能配置为allow app_server并deny ... -
wcf - 无法建立连接,因为目标计算机主动拒绝它(wcf - No connection could be made because the target machine actively refused it)[2021-12-05]
通常,这些问题是服务未运行或您的网址不正确。 如果没有更具体的信息,就无法分辨。 Generally with these issues is either the service is not running or you're url is incorrect. Without more specific information it's impossible to tell. -
这是一个标准的TCP错误 WSAECONNREFUSED - 10061 从客户的角度来看,这意味着“在该地址没有监听该端口的套接字”(“主动拒绝”是一个红鲱鱼)。 i)检查地址ii)检查端口iii)检查防火墙(s) This is a standard TCP error WSAECONNREFUSED - 10061 From the Client's point of view it means 'there is no socket at that address listening to that ...
-
“由于目标机器主动拒绝它,因此无法建立联系”(“No connection could be made because the target machine actively refused it”)[2023-04-30]
找到问题的解决方案,似乎与SDK有关。 创建一个新的CE 5.0应用程序作为测试,一切正常。 Found a solution to the problem, seems to be SDK related. Create a new CE 5.0 app as a test and everything works as it should. -
由于目标机器主动拒绝,因此无法建立连接(No connection could be made because the target machine actively refused)[2024-01-26]
如果您正在从同一台计算机上执行相同的API调用,而另一种方法正在运行而另一种方法不正常,那么您可能无法发送服务器所需的内容。 当您使用AJAX调用时,如果您希望API响应,您的浏览器会附加许多您需要复制的辅助信息。 例如,cookie和用户代理字符串。 服务器检测到您不是浏览器,并拒绝您的请求。 这有很多正当理由,比如试图阻止自动机器人点击他们的API。 您可以使用开发人员工具查看浏览器发送的内容。 例如,在Chrome中,您可以像这样查看网络请求标头: 或者,它可能仍然是您的防火墙限制您的应用程序并允许您 ... -
我发现laravel没有采用新的连接信息,所以我重新启动服务器,它正在工作。 I find out that laravel not taking new connection info so i restart the server and it's working.