Assembly initialization...
-
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
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.
-
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.
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
-
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
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 theAddMessage
function makes a call toRegister
before doing anything else, it is safe to be called by the user without having first explicitly calledRegister
.----------------------------- In just two days, tomorrow will be yesterday.
-
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 theAddMessage
function makes a call toRegister
before doing anything else, it is safe to be called by the user without having first explicitly calledRegister
.----------------------------- In just two days, tomorrow will be yesterday.