Please bear with this post.. its abit long winded.. but i thought it was
important to describe what i am trying to achieve before i ask the questions
(at the bottom)
Thanks.....in advance
Steve
Okay.. here goes........... .......
I have a class library, which i have been tasked with tracing. One of my
requirements is to associate a unique identifier with the tracing.
For example..
MyClassLibrary. InvokeMethod( "3F2504E0-4F89-11D3-9A0C-0305E82C3301" );
would produce a trace output similiar to the following...... ......
trace.source : 1 3F2504E0-4F89-11D3-9A0C-0305E82C3301 ---> loading
config settings
trace.source : 1 3F2504E0-4F89-11D3-9A0C-0305E82C3301 --->
initialising queue emq\qm1
trace.source : 1 3F2504E0-4F89-11D3-9A0C-0305E82C3301 ---> placing msg
"hello" on queue emq\qm1
trace.source : 1 3F2504E0-4F89-11D3-9A0C-0305E82C3301 ---> reading msg
"world" from queue emq\qm1
Obviously the easiest way to implement this behaviour is to just pass the
identifier in to each method in the class library that needs it ..eg
namespace MyClassLibrary
{
class InvokeMethod(st ring identifier)
{
// do smothing
Trace(identifie r, .. other stuff )
}
}
However for a very large class library such as mine, this means passing in
the identifier parameter to almost every method; which quickly gets
unnaceptable and messy. I cannot use a singleton or any form of static
instance of the identifier because the library will be used in a
multithreaded environmet such as a web server.
For example.. my library is used like..
class MyWebService
{
[WebMethod]
void Pointless()
{
MyClassLibrary. InvokeMethod("3 F2504E0-4F89-11D3-9A0C-0305E82C3301");
MyClassLibrary. InvokeMethod2(" 3F2504E0-4F89-11D3-9A0C-0305E82C3301");
}
}
Ideally i want to be able to write
using( MyClassLibrary. SetUniqueID("3F 2504E0-4F89-11D3-9A0C-0305E82C3301") )
{
MyClassLibrary. InvokeMethod1() ;
MyClassLibrary. InvokeMethod2() ;
}
This can be done using the System.Transact ion.Transaction Scope class, using
it i am able to code
using(transacti onScope = new TransactionScop e())
{
MyClassLibrary. InvokeMethod1() ; // buried deep within my
library i can now call Transaction.Tra nsactionInforma tion.LocalIdent ifier
MyClassLibrary. InvokeMethod2() ;
}
I can get the current transaction id, whilst inside the transaction scope by
simply querying the Transaction.Inf ormation.LocalI dentifierproper ty inside
my trace methods. This is perfect for my needs, but it means i have to wrap
all my library code in a transaction scope. I do not know the side effects
/performance implications this may have, so i am ideally looking for a
similar custom solution.
Q) Does anyone know how to implement functionality similar to the
transaction scope / transaction information mechanism? All i want to is to
get a thread safe identifier in a way that i do not have to pass the
identifer through the object tree. Without using a global [multiple
instances and thread safety].
Q) Can anyone think of an alternative mechanism?
}
---
avast! Antivirus: Outbound message clean.
Virus Database (VPS): 0606-4, 10/02/2006
Tested on: 10/02/2006 21:51:47
avast! - copyright (c) 1988-2005 ALWIL Software.
http://www.avast.com