# [.net] Are optional Assemblies possible?

This topic is 2955 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is it possible to compile a project with an optional assembly reference?

For example, assume an assembly referenced nunit.framework.dll for its unit tests. During normal usage of the assembly, the unit tests (and thereby all code that accesses nunit.framework.dll) are not invoked at all.

Now I'd expect the assembly to be usable without nunit.framework.dll as long as the unit tests aren't called. However, as soon as I remove nunit.framework.dll from the output directory, the assembly can no longer be loaded due to missing references.

-

My guess is that normally it would work as I expect it to, just not with NUnit. Reason: nunit.framework.dll contains attributes (TestFixtureAttribute, TestAttribute, etc.) that need to be instantiated during load time.

Could that be it?

##### Share on other sites
Probably. I don't know if such a "delay load" of referenced assemblies is even possible, other than using a plugin logic, i.e. dynamically loading an assembly manually at runtime and use reflection to wire it.

But I can tell you how I "solved" this with my tests. Separate all your tests into a separate project, so your main app does not need to reference NUnit. The drawback being that you likely have to adjust some if not all tests if they for instance access internals, which is only possible from within the same assembly.

##### Share on other sites
Quote:
 Original post by unbirdProbably. I don't know if such a "delay load" of referenced assemblies is even possible, other than using a plugin logic, i.e. dynamically loading an assembly manually at runtime and use reflection to wire it.But I can tell you how I "solved" this with my tests. Separate all your tests into a separate project, so your main app does not need to reference NUnit. The drawback being that you likely have to adjust some if not all tests if they for instance access internals, which is only possible from within the same assembly.

Thanks, I already feared that I would have to take this path in the end. It's better for the end user, but I'll have to scroll up and down in Visual Studio's Solution Explorer whenever I want to find the unit tests of a class :)

The internals issue shouldn't be that much of a problem, there's always the InternalsVisibleTo attribute!

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

##### Share on other sites
That is my belief, too. But as soon as I delete nunit.framework.dll from my project's output folder it stops working with a System.IO.FileNotFoundException.

The unit test classes stay untouched during a normal run of my application, so I had expected that I could simply ship the application without nunit.framework.dll.

I've tried to reproduce the issue several times without success:

• Application only references library: deleting library causes no problems

• Application accesses library in unused method: no problems

• Application uses attributes from library: no problems (!)

• Application calls into library: exception at call site

Yet my production code fails as soon as the library in question is loaded unless nunit.framework.dll is in its directory. I simply get the "xyz has stopped working" dialog with no further details. If have enabled .NET's FusionLog, but it doesn't tell me where nunit.framework.dll was attempted to be loaded from. No callstack, no assembly...

##### Share on other sites
Seems I can learn something here! Put together a test and it seems to work: Crash is triggered only when actually calling NUnit (e.g. Assert.True()). You can even construct an attributed class without problems (!!). I checked then on a non-developer machine and still no problems.

How do you reference the assembly ? Through the GAC ? I've added NUnit manually, maybe that's a solution.

Still, yeah, those errors are very unhelpful. And even try/catching does not give back conclusive results (Edit:) does not catch them at all! Are they triggered by the runtime in a separate thread ?

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
12

• 14
• 9
• 22
• 9
• 31
• ### Forum Statistics

• Total Topics
632618
• Total Posts
3007485
• ### Who's Online (See full list)

There are no registered users currently online

×