【问题标题】:Classic ASP Store objects in the session object会话对象中的经典 ASP Store 对象
【发布时间】:2010-11-14 20:31:25
【问题描述】:

我是经典 ASP 的新手,我需要使用经典 ASP 编写 Web 应用程序,因为客户希望它使用经典 ASP。 :(

无论如何!这是我的问题:

当我有一个名为 person 的类的对象时:

Class Person
 Private m_sFirstName

 Public Property Get firstName
 firstName = m_sFirstName
 End Property

 Public Property Let firstName(value)
   m_sFirstName = value
 End Property

End Class


set aPerson = new Person
Person.firstName = "Danny"

set Session("somePerson") = aPerson

到目前为止一切都很好......

在下一个请求中,我尝试读取会话变量,例如:

If IsObject(Session("aPerson")) = true Then
    set mySessionPerson = Session("aPerson")

      Response.Write(TypeName(myTest)) // will output "Person" 
      Response.Write(mySessionPerson.firstName) // will output "Object doesn't support this property or method: 'mySessionPerson.firstName'
End If

任何关于正在发生的事情的想法都会有很大帮助。

【问题讨论】:

    标签: session asp-classic object-persistence


    【解决方案1】:

    不应该

    If IsObject(Session("somePerson")) = true Then
        set mySessionPerson = Session("somePerson")
    

    【讨论】:

    • 没关系,因为 ASP Classic 中的对象无法序列化。
    • 对不起,我在示例中搞砸了
    • @Jeffery:ASP 和 sessionobject 没有“序列化”的概念。
    【解决方案2】:

    我无法解释为什么你的代码不起作用。

    对象是在脚本上下文中创建的,随后在请求完成后将其拆除。因此,虽然类型名称可用,但对象的功能已损坏。

    我可以告诉你,将对象存储在 Session 中并不是一个好主意,即使那些不是在脚本中创建的也是如此。

    ASP 中使用的大多数对象都存在于单个线程中。一旦创建,只有创建对象的线程可以访问该对象。一旦你在会话中存储了一个对象,为了处理这个问题,ASP 将会话与创建它的特定工作线程关联起来。

    当对该会话的后续请求到达时,它现在必须由其特定的工作线程处理。如果该线程恰好忙于处理其他请求,则会话请求将排队,即使有很多其他可用的工作线程。

    总体影响是损害应用程序的可扩展性,工作负载可能会变得不均匀地分布在工作线程中。

    【讨论】:

    • 我见过这个。随着 Session 和 Application 中对象数量的增加,性能会下降。
    【解决方案3】:

    你可以这样做,但你必须有点偷偷摸摸。我对它的理解是,当您将自制对象存储在 Session 中时,会保留所有数据但会丢失所有功能。要重新获得功能,您必须从会话中“重新补充”数据。

    这是 JScript for ASP 中的一个示例:

    <%@ Language="Javascript" %>
    <%
    
    function User( name, age, home_town ) {
    
        // Properties - All of these are saved in the Session
        this.name = name || "Unknown";
        this.age = age || 0;
        this.home_town = home_town || "Huddersfield";
    
        // The session we shall store this object in, setting it here
        // means you can only store one User Object per session though
        // but it proves the point
        this.session_key = "MySessionKey";
    
        // Methods - None of these will be available if you pull it straight out of the session
    
        // Hydrate the data by sucking it back into this instance of the object
        this.Load = function() {
            var sessionObj = Session( this.session_key );
            this.name = sessionObj.name;
            this.age = sessionObj.age;
            this.home_town = sessionObj.home_town;
        }
    
        // Stash the object (well its data) back into session
        this.Save = function() {
            Session( this.session_key ) = this;
        },
    
        this.Render = function() {
            %>
            <ul>
                <li>name: <%= this.name %></li>
                <li>age: <%= this.age %></li>
                <li>home_town: <%= this.home_town %></li>
            </ul>
            <%
        }
    }
    
    var me = new User( "Pete", "32", "Huddersfield" );
    me.Save();
    
    me.Render();
    
    // Throw it away, its data now only exists in Session
    me = null;
    
    // Prove it, we still have access to the data!
    Response.Write( "<h1>" + Session( "MySessionKey" ).name + "</h1>" );
    
    // But not its methods/functions
    // Session( "MySessionKey" ).Render(); << Would throw an error!
    
    me = new User();
    me.Load();  // Load the last saved state for this user
    
    me.Render();
    
    %>
    

    它是一种非常强大的管理保存状态到 Session 中的方法,如果需要,可以很容易地替换为 DB 调用/XML 等。

    有趣的是 Anthony 提出的关于线程的内容,知道他的知识深度,我确信它是正确的并且值得思考,但如果它是一个小网站,您将能够侥幸逃脱,我们已经在一个媒介上使用了它大小网站(每天 10K 访问者)多年来没有真正的问题。

    【讨论】:

      【解决方案4】:

      我会用VB6 创建一个看起来像你的Person 类的COM 对象。然后存储它。代码非常相似。

      Pete 的方法可能有效。

      【讨论】:

        【解决方案5】:

        我懒得给你测试了,但是

        代替:

        set Session("somePerson") = aPerson
        

        试试:

        Set Session("somePerson") = Server.CreateObject(aPerson)
        

        【讨论】:

          【解决方案6】:

          像这样设置会话数据:

          set Session.Contents("UserData") = UserData
          

          然后像这样得到它:

          Session.Contents("UserData.UserIsActive")
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-07-05
            • 1970-01-01
            • 2011-06-23
            • 1970-01-01
            • 2010-11-14
            相关资源
            最近更新 更多