Search code examples
c#reflectionfodyfody-costura

Fody and Assembly.GetExecutingAssembly().Location returns empty string


I was calling Assembly.GetExecutingAssembly().Location in one of the dlls of a solution of mine. When I tried to use Fody (https://www.nuget.org/packages/Fody/) to package my binaries into a single binary, I noticed that this call started returning the empty string instead of the location of the executing assembly. Should this be considered a bug? And is there a way to get this information when using Fody? It seems to work fine if I make the call from the main project of my solution.

This is my main method:

static void Main(string[] args)
{
    Console.WriteLine($"From Main: \"{Assembly.GetExecutingAssembly().Location}\"");
    Console.WriteLine($"From dll:  \"{Class1.GetLocation()}\"");
}

And then the GetLocation method is defined in a dll like this:

public static string GetLocation()
{
    return Assembly.GetExecutingAssembly().Location;
}

The output in the console without Fody looks like this:

From Main: "C:\Prog\ConsoleApp1\bin\Debug\ConsoleApp1.exe"
From dll:  "C:\Prog\ConsoleApp1\bin\Debug\ClassLibrary1.dll"

While after adding Fody, it looks like this:

From Main: "C:\Prog\ConsoleApp1\bin\Debug\ConsoleApp1.exe"
From dll:  ""

Solution

  • With Costura, assemblies by default are loaded from memory, and therefore don't have a location.

    To change this you can set CreateTemporaryAssemblies in FodyWeavers.xml which will save the assemblies to a temporary location before loading them.

    <Costura CreateTemporaryAssemblies='true' />