我有一个C#/ .NET程序,可以作为控制台应用程序和服务运行.目前我给它一个命令行选项来启动作为控制台应用程序,但我想避免这种情况.
是否有可能以编程方式检测我的程序是否作为服务启动?
如果它是纯Win32,我可以尝试使用StartServiceCtrlDispatcher作为服务启动并返回到控制台,如果它返回ERROR_FAILED_SERVICE_CONTROLLER_CONNECT,但System.ServiceProcess.ServiceBase.Run()弹出一个errordialog如果它失败然后只返回没有发出错误信号到该计划.
有任何想法吗?
根据MSDN,在Windows服务或ASP.NET服务中使用System.Drawing命名空间中的类并不是一个特别好的主意.现在我正在开发一个类库,它可能需要访问这个特定的命名空间(用于测量字体),但不能保证主机进程不是服务.
现在有一个不太理想的方法,如果System.Drawing不可用,我可以回退,但如果可行的话,我宁愿在System.Drawing中使用类.所以我想要做的是在runtume确定System.Drawing是否安全,如果是,请使用它,否则回到次优选项.
我的问题是:我怎么可能检测System.Drawing是否可以安全使用?
我想我应该
不幸的是,我无法想出一种方法来实现任何这些方法.有谁有想法吗?
是否有一种简单的方法可以让代码库自动检测是否从控制台应用程序或Windows应用程序调用它?我希望我的库不报告Windows事件日志,如果它是从控制台窗口调用,而是报告给控制台窗口.但是,如果它不在控制台窗口中运行,则应报告给Windows事件日志.
我考虑过要求我的日志记录组件传递日志目标,但如果它本身可以自动处理这两个目标,它会很整洁.我还没有需要像log4net这样广泛的东西,事实上,如果有必要提供支持来记录数据库/文件和其他未知的日志记录目标,那么我可能会推荐这样的解决方案.现在,只是让我的组件自动检测环境并根据环境登录到控制台或事件日志就足够了.
我使用InstallUtil来安装我的服务,我只是无法弄清楚如何为它指定启动参数!
这是我的Installer子类:
[RunInstaller(true)]
public class ServerHostInstaller : Installer
{
private ServiceInstaller m_serviceInstaller;
private ServiceProcessInstaller m_serviceProcessInstaller;
private static string s_usage = "Usage:\ninstallutil /i /username=<user_name> /password=<user_password> NCStub.Server.Host.exe";
public ServerHostInstaller()
{
m_serviceInstaller = new ServiceInstaller();
m_serviceInstaller.ServiceName = Program.ServiceName;
m_serviceInstaller.DisplayName = Program.ServiceName;
m_serviceInstaller.StartType = ServiceStartMode.Automatic;
m_serviceProcessInstaller = new ServiceProcessInstaller();
m_serviceProcessInstaller.Account = ServiceAccount.User;
Installers.Add(m_serviceInstaller);
Installers.Add(m_serviceProcessInstaller);
}
public override void Install(IDictionary stateSaver)
{
base.Install(stateSaver);
string userName = this.Context.Parameters["username"];
if (userName == null)
{
Console.WriteLine(s_usage);
throw new InstallException("Missing parameter 'username'");
}
string userPass = this.Context.Parameters["password"];
if (userPass …Run Code Online (Sandbox Code Playgroud) 我目前正在编写一个小的Windows服务应用程序,我可以通过以下方式成功地/卸载它等:
serviceProcessInstaller = new ServiceProcessInstaller();
serviceInstaller = new System.ServiceProcess.ServiceInstaller();
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.ServiceName = "ABC";
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.Description = "DEF";
Installers.AddRange(new Installer[] { serviceProcessInstaller, serviceInstaller });
Run Code Online (Sandbox Code Playgroud)
...但我显然无法在那里设置启动参数......或者我可以吗?我宁愿不继续以后修改注册表..因此问题...有什么办法我可以用编程方式设置这些参数吗?
有没有办法让.NET库检测它是否作为服务运行?
我的图书馆可以以任何方式运行.但是当它作为服务运行时,开发人员需要调用另一个方法来指示事实或某些功能无法正常工作.我希望我的库处理日志记录,如果在没有调用该方法的服务中使用它,则会写一个警告.
我有一个用C#编写的Windows服务,它充当后端数据库的一堆网络设备的代理.为了测试并添加模拟层来测试后端,我希望有一个GUI供测试操作员运行模拟.还可以将条带化版本作为演示发送出去.GUI和服务不必同时运行.实现这种决斗操作的最佳方法是什么?
编辑:这是我的解决方案,从梳理的东西这个问题,我正在运行的服务和安装.NET Windows服务不InstallUtil.exe使用这个优秀的代码 由马克Gravell
它使用以下行来测试是运行gui还是作为服务运行.
if (arg_gui || Environment.UserInteractive || Debugger.IsAttached)
Run Code Online (Sandbox Code Playgroud)
这是代码.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.ComponentModel;
using System.ServiceProcess;
using System.Configuration.Install;
using System.Diagnostics;
namespace Form_Service
{
static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static int Main(string[] args)
{
bool arg_install = false;
bool arg_uninstall = false;
bool arg_gui = false;
bool rethrow = false;
try
{
foreach …Run Code Online (Sandbox Code Playgroud) 我们有一个控制台应用程序,我们从命令提示符启动进行调试,但我们也将其作为生产的NT服务启动.
现在,代码有这样的逻辑:
if (__argc <= 1) {
assumeService();
} else {
assumeForgound();
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来检查流程是如何启动的?我们是一个开源项目,所以每次我们得到一个新的Windows开发人员时,我们必须解释他们必须指定-farg来阻止应用程序连接到服务控制器.
那么检查父进程呢?
我忘了提到我们正在使用C++(非托管).
我目前正在开发一个 Windows 服务(它启动了,所以这是一件好事)。最大的问题是如何在开始参数字段中获取参数(当然无需手动操作)。
所以我想看到的是以下内容。安装服务后,如果发生以下情况,我会喜欢它。
安装服务并设置启动参数。
怎么会做这样的事情(已经在浏览 StackOverflow 但它不符合我想要的)

我提出这个问题的原因如下:该服务是 GUI 和接收后端之间通信层的一部分。如果后端位置不同(例如另一个 IP 地址),则服务需要相应地具有新地址。
如果您想了解更多信息,请询问(如果某些内容不符合要求,请不要在帖子中“只问:)”)
提前致谢
如果我Program.exe从现有的命令行窗口运行,那么当它完成并退出时,控制台输出仍然存在并且可见。
如果我双击 my Program.exe,那么它将打开一个新的命令行窗口,用于控制台输出...但是当我的 exe 完成时,该窗口将关闭,并带走输出。
在后一种情况下,为了防止输出日志丢失,我可能想要我的Main()be的最后两行Console.WriteLine("Press any key to exit"); Console.ReadKey();
但如果我在前一种情况下这样做,那就有点烦人了。
有什么方法可以检测这两种情况之间的差异,以便我可以有条件地“等待用户说我可以关闭”......仅在必要时?