114

How do I determine the dependencies of a .NET application? Does Dependency Walker work with managed apps? I've downloaded the latest and tried profiling the app, but it just exits without much of an explanation. If it doesn't work with .NET, then is there some other tool that would help me debug a run-time DLL loading issue?

Colonel Panic
  • 119,181
  • 74
  • 363
  • 435
Brian Stewart
  • 8,647
  • 11
  • 49
  • 65

12 Answers12

100

Dependency walker works on normal win32 binaries. All .NET dll's and exe's have a small stub header part which makes them look like normal binaries, but all it basically says is "load the CLR" - so that's all that dependency walker will tell you.

To see which things your .NET app actually relies on, you can use the tremendously excellent .NET reflector from Red Gate. (EDIT: Note that .NET Reflector is now a paid product. ILSpy is free and open source and very similar.)

Load your DLL into it, right click, and chose 'Analyze' - you'll then see a "Depends On" item which will show you all the other dll's (and methods inside those dll's) that it needs.

It can sometimes get trickier though, in that your app depends on X dll, and X dll is present, but for whatever reason can't be loaded or located at runtime.

To troubleshoot those kinds of issues, Microsoft have an Assembly Binding Log Viewer which can show you what's going on at runtime

yoyo
  • 7,170
  • 4
  • 49
  • 47
Orion Edwards
  • 113,829
  • 60
  • 223
  • 307
  • I think you missed a little of that URL - the .aspx got put in the link text. I managed to find it though. – Brian Stewart Oct 23 '08 at 00:33
  • oh... yeah the markdown control eats brackets in URL's, and unfortunately MSDN puts (VS80) in all it's url's :-( – Orion Edwards Oct 27 '08 at 04:12
  • 45
    Note that as of early 2011, .NET Reflector is no longer free. The open source ILSpy project is very similar. – yoyo Feb 21 '12 at 05:09
  • 2
    Assembly Binding Log View v4.0.30319.1 is utterly unusable. Log entries are not displayed in chronological order and you cannot sort them. It displays paths which do not fit in the viewer and you cannot resize it. It's a complete waste of time. – Neutrino Aug 28 '13 at 14:42
  • http://www.dependencywalker.com You should include urls of things you mention, esp. if they work. – toddmo Nov 15 '13 at 17:17
  • I just made an edit to this answer to include a note on ILSpy. Assuming my edit is accepted that will make the info easy to find. – yoyo Jun 15 '15 at 17:33
58

I find the small utility AsmSpy an invaluable tool to for resolving issues with loading assemblies. It lists all assembly references of managed assemblies including assembly versions.

Run it in a command prompt in the directory of the .dll with the following arguments:

asmspy . all

asmspy output screenshot

Install it quickly with Chocolatey:

choco install asmspy
Alexander van Trijffel
  • 2,636
  • 1
  • 24
  • 29
  • Can it work on the C# files or Razor views also? I am creating a sub project by exporting some of views and a controller from a mvc project at run time. And I want to know what dependencies are required by these Views and the controller so that I can copy these dependencies also at run time to make the sub-project publishable as a separate web project on IIS. – Rupendra Jun 21 '17 at 08:49
25

Open the assembly file in ILDASM and look @ the .assembly extern in the MANIFEST

Jim
  • 259
  • 3
  • 2
  • 1
    Can I see the version of the dependent assemblies this way too? I only see the dependency name, not it's version as well. – Michael R Mar 10 '16 at 19:21
  • Actually, yes, I can see the version of the dependent assemblies this way too, upon clicking "M A N I F E S T" – Michael R Mar 10 '16 at 19:26
  • 1
    I prefer this one - don't need to download any additional utilities if you're working in a dev environment – Dan Field Feb 03 '17 at 19:48
  • When debugging a third party app crash, how to install only ildasm on customer of? – realtebo Jun 15 '18 at 11:58
17

To browse .NET code dependencies, you can use the capabilities of the tool NDepend. The tool proposes:

For example such query can look like:

from m in Methods 
let depth = m.DepthOfIsUsing("NHibernate.NHibernateUtil.Entity(Type)") 
where depth  >= 0 && m.IsUsing("System.IDisposable")
orderby depth
select new { m, depth }

And its result looks like: (notice the code metric depth, 1 is for direct callers, 2 for callers of direct callers...) (notice also the Export to Graph button to export the query result to a Call Graph)

NDepend dependencies browsing through C# LINQ query

The dependency graph looks like:

NDepend Dependency Graph

The dependency matrix looks like:

NDepend Dependency Matrix

The dependency matrix is de-facto less intuitive than the graph, but it is more suited to browse complex sections of code like:

NDepend Matrix vs Graph

Disclaimer: I work for NDepend

Patrick from NDepend team
  • 12,441
  • 4
  • 53
  • 72
16

You don't need to download and install shareware apps or tools. You can do it programitically from .NET using Assembly.GetReferencedAssemblies()

Assembly.LoadFile(@"app").GetReferencedAssemblies()
Colonel Panic
  • 119,181
  • 74
  • 363
  • 435
  • 11
    For debugging purposes, it's more convenient to do this via PowerShell: `[Reflection.Assembly]::LoadFile('C:\absolute\path\to\my.dll').GetReferencedAssemblies()`. Has the nice advantage of not downloading or hunting obscure Windows locations for tools. +1 – jpmc26 Aug 12 '16 at 20:59
  • 4
    correct me if im wrong but this will just give you the same error that your application with a missing dependency gives so is not very useful – jk. Aug 31 '16 at 10:51
  • This works only when the assembly is loaded into an AppDomain. Assemblies loaded for Reflection return a null set. – David A. Gray Dec 19 '19 at 06:28
6

If you are using the Mono toolchain, you can use the monodis utility with the --assemblyref argument to list the dependencies of a .NET assembly. This will work on both .exe and .dll files.

Example usage:

monodis --assemblyref somefile.exe

Example output (.exe):

$ monodis --assemblyref monop.exe
AssemblyRef Table
1: Version=4.0.0.0
    Name=System
    Flags=0x00000000
    Public Key:
0x00000000: B7 7A 5C 56 19 34 E0 89
2: Version=4.0.0.0
    Name=mscorlib
    Flags=0x00000000
    Public Key:
0x00000000: B7 7A 5C 56 19 34 E0 89

Example output (.dll):

$ monodis --assemblyref Mono.CSharp.dll
AssemblyRef Table
1: Version=4.0.0.0
    Name=mscorlib
    Flags=0x00000000
    Public Key:
0x00000000: B7 7A 5C 56 19 34 E0 89
2: Version=4.0.0.0
    Name=System.Core
    Flags=0x00000000
    Public Key:
0x00000000: B7 7A 5C 56 19 34 E0 89
3: Version=4.0.0.0
    Name=System
    Flags=0x00000000
    Public Key:
0x00000000: B7 7A 5C 56 19 34 E0 89
4: Version=4.0.0.0
    Name=System.Xml
    Flags=0x00000000
    Public Key:
0x00000000: B7 7A 5C 56 19 34 E0 89
Alexander O'Mara
  • 52,993
  • 16
  • 139
  • 151
4

Enable assembly binding logging set the registry value EnableLog in HKLM\Software\Microsoft\Fusion to 1. Note that you have to restart your application (use iisreset) for the changes to have any effect.

Tip: Remember to turn off fusion logging when you are done since there is a performance penalty to have it turned on.

Ramesh
  • 12,212
  • 2
  • 48
  • 83
4

It's funny I had a similar issue and didn't find anything suitable and was aware of good old Dependency Walker so in the end I wrote one myself.

This deals with .NET specifically and will show what references an assembly has (and missing) recursively. It'll also show native library dependencies.

It's free (for personal use) and available here for anyone interested: www.netdepends.com

www.netdepends.com

Feedback welcome.

Lloyd
  • 27,966
  • 4
  • 78
  • 91
  • Please add drag and drop support for opening assemblies. It would also be nice if XCOPY deployment was available, as well as source code. – gigaplex Dec 21 '15 at 00:02
  • I just noticed that the website doesn't have any obvious links to the section where there are two editions, and the free one is for non-commercial use. I accidentally stumbled on this by finding the "Upgrade to Professional" option in the Help menu. There should be a notice on the download page saying that it's not free for commercial use. – gigaplex Dec 21 '15 at 00:08
  • @gigaplex I'll take note of both of these thanks, I'll see what I can do. – Lloyd Dec 21 '15 at 11:41
  • 1
    Shiftclick to open a tree and all subitems would be useful as well. – T.S Jul 07 '17 at 09:02
  • 1
    How inform me of which are the missing dependencies? – realtebo Jun 15 '18 at 11:56
1

http://www.amberfish.net/

ChkAsm will show you all the dependencies of a particular assembly at once, including the versions, and easily let you search for assemblies in the list. Works much better for this purpose than ILSpy (http://ilspy.net/), which is what I used to use for this task.

mhenry1384
  • 7,126
  • 4
  • 50
  • 69
0

Try compiling your .NET assembly with the option --staticlink:"Namespace.Assembly" . This forces the compiler to pull in all the dependencies at compile time. If it comes across a dependency that's not referenced it will give a warning or error message usually with the name of that assembly.

Namespace.Assembly is the assembly you suspect as having the dependency problem. Typically just statically linking this assembly will reference all dependencies transitively.

0

Another handy Reflector add-in that I use is the Dependency Structure Matrix. It's really great to see what classes use what. Plus it's free.

Richard Morgan
  • 7,439
  • 9
  • 46
  • 84
  • Doesn't show version numbers, unfortunately, at least the version that installs as a visual studio add-in doesn't. – mhenry1384 Jun 09 '15 at 13:35
-4

Best app that I see and use, show missed/problematic dlls: http://www.dependencywalker.com/

user1005462
  • 229
  • 2
  • 6