How Costura works: Embedding assemblies as resources
developILTemplate.Attach(). This code, along with injected ILTemplate classes, ensures that if an assembly load fails, it is automatically loaded from the embedded resources.repository·develop·Indexed 25 days ago
https://github.com/fody/costuraA Fody add-in that embeds dependencies and PDBs marked as 'Copy Local' as embedded resources within a single assembly. It simplifies application distribution by reducing the number of required files and provides configuration options for handling runtimes, mixed-mode/unmanaged assemblies, and native libraries via FodyWeavers.xml.
ILTemplate.Attach(). This code, along with injected ILTemplate classes, ensures that if an assembly load fails, it is automatically loaded from the embedded resources.After installing the NuGet packages, you must enable Costura by adding the <Costura/> element to your FodyWeavers.xml file.
<Weavers>
<Costura/>
</Weavers>To use Costura, you must install both the Fody and Costura.Fody NuGet packages. It is important to install Fody explicitly to ensure you get a recent version rather than a default older version.
PM> Install-Package Fody
PM> Install-Package Costura.FodyTo include native libraries, add them to your project as an Embedded Resource in a folder named according to the target platform:
costuraX86costuraX64costuraArm64You can also specify the loading order using the PreloadOrder option.
<Costura>
<PreloadOrder>
Foo
Bar
</PreloadOrder>
</Costura>You can control which assemblies are embedded by using ExcludeAssemblies or IncludeAssemblies. These options accept assembly names without the .dll or .exe extension. You can use wildcards at the end of a name for partial matching (e.g., System.*).
Note: You cannot use IncludeAssemblies if you have already defined ExcludeAssemblies (and vice versa).
<Costura>
<ExcludeAssemblies>
Foo
Bar
</ExcludeAssemblies>
</Costura>Mixed-mode assemblies cannot be loaded like managed assemblies. To help Costura identify them and determine the correct environment, include their names in UnmanagedWinX86Assemblies, UnmanagedWinX64Assemblies, or UnmanagedWinArm64Assemblies.
<Costura
UnmanagedWinX86Assemblies='Foo32|Bar32'
UnmanagedWinX64Assemblies='Foo64|Bar64'
UnmanagedWinArm64Assemblies='FooArm64|BarArm64'/>By default, Costura embeds all available runtimes. To restrict this to specific platforms, use the IncludeRuntimes option. Common values include win, win-x86, win-x64, win-arm64, and unix.
<Costura IncludeRuntimes='win-x64|win-arm64' /><Costura /> node in FodyWeavers.xml.Most unit test frameworks require physical .dll files to discover tests. If your testing assembly is being affected by Costura, you may need to configure it to create temporary assemblies and disable cleanup in your testing project's FodyWeavers.xml.
<Weavers>
<Costura ExcludeAssemblies='TargetExe|TargetExeTest'
CreateTemporaryAssemblies='true'
DisableCleanup='true'/>
</Weavers>If you disable automatic loading via LoadAtModuleInit='false', or if you are working in a scenario where module initializers do not work (such as in libraries or Mono), you must manually call CosturaUtility.Initialize() as early as possible in your code.
class Program
{
static Program()
{
CosturaUtility.Initialize();
}
static void Main(string[] args) { ... }
}The following configuration options are available for the <Costura> node in FodyWeavers.xml:
| Option | Default | Description |
|---|---|---|
CreateTemporaryAssemblies | false | Copies embedded files to disk before loading. Useful if assemblies expect a physical file path. |
IncludeDebugSymbols | true | Controls if .pdb files for reference assemblies are embedded. |
IncludeRuntimeReferences | true | Controls whether the runtimes folder (used by .NET Core) is embedded. |
UseRuntimeReferencePaths | false (Framework) / true (Core) | If true, runtime assemblies are embedded with their full path. |
DisableCompression | false | Disables the default compression of embedded assemblies. |
DisableCleanup | false | Prevents Costura from removing embedded assemblies from the build process. |
DisableEventSubscription | false | Disables subscribing to AppDomain.AssemblyResolve or AssemblyLoadContext.Resolving. Use only for advanced plugin scenarios. |
LoadAtModuleInit | true | If false, you must manually call CosturaUtility.Initialize() in your code. |
IgnoreSatelliteAssemblies | false | Disables the automatic handling of assemblies named like resources.dll. |
ExcludeAssemblies | N/A | List of assembly names (no .dll/.exe) to exclude. Supports wildcards at the end (e.g., System.*). |
IncludeAssemblies | N/A | List of assembly names to include. Supports wildcards at the end. |
IncludeRuntimes | N/A | List of runtimes to include (e.g., win, win-x64, unix). |
UnmanagedWinX86Assemblies | N/A | List of mixed-mode/unmanaged assemblies for Win x86. |
UnmanagedWinX64Assemblies | N/A | List of mixed-mode/unmanaged assemblies for Win x64. |
UnmanagedWinArm64Assemblies | N/A | List of mixed-mode/unmanaged assemblies for Win Arm64. |
PreloadOrder | N/A | Defines the order in which preloaded assemblies are loaded. |
Note: ExcludeAssemblies and IncludeAssemblies can be defined as child elements with newline-delimited names or as attributes using the pipe | delimiter.
<!-- Example using child elements -->
<Costura>
<ExcludeAssemblies>
Foo
Bar
</ExcludeAssemblies>
<IncludeAssemblies>
Baz
</IncludeAssemblies>
</Costura>
<!-- Example using attributes -->
<Costura ExcludeAssemblies='Foo|Bar' IncludeAssemblies='Baz' />