简体   繁体   中英

In C#, how can I get the name of the calling project from within a referenced project?

I have three projects: My.Business, My.WebSite and My.WebService

I need my logging class to be able to identify when a request is made from the website versus the web service. I used to have the web service running as a separate application underneath the web site, so I'd just use different config files, which worked fine. But I want the web service now to run under the same application.

If I could figure out if the request was coming from My.WebSite or My.WebService, I'd be set, but I'm not sure how to do this.

  • Assembly.GetExecutingAssembly() returns back My.Business
  • Assembly.GetEntryAssembly() is null
  • I could check the StackTrace, but that seems sloppy and how for back would I have to go? Especially because the logging may be triggered by code in My.Business that was originally invoked from one of the other projects.

Since the web service requests end in ".asmx", the following concept works, but it just doesn't feel right.

return HttpContext.Current.Request.Path.IndexOf(".asmx") >= 0 ? "My.WebService" : "My.WebSite";

Thanks!

You should be able to use Assembly.GetCallingAssembly():

return Assembly.GetCallingAssembly().FullName;

This will return the assembly that invoked the current executing method, so you can capture whoever is calling into your My.Business assembly that way.

With that said, I tend to agree with the comment above by Meirion Hughes. You might like to consider passing in any info that is required to your logging class, especially if it is likely to be used across more applications in the future.

this.GetType().Assembly.FullName将为您提供当前类的程序集名称。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM