Skip to content

Loads all the references on startup by actually using the types in the module initializer.

License

Notifications You must be signed in to change notification settings

ykankaya/LoadAssembliesOnStartup

 
 

Repository files navigation

Chat on Gitter NuGet Status

LoadAssembliesOnStartup

Icon

This is an add-in for Fody

Loads all the references on startup by actually using the types in the module initializer.

NuGet package

Available here: http://nuget.org/packages/LoadAssembliesOnStartup.Fody

To Install from the Nuget Package Manager Console

PM> Install-Package LoadAssembliesOnStartup.Fody

How it works

By default, assemblies are only loaded on-demand. This means that the first time a type is actually used, the .NET runtime will load the assembly.

When using ModuleInit, it is possible to initialize an assembly at startup. For example, to register types in a service locator.

To prevent hacks such as the one displayed below:

// Note: this is a hack, force loading of external assembly 
var dummyType = typeof(MyExternalAssemblyType);
Console.WriteLine(dummyType.FullName); 

it is possible to let this plugin take care of this. It will add the following code for each referenced assembly that contains at least one public class:

var preloadType = typeof(ReferenceType);

This will ensure that an assembly is actually being loaded into the AppDomain (which is not the same as Assembly.LoadFrom).

Configuration options

All config options are accessible by modifying the LoadAssembliesOnStartup node in FodyWeavers.xml.

ExcludeAssemblies

A list of assembly names to exclude from the default action of "embed all Copy Local references".

Do not include .exe or .dll in the names.

Can not be defined with IncludeAssemblies.

Can take two forms.

As an element with items delimited by a newline.

<LoadAssembliesOnStartup>
    <ExcludeAssemblies>
        Foo
        Bar
    </ExcludeAssemblies>
</LoadAssembliesOnStartup>

Or as a attribute with items delimited by a pipe |.

<LoadAssembliesOnStartup ExcludeAssemblies='Foo|Bar' />

IncludeAssemblies

A list of assembly names to include from the default action of "embed all Copy Local references".

Do not include .exe or .dll in the names.

Can not be defined with ExcludeAssemblies.

Can take two forms.

As an element with items delimited by a newline.

<LoadAssembliesOnStartup>
    <IncludeAssemblies>
        Foo
        Bar
    </IncludeAssemblies>
</LoadAssembliesOnStartup>

Or as a attribute with items delimited by a pipe |.

<LoadAssembliesOnStartup IncludeAssemblies='Foo|Bar' />

ExcludeOptimizedAssemblies

By default, this weaver will include references that are optimized away by the compiler. This can happen when you only use interfaces from a reference. Types can still be excluded using the ExcludeAssemblies option.

To disable all the optimized assemblies (default .NET compiler behavior), use the option below:

<LoadAssembliesOnStartup ExcludeOptimizedAssemblies='true' />

WrapInTryCatch

By default, this weaver calls the typeof(SomeType) without any exception handling. While in general, this is good, it might happen that an assembly cannot be loaded. This can either be solved by adding it to the ExcludeAssemblies list or setting the WrapInTryCatch property to true:

<LoadAssembliesOnStartup WrapInTryCatch='true' />

Then the weaved code will look like:

try
{
	typeof(FirstTypeFromReference1);
}
catch (Exception
{
}


try
{
	typeof(FirstTypeFromReference2);
}
catch (Exception
{
}

Icon

Explosion by Gustav Salomonsson from The Noun Project

About

Loads all the references on startup by actually using the types in the module initializer.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 97.8%
  • Batchfile 2.2%