Static Constructor in ASP.NET app

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave

    Static Constructor in ASP.NET app

    Hello,

    Suppose you have a class with a static property with only a get (read
    only). You also have code in a static constructor that sets these
    properties but takes 1 hour to run.

    Now suppose the first request comes in at 11:00 am and tries to read
    from this property. It will need to wait an hour until the page loads
    which is fine and makes sense. If a second (different) request at
    11:01 am comes in and tries to read the same property, will it wait
    until the static constructor is finished? I'm hoping it does... If it
    doesn't then the 2nd request will read dirty data from un-initialized
    properties.

    In my scenerio, the static constructor runs much quicker, but it's a
    high traffic mission critical financial application that must read the
    info each time correctly.

    Thanks!
    Dave
  • Cowboy \(Gregory A. Beamer\)

    #2
    Re: Static Constructor in ASP.NET app

    What is it calculating? Can it be done offline (pre-aggregated outside of
    the web app) and then run from the work done real time?

    I see no reason to run long running processes in page code.

    --
    Gregory A. Beamer
    MVP, MCP: +I, SE, SD, DBA

    Subscribe to my blog


    or just read it:


    *************** *************** **************
    | Think outside the box! |
    *************** *************** **************
    "Dave" <chakachimp@yah oo.comwrote in message
    news:100ce0f8-d828-42cb-a829-d3674c707ecf@w2 4g2000prd.googl egroups.com...
    Hello,
    >
    Suppose you have a class with a static property with only a get (read
    only). You also have code in a static constructor that sets these
    properties but takes 1 hour to run.
    >
    Now suppose the first request comes in at 11:00 am and tries to read
    from this property. It will need to wait an hour until the page loads
    which is fine and makes sense. If a second (different) request at
    11:01 am comes in and tries to read the same property, will it wait
    until the static constructor is finished? I'm hoping it does... If it
    doesn't then the 2nd request will read dirty data from un-initialized
    properties.
    >
    In my scenerio, the static constructor runs much quicker, but it's a
    high traffic mission critical financial application that must read the
    info each time correctly.
    >
    Thanks!
    Dave

    Comment

    • George

      #3
      Re: Static Constructor in ASP.NET app

      Good question. I was asking that one myself.

      According to documentation it will wait.
      "A CLR type, such as a class or struct, can have a static constructor, which
      can be used to initialize static data members. A static constructor will be
      called at most once, and will be called before the first time a static
      member of the type is accessed."

      But i would check before trust it :)


      George.


      "Dave" <chakachimp@yah oo.comwrote in message
      news:100ce0f8-d828-42cb-a829-d3674c707ecf@w2 4g2000prd.googl egroups.com...
      Hello,
      >
      Suppose you have a class with a static property with only a get (read
      only). You also have code in a static constructor that sets these
      properties but takes 1 hour to run.
      >
      Now suppose the first request comes in at 11:00 am and tries to read
      from this property. It will need to wait an hour until the page loads
      which is fine and makes sense. If a second (different) request at
      11:01 am comes in and tries to read the same property, will it wait
      until the static constructor is finished? I'm hoping it does... If it
      doesn't then the 2nd request will read dirty data from un-initialized
      properties.
      >
      In my scenerio, the static constructor runs much quicker, but it's a
      high traffic mission critical financial application that must read the
      info each time correctly.
      >
      Thanks!
      Dave

      Comment

      • =?Utf-8?B?YnJ1Y2UgYmFya2Vy?=

        #4
        RE: Static Constructor in ASP.NET app

        yes. the runtime takes a lock, so no code can access the class statics until
        the constructor has completed. of course if it took an hour, asp.net would
        try to kill the thread, which would not die until the constructor completed,
        so a recycle would probably happen.


        -- bruce (sqlwork.com)


        "Dave" wrote:
        Hello,
        >
        Suppose you have a class with a static property with only a get (read
        only). You also have code in a static constructor that sets these
        properties but takes 1 hour to run.
        >
        Now suppose the first request comes in at 11:00 am and tries to read
        from this property. It will need to wait an hour until the page loads
        which is fine and makes sense. If a second (different) request at
        11:01 am comes in and tries to read the same property, will it wait
        until the static constructor is finished? I'm hoping it does... If it
        doesn't then the 2nd request will read dirty data from un-initialized
        properties.
        >
        In my scenerio, the static constructor runs much quicker, but it's a
        high traffic mission critical financial application that must read the
        info each time correctly.
        >
        Thanks!
        Dave
        >

        Comment

        • Dave

          #5
          Re: Static Constructor in ASP.NET app

          Thanks Bruce,

          That's what I figured. My static constructor doesn't really last an
          hour, it's just a hypothetical situation (probably a bad choice of an
          example). My static constructor really only should take about a second
          or so. I'm simply trying to find out if all requests to the same
          static property will wait for the static constructor to finish. This
          happens to be very important for my app.

          Cowboy, sorry I chose an hour; bad example.
          George, I think you missed my point. I know the constructor will
          finish before reading the static property. What I'm trying to find out
          is if a 2nd (different) request for the same static property comes in
          before the static constructor is finished executing from the first
          request - what happens then? I would guess that it autamically
          "locks" like bruce suggested, but if it didn't I'd have some issues.

          Or to rephrase - I don't need to put any locking into my constructor
          correct?

          Big picture, I've always been a big fan of static methods and static
          properties in an ASP.NET. Used correctly, it can really improve speed/
          effeciency on high traffic sites. However, using static properties
          you have to be carefull for multi-threaded locking issues. These
          issues only arrise when you try and read and write to the same
          memory. Therefore, I've avoided this by just having static properties
          that are "get" only and methods with only local variables. The one
          exception is in the static constructor where I "write" to these static
          properties. This is what led me to this post.

          Thanks!
          Dave

          On Oct 16, 12:48 pm, bruce barker
          <brucebar...@di scussions.micro soft.comwrote:
          yes. the runtime takes a lock, so no code can access the class statics until
          the constructor has completed. of course if it took an hour, asp.net would
          try to kill the thread, which would not die until the constructor completed,
          so a recycle would probably happen.
          >
          -- bruce (sqlwork.com)
          >
          >
          >
          "Dave" wrote:
          Hello,
          >
          Suppose you have a class with a static property with only a get (read
          only).  You also have code in a static constructor that sets these
          properties but takes 1 hour to run.
          >
          Now suppose the first request comes in at 11:00 am and tries to read
          from this property. It will need to wait an hour until the page loads
          which is fine and makes sense.  If a second (different) request at
          11:01 am comes in and tries to read the same property, will it wait
          until the static constructor is finished?  I'm hoping it does... If it
          doesn't then the 2nd request will read dirty data from un-initialized
          properties.
          >
          In my scenerio, the static constructor runs much quicker, but it's a
          high traffic mission critical financial application that must read the
          info each time correctly.
          >
          Thanks!
          Dave- Hide quoted text -
          >
          - Show quoted text -

          Comment

          • =?Utf-8?B?YnJ1Y2UgYmFya2Vy?=

            #6
            Re: Static Constructor in ASP.NET app

            you could also implement your own locking (inside the property), and load the
            value on first reference. if you have static member that take time to load,
            but not refenced a lot this may help.

            also .net will call the static contructor on the first reference to the
            static object (not at startup).

            -- bruce (sqlwork.com)


            "Dave" wrote:
            Thanks Bruce,
            >
            That's what I figured. My static constructor doesn't really last an
            hour, it's just a hypothetical situation (probably a bad choice of an
            example). My static constructor really only should take about a second
            or so. I'm simply trying to find out if all requests to the same
            static property will wait for the static constructor to finish. This
            happens to be very important for my app.
            >
            Cowboy, sorry I chose an hour; bad example.
            George, I think you missed my point. I know the constructor will
            finish before reading the static property. What I'm trying to find out
            is if a 2nd (different) request for the same static property comes in
            before the static constructor is finished executing from the first
            request - what happens then? I would guess that it autamically
            "locks" like bruce suggested, but if it didn't I'd have some issues.
            >
            Or to rephrase - I don't need to put any locking into my constructor
            correct?
            >
            Big picture, I've always been a big fan of static methods and static
            properties in an ASP.NET. Used correctly, it can really improve speed/
            effeciency on high traffic sites. However, using static properties
            you have to be carefull for multi-threaded locking issues. These
            issues only arrise when you try and read and write to the same
            memory. Therefore, I've avoided this by just having static properties
            that are "get" only and methods with only local variables. The one
            exception is in the static constructor where I "write" to these static
            properties. This is what led me to this post.
            >
            Thanks!
            Dave
            >
            On Oct 16, 12:48 pm, bruce barker
            <brucebar...@di scussions.micro soft.comwrote:
            yes. the runtime takes a lock, so no code can access the class statics until
            the constructor has completed. of course if it took an hour, asp.net would
            try to kill the thread, which would not die until the constructor completed,
            so a recycle would probably happen.

            -- bruce (sqlwork.com)



            "Dave" wrote:
            Hello,
            Suppose you have a class with a static property with only a get (read
            only). You also have code in a static constructor that sets these
            properties but takes 1 hour to run.
            Now suppose the first request comes in at 11:00 am and tries to read
            from this property. It will need to wait an hour until the page loads
            which is fine and makes sense. If a second (different) request at
            11:01 am comes in and tries to read the same property, will it wait
            until the static constructor is finished? I'm hoping it does... If it
            doesn't then the 2nd request will read dirty data from un-initialized
            properties.
            In my scenerio, the static constructor runs much quicker, but it's a
            high traffic mission critical financial application that must read the
            info each time correctly.
            Thanks!
            Dave- Hide quoted text -
            - Show quoted text -
            >
            >

            Comment

            Working...