Friday, 13 September 2013

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

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!

No comments:

Post a Comment