2

Say I have a PDF-Factory dll which generates PDFs.

Besides referencing System.Web and check for HttpContext.Current == null ,

Question:

Is there any more precise way of knowing if a function in the DLL was called through Web/Console/Gui context ?

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 2
    Just don't use an oracle, the caller always knows. Add a parameter. – Hans Passant Mar 27 '19 at 13:43
  • How about `Process.GetCurrentProcess().ProcessName`? For IIS it would be `w3wp.exe` and for console it would be the name of the exe file – haim770 Mar 27 '19 at 13:44
  • Out of curiosity. Why do you need to know that? – Steve Mar 27 '19 at 13:44
  • @Steve we had a pdf problem timeout , and when I was about to set thread execution timeout for the web , it turned out actually that it was called through Gui. And then I've asked myself ( for learning purpose) , how can I know if it's web/cmd/web ... – Royi Namir Mar 27 '19 at 13:45
  • @Vikhram If that cmd called the PDF method in its code , then it should be CMD. – Royi Namir Mar 27 '19 at 13:47
  • 1
    I would give the client the control over this parameter then. Let it set the appropriate timeout or add some kind of configuration to make your library adapt to different environments. – Steve Mar 27 '19 at 13:48
  • @Steve that pdf library doesn't support timeout. it was the asp.net thread timeout exception. – Royi Namir Mar 27 '19 at 13:49
  • You can also try to dynamically load some type from `System.Web` using something like `Type.GetType("System.Web.HttpContext", false, true)` and conclude it's a Web environment when it doesn't return `null` – haim770 Mar 27 '19 at 13:49
  • @haim770 yes. I wrote _a similar_ approach in my question :-) – Royi Namir Mar 27 '19 at 13:50
  • 1
    I must say that I disagree of "sending a variable indicating which env". We should keep DRY. and if a source of information can be concluded from one source , then no other source should be created. Caller can be wrong ( copy paste). if that information can be gotten inside the dll , then it should be done there. – Royi Namir Mar 27 '19 at 13:51
  • @RoyiNamir, Not exactly, my approach would not require you to reference the actual `System.Web` assembly. It dynamically checks for its existence using names only – haim770 Mar 27 '19 at 13:52
  • And `AppDomain.CurrentDomain.GetAssemblies().Any(a => a.GetName().Name == "System.Web")` might work as well... – haim770 Mar 27 '19 at 13:53
  • @haim770 Yes. I agree , I said "similar". Yours is a better approach. ( but won't show GUI vs CMD). it will only detect web vs not-web – Royi Namir Mar 27 '19 at 13:53
  • @Vikhram You can load `System.Web` but not to use it and then my original solution would work. and I don't understand why you keep providing the GUI app which is calling CMD app. if the CMD app called pdf code , then it should be recognized as CMD. if the GUI app called that dll , then it should be the GUI. – Royi Namir Mar 27 '19 at 14:14
  • @Vikhram It is possible . Both you and I know it. It's just that I want to find a better way of knowing it. – Royi Namir Mar 27 '19 at 14:17
  • So you've got automated test runners that simulate each environment that you're magically testing for? Because if not and you're just using standard test runners, it leaves a lot of code either untested or only manually tested... – Damien_The_Unbeliever Mar 27 '19 at 15:34
  • You're also limiting yourself for future use. What if someone finds a new environment to call your code from (e.g. imagine you'd written this before UWP came along) and *none* of your current behaviours are appropriate? – Damien_The_Unbeliever Mar 27 '19 at 15:36
  • @haim770 As people said here , you can add reference assemblies and still not using them . so your solution might fail. – Royi Namir Mar 28 '19 at 07:46

2 Answers2

2

The only thing i could think of is using system.environment.userinteractive to check if your code is running in a context that allows user-interaction and therefore is a GUI-app and not a web-app.

D.J.
  • 3,644
  • 2
  • 15
  • 22
0

After using the above suggestions to determine web or not, check out this post:

How to tell if there is a console

Señor CMasMas
  • 4,290
  • 2
  • 14
  • 21