Just wanted to know if there is any difference between the two, in the context of a fully trust asp.net mvc 2 application.
-
@starblue: care to explain the vote down? – Herman Mar 19 '10 at 20:00
-
1Thanks for asking this. I didn't know BuildManager existed, which just totally saved me. (Because of the behavior Levi described) – Jeff D Jun 21 '10 at 21:13
1 Answers
The .NET Framework defers loading assemblies into the current AppDomain until they're needed. For example, if you call into a third-party library only from SomeMethod()
, the third-party DLL normally won't be loaded until the first time SomeMethod()
runs.
AppDomain.GetAssemblies()
gives you all assemblies which have already been loaded into the current AppDomain. BuildManager.GetReferencedAssemblies()
(This method is only available in the .Net Framework System.Web.dll
) returns a list of all assemblies referenced from Web.config and elsewhere, and it loads those assemblies into the current AppDomain.
Here's a worked-out example of the above.
SomeMethod()
hasn't yet run.- Call
AppDomain.GetAssemblies()
, returns a set that does not include ThirdParty.dll. - Call
SomeMethod()
. - Call
AppDomain.GetAssemblies()
, returns a set that includes ThirdParty.dll.
In this example, the CLR defers loading ThirdParty.dll into the current AppDomain until it's absolutely necessary. And since it's necessary for the execution of SomeMethod()
, that's when it gets loaded.
Alternatively:
SomeMethod()
hasn't yet run.- Call
AppDomain.GetAssemblies()
, returns a set that does not include ThirdParty.dll. - Call
BuildManager.GetReferencedAssemblies()
, returns a set that includes ThirdParty.dll. - Call
AppDomain.GetAssemblies()
, returns a set that includes ThirdParty.dll.
Here, even though you never called SomeMethod()
, the call to BuildManager.GetReferencedAssemblies()
loaded the third-party library into the current AppDomain on your behalf.
Of course, this is all subject to certain optimizations, etc., but the general idea holds.
-
2So the only way to get a hold of BuildManager is by referencing System.Web.dll? Sounds a bit weird. – Ted Nov 11 '13 at 14:30
-
@Ted What's wrong with this? I simplified quite a bit in my answer above, but the concept of BuildManager only really makes sense inside an ASP.NET application. – Levi Nov 12 '13 at 02:57
-
2yes, referencing System.Web.dll and using BuildManager will throw an exception if its not an ASP.NET project. In a WinForms you cant do this. – Ted Nov 12 '13 at 21:37
-
2Why would you want / need to reference BuildManager outside of ASP.NET? BuildManager is specifically designed to interface with the ASP.NET compilation system, so it doesn't make sense to reference this type from a WinForms application. Consider opening a new SO question with the exact problem you're trying to solve, as more than likely there's a supported way to accomplish what you want. – Levi Nov 12 '13 at 23:11
-
7@Levi, probably because he wants to get all of the referenced assemblies and the BuildManager has a method for getting all of the referenced assemblies. – Joel McBeth Mar 04 '15 at 20:39
-
4It's worth noting that this answer is now 11 years old and `BuildManager.GetReferencedAssemblies()` doesn't exist **at all** in .Net Core or .Net 5.0. – Liam Mar 19 '21 at 15:05