Walking the Call Stack

Does anyone know how to walk the call stack on a calling assembly? I blogged earlier about a new limit I was writing for XHEO|Licensing. Basically I want to limit an assembly's use at runtime, but I don't know what that assembly is going to be at compile time. I have written some code that loads up all the running assemblies into an array, and walks the array to see if the assembly is loaded. That is not the optimal way however, because someone could just reference and load the required caller, then execute my assembly separately, because the licensed caller is loaded into the AppDomain.

The code to check all the assemblies in the AppDomain looks like this:

1Assembly[] loadedAssemblies = Thread.GetDomain().GetAssemblies();

2Assembly asm = null;
3
4for(int i = 0; i < loadedAssemblies.Length; i++)
5 if(String.Compare(loadedAssemblies[i].GetName().Name, AssemblyName, true) == 0)
6 asm = loadedAssemblies[i];
7
8 // Display the full assembly information to the console.
9 if(asm == null)
10 {
11 return false;
12 }
Now. I'm thinking instead, it might be better to create the array with a recursive stack walk, backpedaling through Assembly.GetCallingAssembly(). My question is, is there an easier way to get the call tree from a given assembly? I don't have a whole lot of experience in this particular area of .NET, so I'm sure one of you has a better idea of what to do. Any ideas?

7 Comments

  • Try System.Diagnostics.StackTrace/StackFrame.

  • Unfortunately, none of that helps. According to Paul @ Xheo, the CallingAssembly will always be Xheo.Licensing.dll. I have to walk the loaded assemblies to see who called who, and see if a specific assembly is in that call chain. I need to know the name of the assembly, and the public key token.

  • Why doesn't that work? You can walk those stack frames to go all the way down the call chain and get the name of any assembly in the chain and everything else you need.

  • Wouldn't it just be simpler to just do [Assembly].GetCallingAssembly() recursively, and dump that into an array of assemblies that I can walk? I could be wrong, but after putzing around that namespace for about an hour, that seemed like the best bet to me. What do you think?

  • Jerry, you frickin rock. Next time I see you, I owe you a beer.

  • Robert, The PublisherLimit goes through the callstack checking each Assembly.FullName for the PublicKeyToken in the Limit, To limit it to a publishers specific assembly you would just add a check for the assembly name in the same Assembly.FullName string.

    eg. MyNameSpace.MyClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1a2e12345f7b4948

    Currently the PublisherLimit only looks for the presence of the PublicKeyToken specified in the license, your AssemblyLimit would check for PublicKeyToken &amp;&amp; MyNameSpace.MyClass

    Is that not what you are looking for ?

  • Sort of. I got it working already last night the way I wanted it to work. Thanks everyone!

Comments have been disabled for this content.