79

What is the ref folder when compiling .NET 5.0 application?

I mean this one:

[project_path]\bin\Debug\net5.0\ref\
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
Alek Depler
  • 1,182
  • 2
  • 6
  • 11
  • What application? What's inside? – Sinatr Nov 20 '20 at 08:04
  • 2
    @Sinatr it is just test console application without dependencies. There is only one dll inside (which is entry point of application), and the same dll with slightly different size is located in parent folder. Just create sample NET5.0 and you'll see – Alek Depler Nov 20 '20 at 08:09
  • Tip: use `dotnet clean` to remove these intermediate build files. Doing this will remove files outside `publish` directory and files inside `ref` directory. – kbridge4096 Feb 25 '22 at 08:02

1 Answers1

95

These are so called Reference Assemblies (assemblies that only contain the public interface of an assembly), these help speed up the build process, since projects that depend on this one will be able to see that there is no reason to recompile, even if the innards of the assembly have changed, because outwardly it's still the same.

These Reference Assemblies need to look the same as the real thing from the outside. Hence, they have the same filename, assembly name, assembly identity and everything. This allows the build system to use them as a substitute for the real thing. And since these assemblies don't have any of the implementation details, they only change when the interface of the contents changes. Due to these facts, they can't live in the same folder as the actual build output, and this is the reason for the extra ref folder. MsBuild will use these reference assemblies automatically to speed up the build process (at the expense of generating and comparing the reference assembly each time the compiled code results in a new project output and a few files in the output directory).

If your project isn't referenced by other projects, you don't get any benefits from these reference assemblies (if you don't hand them out to 3rd parties that is). And you can turn off this feature by adding this property to the project file:

<PropertyGroup>
     <!-- 
     Turns off reference assembly generation 
     See: https://learn.microsoft.com/en-us/dotnet/standard/assembly/reference-assemblies
     -->
     <ProduceReferenceAssembly>false</ProduceReferenceAssembly>
</PropertyGroup>

After changing the setting, make sure you clean your build outputs.

These reference assemblies can also be used to allow people to compile projects to work with your system, without having to install/redistribute the actual compiled assemblies that run on your server. This way people can develop extensions, plugins, or clients for your application without having to give them access to actual implementation. This can help protect your intellectual property, since .NET assemblies are easy to decompile.

See also:

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • You don't need it if you don't want to distribute these reference assemblies, but Msbuild will use them when appropriate. – jessehouwing Nov 22 '20 at 14:22
  • 6
    Since they have the exact same name of the actual assemblies, they can't be put in the same folder. You can turn off the generation of reference assemblies if you don't want them you can add `false` to your project file to turn it off (at the possible expense of more rebuilds in larger solutions). – jessehouwing Nov 22 '20 at 15:54
  • 2
    Exactly the kind of explanation I was searching for, thanks @jessehouwing – AFract Feb 18 '21 at 12:50
  • 2
    Add it in , just if anybody has doubts ;-) – 0Pat Jun 25 '21 at 09:12
  • It broke some of my scripts that now return multiples >:( – Sinaesthetic Oct 17 '21 at 22:20
  • Why they want to add a functionality without asking to the user, a functionality that is probably the user doesn't want? Oh, wait, is Microsoft – Leandro Bardelli Dec 16 '21 at 15:00
  • 1
    Doesn't seems to work anymore ? VS 2022, NET CORE 3.1 – Leandro Bardelli Dec 16 '21 at 15:51
  • 1
    @LeandroBardelli, because it drastically speeds up build times in many cases and people want that. You can opt out if you need to. Make sure you clean your build output for these files to go away. – jessehouwing Dec 16 '21 at 23:03
  • @jessehouwing thanks!!! I never thought in clean it – Leandro Bardelli Dec 17 '21 at 01:05