Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Assembly initialization...

Assembly initialization...

Scheduled Pinned Locked Moved C#
question
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Phil Boyd
    wrote on last edited by
    #1

    I have a class assembly that references the IMB WebSphere MQ system. I have to register the MQ one time before I can start making calls. I would like to do this when I load my class assembly. What's the best way to achieve this? Tanks

    Phil

    S 1 Reply Last reply
    0
    • P Phil Boyd

      I have a class assembly that references the IMB WebSphere MQ system. I have to register the MQ one time before I can start making calls. I would like to do this when I load my class assembly. What's the best way to achieve this? Tanks

      Phil

      S Offline
      S Offline
      Scott Dorman
      wrote on last edited by
      #2

      You can do this in the class constructor (or a static constructor) as long as you put the appropriate safegaurds in place. You want to make sure that once MQ has been registered you don't try to do it again. You also want to make sure that if the registration fails, your constructor doesn't throw an exception but sets the class state in such a way that the class is created but not usable. Throwing an exception in the constructor will result in a very ambiguous "Type initialization failed" error message. The better way would be to create a singleton class that manages the relationship with MQ. The singleton is instantiated once in your class constructor. It would need to provide an explicit "register" method, and any other methods that access MQ would be part of this singleton and can take advantage of the register method implicitly. The benefit here is that everytime you make a call to MQ, the system verifies that MQ has been registered, and if not registers it before making the call.

      ----------------------------- In just two days, tomorrow will be yesterday.

      P 1 Reply Last reply
      0
      • S Scott Dorman

        You can do this in the class constructor (or a static constructor) as long as you put the appropriate safegaurds in place. You want to make sure that once MQ has been registered you don't try to do it again. You also want to make sure that if the registration fails, your constructor doesn't throw an exception but sets the class state in such a way that the class is created but not usable. Throwing an exception in the constructor will result in a very ambiguous "Type initialization failed" error message. The better way would be to create a singleton class that manages the relationship with MQ. The singleton is instantiated once in your class constructor. It would need to provide an explicit "register" method, and any other methods that access MQ would be part of this singleton and can take advantage of the register method implicitly. The benefit here is that everytime you make a call to MQ, the system verifies that MQ has been registered, and if not registers it before making the call.

        ----------------------------- In just two days, tomorrow will be yesterday.

        P Offline
        P Offline
        Phil Boyd
        wrote on last edited by
        #3

        I had thought about that approach. I'm trying to eliminate the need for the user of my library to initialize the library (hiding the details). It's almost like the global.asax file - with the Application.Start event. If there was an Assembly.Load event or something similar that i could hook into, so the initialization could be done automatically.

        Phil

        S 1 Reply Last reply
        0
        • P Phil Boyd

          I had thought about that approach. I'm trying to eliminate the need for the user of my library to initialize the library (hiding the details). It's almost like the global.asax file - with the Application.Start event. If there was an Assembly.Load event or something similar that i could hook into, so the initialization could be done automatically.

          Phil

          S Offline
          S Offline
          Scott Dorman
          wrote on last edited by
          #4

          There aren't any equivalent events like Assembly.Load. It still sounds like the best approach would be to implement a singleton MQManager class. Since it is a singleton you can hide all of the initialization details behind an explicit Register method. Each "normal" method can check the state and initialize as needed. Something like this (rough pseudo-code):

          public class MQManager {
          private bool registered;

          public void Register() {
          if (!registered) {
          // register
          registered = true;
          }
          }

          public void AddMessage() {
          Register();
          MQ.Add();
          }
          }

          Since the Register function protects itself from registering more than once through the registered flag, it is safe to call multiple times. Also, since the AddMessage function makes a call to Register before doing anything else, it is safe to be called by the user without having first explicitly called Register.

          ----------------------------- In just two days, tomorrow will be yesterday.

          P 1 Reply Last reply
          0
          • S Scott Dorman

            There aren't any equivalent events like Assembly.Load. It still sounds like the best approach would be to implement a singleton MQManager class. Since it is a singleton you can hide all of the initialization details behind an explicit Register method. Each "normal" method can check the state and initialize as needed. Something like this (rough pseudo-code):

            public class MQManager {
            private bool registered;

            public void Register() {
            if (!registered) {
            // register
            registered = true;
            }
            }

            public void AddMessage() {
            Register();
            MQ.Add();
            }
            }

            Since the Register function protects itself from registering more than once through the registered flag, it is safe to call multiple times. Also, since the AddMessage function makes a call to Register before doing anything else, it is safe to be called by the user without having first explicitly called Register.

            ----------------------------- In just two days, tomorrow will be yesterday.

            P Offline
            P Offline
            Phil Boyd
            wrote on last edited by
            #5

            I appreciate the help. If that's what I got do - then that's the way I'll go. Tanks

            Phil

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups