如何判斷方法是否從 .Net(託管)代碼中的 Windows 服務調用 (How to tell if method is called from Windows Service in .Net (managed) code)


問題描述

如何判斷方法是否從 .Net(託管)代碼中的 Windows 服務調用 (How to tell if method is called from Windows Service in .Net (managed) code)

How can I tell if a method I am writing in managed code is being call from an interactive app vs a windows service?


參考解法

方法 1:

What part of a Windows Service is it that you want to know about? Or what part of an interactive app do you not want to know about? What actually matters to you?


Any time I hear a request like this, it is almost always a mistake in design. I would suggest a few answers:

  1. Let the caller tell you which formatter to use, or
  2. Place the name of the formatter class into a configuration file. Have all the formatters implement the same interface. At runtime, the first time you need a formatter, create an instance of the one specified in the config file, and call it through the common interface.
  3. Don't reinvent the wheel. Use the classes in System.Diagnostics, which, in fact, configure a lot like my #2.

It is almost always a mistake for code to be sensitive to the context it was called in.

方法 2:

I think I may have figured this out (at least this works for my needs--your mileage may vary depending on what you are trying to do).  There's a property hanging off of the Environment object called "UserInteractive".  It tells you whether or not you are running in a context with access to the desktop.

方法 3:

There are at least 2 ways to do this:

  1. "System.Reflection.Assembly.GetCallingAssembly().FullName" will return the name of the assembly that is calling your code.
  2. "Environment.StackTrace" will return the full stack trace for who is calling your code.  You should see your calling method name in the string.

方法 4:

You can define two different logger: one for interactive apps and one for windows service. and let client choose which logger he wants to use using a config file. You can also have a default logger if clients chooses a wrong logger or forgets to configure.  I think it should be a better idea to have functionality like logging and formatting message to be configurable.

方法 5:

Don't know if there  is a builtin possibility, but have a look at the System.Diagnostics.Process class. It has, among other things, a GetService() method, maybe that will help you. If that fails,  there is the StartInfo member which may contain helpful information.

If you don't mind using PInvoke, you can get the parent process of the current process. If it is running under the account NT AUTHORITY\SYSTEM and it's name is service.exe, the current process is (most probably) a service. 

(by Phred MenyhertJohn SaundersPhred MenyhertDavid24x7ProgrammerTreb)

參考文件

  1. How to tell if method is called from Windows Service in .Net (managed) code (CC BY-SA 3.0/4.0)

#service #managed #Windows






相關問題

服務間數據的參照完整性 (Referential Integrity of Data Between Services)

系統重新啟動時自動啟動星號 (start asterisk autometically when system got restarted)

CanStop 設置為 False 時停止 Windows 服務的方法 (C#) (Way to Stop a Windows Service when CanStop is set to False (C#))

Android: App Widget trong quá trình cập nhật hiển thị hình ảnh lạ (Android: App Widget during update shows strange images)

查找具有特定 startname 的特定服務 (Finding specific services with specific startname)

如何判斷方法是否從 .Net(託管)代碼中的 Windows 服務調用 (How to tell if method is called from Windows Service in .Net (managed) code)

如何使用主屏幕小部件按鈕關閉服務? (How to use a homescreen widget button to shutdown a service?)

如何將版本號放入 rdlc 文件中? (How do you put a version number into an rdlc file?)

android asynTask 到活動問題? (android asynTask to activity problem ?)

GetCurrentDirectory 並沒有真正返回可執行文件的路徑 (GetCurrentDirectory does not really return the path of the executable file)

通知未顯示,因為服務或警報管理器已被終止 (Notification not showing because service or alarm manager is killed off)

當 gunicorn / celery 服務重新啟動時,Django 中有沒有辦法只執行一次 python 代碼? (Is there a way in Django to execute some python code only once when gunicorn / celery services are getting restarted?)







留言討論