473,566 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initialising when an Assembly loads

I want to perform some initialisation when an assembly is first loaded.

I know about static constructors, but that's not what I want. A class's
static constructor is not called until something uses the class at run
time. That may be too late.

What I'm actually trying to do is hook up a TraceListener so that calls
to Debug.Assert will throw an exception inside NUnit unit tests.
Because the DefaultTraceLis tener just pops up a dialog within the NUnit
GUI (which interrupts the unit tests), or else writes to the output
when run without a GUI (e.g. within the TestDriven.NET add-in), the
unit test in which the assertion failed is wrongly flagged as having
succeeded. NUnit needs an exception. I intend to supply that exception
by writing a TraceListener that will be hooked up by my unit tests. I
could hook it up in, say, the SetUp of my unit tests, or in their
static constructor, but what if I forget to do so for one of my test
fixture classes? I would rather do it once and for all on loading the
assembly that contains my unit tests.

But I can't figure out how!

Thanks,
Peter Gummer
Nov 17 '05 #1
8 1918
How about configuring your listener in app.config?

Nov 17 '05 #2
Truong Hong Thi wrote:
How about configuring your listener in app.config?


Nice idea, except that it's not my application that I want to do this
to, it's my applications unit tests. They are executed by some other
app, e.g. the NUnit GUI app.

I guess I could edit the NUnit GUI's config. But most of the time I use
the TestDriven.NET Visual Studio add-in. I wonder what config file I'd
have to edit to make that use my listener.

I think the ideal solution would be if there were an answer to my
original question, i.e. by performing the initialisation on loading the
unit test assembly.

- Peter Gummer
Nov 17 '05 #3
Hi Peter,

How about AppDomain.Assem blyLoad ?

Stephen.

"Peter Gummer" <pe************ ******@hotnospa mmail.com> wrote in message
news:ON******** ******@TK2MSFTN GP10.phx.gbl...
....

What I'm actually trying to do is hook up a TraceListener so that calls
to Debug.Assert will throw an exception inside NUnit unit tests.
Because the DefaultTraceLis tener just pops up a dialog within the NUnit
GUI (which interrupts the unit tests), or else writes to the output
when run without a GUI (e.g. within the TestDriven.NET add-in), the
unit test in which the assertion failed is wrongly flagged as having
succeeded. NUnit needs an exception. I intend to supply that exception
by writing a TraceListener that will be hooked up by my unit tests. I
could hook it up in, say, the SetUp of my unit tests, or in their
static constructor, but what if I forget to do so for one of my test
fixture classes? I would rather do it once and for all on loading the
assembly that contains my unit tests.

But I can't figure out how!

Thanks,
Peter Gummer

Nov 17 '05 #4
Stephen Ahn wrote:
How about AppDomain.Assem blyLoad ?


Hi Stephen!

Yes, it would be good to set up my TraceListener when that event is
fired. But where would I set up my AssemblyLoadEve ntHandler? It's the
same problem as for my TraceListener.

I have no "Main". Well, the NUnit GUI has a Main, but I don't want to
go editing that. Anyway, doing so still wouldn't solve the problem for
other things that might run my unit tests, such as TestDriven.NET.

What I really need is something comparable to Delphi's unit
"initialization " section: a decentralised bit of code that is
guaranteed to be run on load. C# seems to have no such thing.

There's no magic way to do this with AssemblyInfo.cs , is there?

-- Peter Gummer
Nov 17 '05 #5

Peter,

Hmm, somehow i didn't think the solution would be that easy (otherwise one
of you guys would have worked it out already :)
Good luck, i hope everyone is doing well over there !

Stephen
"Peter Gummer" <pe************ ******@hotnospa mmail.com> wrote in message
news:OH******** *******@TK2MSFT NGP10.phx.gbl.. .
Stephen Ahn wrote:
How about AppDomain.Assem blyLoad ?


Hi Stephen!

Yes, it would be good to set up my TraceListener when that event is
fired. But where would I set up my AssemblyLoadEve ntHandler? It's the
same problem as for my TraceListener.

I have no "Main". Well, the NUnit GUI has a Main, but I don't want to
go editing that. Anyway, doing so still wouldn't solve the problem for
other things that might run my unit tests, such as TestDriven.NET.

What I really need is something comparable to Delphi's unit
"initialization " section: a decentralised bit of code that is
guaranteed to be run on load. C# seems to have no such thing.

There's no magic way to do this with AssemblyInfo.cs , is there?

-- Peter Gummer

Nov 17 '05 #6
Hi Peter,

I think Debug.Assert is more suitable for debugging than for unit
testing purpose.
So I think you don't need to try to convert a Debug.Assert into an
exception. If the exception makes sense, why not throw it instead of
using Debug.Assert?

I think if you wrote complete unit test suite which cover the cases
that Debug.Assert is used, then such an exception cannot escape.

I think unit test should yield the same results when run with either
debug or release builds.

Regards,
Thi - http://thith.blogspot.com

Nov 17 '05 #7
Truong Hong Thi wrote:
So I think you don't need to try to convert a Debug.Assert into an
exception. If the exception makes sense, why not throw it instead of
using Debug.Assert?


Because I'm using Debug.Assert to detect programming errors, in the
manner of Design by Contract. Hopefully these errors will be eradicated
before releasing the product. Debug.Assert has no runtime overhead for
release builds.

So, in a debug build, the failure of a Debug.Assert is a bug. If it
happens to fail in a unit test then I want to know about it. NUnit
detects failures via exceptions. I don't think there's any way to
configure NUnit to detect Debug.Assert failures too. It might be nice
to allow it to keep running, as it would in a release build, as long as
NUnit can detect that it failed and report as much, instead of wrongly
saying the test passed. Doing that would require a hook into NUnit that
I think NUnit just doesn't have.

Regardless of whether I throw an exception or somehow find a way to
tell NUnit that it failed without throwing an exception, I still need
to set up my own TraceListener.

So my original question remains. Is there a way for C# to let me hook
up a TraceListener on assembly load? A static constructor nearly does
what I want, but it may be too late; and I don't have a Main routine
that I can depend on.

Anyway, I've posted a message to the NUnit forum on SourceForge, so I
may get a solution there.

-- Peter Gummer
Nov 17 '05 #8
OK, Peter, do whatever you think is right.
I guess you could write a base class for all of your test fixture, and
put your initialization in either setup method or constructor. To
prevent your listener being added multiple times, you might need to
check for its existence before adding.
Regards,
Thi

Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5000
by: Benjamin | last post by:
Hi, I'm currently writing a Web Services that interacts with a database. To allow me to use not just one database provider (for example, I could use MS Access, SQL Server or MySQL), the Web Service dynamically loads up an assembly that implements an Interface I called IDatabase. To load the assembly and create an object, I wrote this...
3
1527
by: Daniel | last post by:
how do i access methods when the .net assembly was loaded with late biding? class Class1 { public static void Main() { System.Reflection.Assembly SampleAssembly; SampleAssembly = System.Reflection.Assembly.LoadFrom("C:\\SimpleSolutions\\asmload\\asmtoload \\bin\\Debug\\asmtoload.dll");
0
1373
by: Ricardo Lopez | last post by:
When I load a Visual C# project which has assembly references (dll references) that do not exist, I get a little yellow triangle with an exclamation point. But the project still loads. If I do the same thing with a Visual C++ project, I get the "Project <foo.vcproj> failed to load". I created a simple example of this to make sure it...
3
1745
by: Clift Norris | last post by:
I've created a managed C++ assembly containing a managed class that is instantiated from ASP.NET. The managed C++ assembly needs msvcp71.dll and msvcr71.dll. When ASP.NET loads the assembly, it cannot locate the C++ runtime DLLs and hence throws an exception. I have placed the C++ runtime DLLs in the same directory as the managed C++...
4
2478
by: BrianS | last post by:
What is the best strategy for dynamic loading private assemblies in asp.net? I understand, and have confirmed, that any dll placed in the app's /bin dir will get loaded on startup. This is not desirable. I have a web service that, based on an input parameter, dynamically loads A, B or C library. I also understand that an separate AppDomain...
9
2208
by: Gravy | last post by:
Hi I want to use one of my c# assemblies on the client browser (IE), say using javascript to call it. I have seen loads of people asking how they can host a WinForms control in IE but all I want to do is call a few methods in my assembly which do not have or need a UI. The <object> tag seems to play a big role here but I haven't...
4
1248
by: steve | last post by:
Hi All After writing numerous programs in VB.net 2005 I find that the Toolbox takes forever to initialise. When it appears it has loads of extra tools mainly report viewer Table Adaptors etc that I have built in my programs Is there any way to stop the toolbox from loading anything other than the default tools
1
2074
by: Boot2TheHead | last post by:
http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.reflectiononlyloadfrom.aspx When I open an assembly using Assembly.ReflectionOnlyLoadFrom it loads fine, but when I try to build that assembly (I'm using a winforms prog to look at the assembly, VS to build it) I get an IO error on the build. This seems a little...
8
3731
by: Joe Withawk | last post by:
I have a solution consisting of a c# project as win application and a c++ project as classlibrary. Both are .net 2.0 The classlibrary handles some loading of quicktime movies, but that should not be relevant. On my development machine I build the exe and dll and run the exe which has a reference to the dll. At some point it uses featured in...
0
7666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8108
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7951
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.