.NET Core的服务检测 [英] Service detection for .NET Core

查看:73
本文介绍了.NET Core的服务检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能否为 .NET Core 建议一种方法,该方法能够在运行时检测代码是否在服务中执行?

Could you please suggest a method for .NET Core which is able to detect in runtime whether the code is executed within a service or not?

我有 .NET Framework 的工作代码,但我认为无法移植它,因为它基于<$中不可用的属性。 c $ c> .NET Core 1.1 。

I have the working code for .NET Framework but I think it is impossible to port it because it is based on properties unavailable in .NET Core 1.1.

推荐答案

使用.NET Core中的公共托管API,这是不可能直接实现的。 Environment.UserInteractive 默认为固定值,并检查测试stdout的连接方式是否会在各种情况下产生意外结果(例如,带有&而没有stdout接线的子流程)

This is not directly possible with public managed APIs in .NET Core. Environment.UserInteractive is defaulted to a fixed value and checks to test how stdout is wired up can yield unexpected results as well for various situations (like subprocesses with & without stdout wiring).

在我的服务托管API的示例实现我最终要求服务注册添加命令行参数(-run-as-service )作为可执行文件作为Windows服务安装(在此应用中,用-register-service 调用)。许多非.net应用程序都以这种方式工作,包括docker。

In my sample implementation for service hosting APIs i ended up requiring the service registration to add a command line argument (--run-as-service) when the executable is installed as a windows service (in this app, wen called with --register-service). A lot of non-.net applications work this way, including docker.

这篇关于.NET Core的服务检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆