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?– HermanMar 19, 2010 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 DJun 21, 2010 at 21:13
1 Answer
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.– TedNov 11, 2013 at 14:30
-
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.– TedNov 12, 2013 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.– LeviNov 12, 2013 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. Mar 4, 2015 at 20:39
-
1It'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.– LiamMar 19, 2021 at 15:05