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

Fody/LoadAssembliesOnStartup

Repository files navigation

LoadAssembliesOnStartup.Fody

Chat on Gitter NuGet Status

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

See Milestones for release notes.

This is an add-in for Fody

It is expected that all developers using Fody either become a Patron on OpenCollective, or have a Tidelift Subscription. See Licensing/Patron FAQ for more information.

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 use wildcard patterns such as *.Tools.*.

Can take two forms.

As an element with items delimited by a newline.

<LoadAssembliesOnStartup>
    <ExcludeAssemblies>
        Foo
        Bar
        Company.Tools.*
    </ExcludeAssemblies>
</LoadAssembliesOnStartup>

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

<LoadAssembliesOnStartup ExcludeAssemblies='Foo|Bar|Company.Tools.*' />

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 use wildcard patterns such as *.Tools.*.

Can take two forms.

As an element with items delimited by a newline.

<LoadAssembliesOnStartup>
    <IncludeAssemblies>
        Foo
        Bar
        Company.Plugins.*
    </IncludeAssemblies>
</LoadAssembliesOnStartup>

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

<LoadAssembliesOnStartup IncludeAssemblies='Foo|Bar|Company.Plugins.*' />

ExcludePrivateAssemblies

Exclude private assembly references in modern SDK projects, e.g.:

  <PackageReference Include="Catel.Core" Version="5.8.0" PrivateAssets="true" />

Types can still be excluded using the ExcludeAssemblies option when this option is set to false.

The default value is true.

To include private assemblies, use the option below:

<LoadAssembliesOnStartup ExcludePrivateAssemblies='false' />

ExcludeSystemAssemblies

Exclude system assemblies (such as System.Runtime.Serialization). Types can still be excluded using the ExcludeAssemblies option when this option is set to false.

The default value is true.

To include system assemblies, use the option below:

<LoadAssembliesOnStartup ExcludeSystemAssemblies='false' />

The following wildcards will be used:

  • Mono.*
  • System.*

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

Security policy

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published