0

I'm trying to get the assembly of a "currently using by" assembly and I can't figure out how I can do this. I were able to do it when it is referenced in a Form App, but it's is not working when used in a Web Service.

To get it from a WinForm, I used : Assembly.GetCallingAssembly() which return my CallingApp's dll well and then have access to his Assembly. But when it is use for a Web Service, it return the Current DLL itself instead of Web Service's one.

Anyone know how I can resolve this?

Edit #1

More precisely : MyWebService reference MyDLL. In MyDLL, I want to get the MyWebService or any other Project Assemblies which use MyDLL. That why I use Assembly.GetCallingAssembly() which work perfectly for a Win Form app... but not with a Web Service.

Simon Dugré
  • 17,980
  • 11
  • 57
  • 73
  • How could a web service know what assembly called it? It's stateless. It's a web service. – Mike Perrenoud Nov 15 '13 at 16:47
  • Hope you guys will understand what I mean here... I must tell that I usualy talking french so it's sometimes difficult for me to explain what I mean precisely... – Simon Dugré Nov 15 '13 at 16:47
  • @MichaelPerrenoud: It is in fact, the DLL, referenced IN a webservice who want to know the Assembly of the Webservice. – Simon Dugré Nov 15 '13 at 16:48
  • `GetCallingAssembly` is simply looking at next line on call stack. You can do it too in debugger and likely you'll see the reason of behavior you see. – Alexei Levenkov Nov 15 '13 at 16:54
  • In fact, I don't know if my approach is good... In other words, I just want to know, who's using my Class Library's in order to Logs theses informations somewhere and monitoring it – Simon Dugré Nov 15 '13 at 16:59

1 Answers1

1

I can imagine two different approaches:

Via StackTrace you may have to iterate through each frame in order to determine the assembly.

This thread may give you some hints:

C# - Get calling method's Assembly?

Community
  • 1
  • 1
OnoSendai
  • 3,960
  • 2
  • 22
  • 46
  • Is it the best way to do it? I mean, in fact, it is based on an example I saw somewhere... But is there anyway other way I should consider to do what I want? I mean, a class library should know from where it is call and what called it? In other word, I just want to know, who's using my Class Library's in order to Logs theses informations somewhere and monitoring it. – Simon Dugré Nov 15 '13 at 16:57
  • Your question is quite valid, and I asked myself the same thing before. But some characteristics of how C# handles its stack - JIT inline calls, for example - may render both approaches unreliable. To my limited knowledge, tracing the stack back -usually- yields the most reliable information. – OnoSendai Nov 15 '13 at 17:03