Tag: windows services

以用户身份安装Windows服务失败

我正在使用.Net framework 3.5版开发Windows服务。 它需要在远程计算机上调用Web服务,我遇到了一个奇怪的安装问题。 我曾经在我的机器上安装它作为User (默认),并在提示时提供我的登录名和密码,一切正常。 然后在某些时候,它停止工作,我发现将其安装为LocalSystem并且工作正常。 现在我正在尝试调用远程Web服务,我从WCF收到错误: There was no endpoint listening at https://www.remote.com/webservice.asmx that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. 显然,这可能是由于将服务作为LocalSystem (没有Internet访问权限)运行: 没有端点侦听可以接受该消息。 这通常是由不正确的地址或SOAP操作引起的 所以,我已经尝试切换回以User身份安装服务并提供我的凭据(我应该注意,此时我是我的机器的管理员)。 但它不起作用,错误消息(以及InstallUtil日志文件的内容)旁边是无用的: Running a transacted installation. Beginning the Install phase of the installation. […]

在c#中重新启动Windows服务

我从SO获得了一个链接无法重新启动服务 ,它说重启Windows服务。 它应该重新启动服务的方式在问题中提到 public static void RestartService(string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); int millisec1 = Environment.TickCount; TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); if (!(service.Status.Equals(ServiceControllerStatus.Stopped) || service.Status.Equals(ServiceControllerStatus.StopPending))) { service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); } // count the rest of the timeout int millisec2 = Environment.TickCount; timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds – (millisec2 – millisec1)); if (!(service.Status.Equals(ServiceControllerStatus.Running) || service.Status.Equals(ServiceControllerStatus.StartPending))) […]

在处理大文件时,通过Windows服务运行ffmpeg.exe无法完成

我使用ffmpeg.exe将video文件转换为flv格式 。 为此,我使用Windows服务在后台运行转换过程 。 在尝试通过Windows服务 转换大文件 (我在文件大小> 14MB时经历过)时, 它会卡在启动进程的行 (即process.start(); )。 但是当我试图直接从命令提示符执行ffmpeg.exe时,它解决了任何问题。 我在windows服务中的 代码 如下: private Thread WorkerThread; protected override void OnStart(string[] args) { WorkerThread = new Thread(new ThreadStart(StartHandlingVideo)); WorkerThread.Start(); } protected override void OnStop() { WorkerThread.Abort(); } private void StartHandlingVideo() { FilArgs = string.Format(“-i {0} -ar 22050 -qscale 1 {1}”, InputFile, OutputFile); Process proc; […]

在自动启动时将参数传递给Windows服务

我发现了一些类似的问题,但答案似乎对我的情况没有帮助。 我希望用1参数配置我的自动启动服务。 我的服务OnStart方法如下所示: /// /// Starts the service /// /// args must contain the listening port number of the service protected override void OnStart(string[] args) { if (args != null && args.Length > 0) { int port = -1; if (int.TryParse(args[0], out port) && port >= 0 && port <= 65535) { server.Start(port); } else […]

在C#中启动Windows服务

我想启动刚刚安装的Windows服务。 ServiceBase[] ServicesToRun; if (bool.Parse(System.Configuration.ConfigurationManager.AppSettings[“RunService”])) { ServicesToRun = new ServiceBase[] { new IvrService() }; ServiceBase.Run(ServicesToRun); } IvrService代码是: partial class IvrService : ServiceBase { public IvrService() { InitializeComponent(); Process myProcess; myProcess = System.Diagnostics.Process.GetCurrentProcess(); string pathname = Path.GetDirectoryName(myProcess.MainModule.FileName); //eventLog1.WriteEntry(pathname); Directory.SetCurrentDirectory(pathname); } protected override void OnStart(string[] args) { string sProcessName = Process.GetCurrentProcess().ProcessName; if (Environment.UserInteractive) { if (sProcessName.ToLower() != […]

通过Windows服务运行时,操作不可用(从HRESULTexception:0x800401E3(MK_E_UNAVAILABLE))?

我试图从Windows服务挂钩到Outlook应用程序,但得到一个exception操作不可用(HRESULTexception:0x800401E3(MK_E_UNAVAILABLE))这里是我的代码。 public void ItemSendEvent() { try { if (Process.GetProcessesByName(ApplicationConstants.OUTLOOK_PROCESS_NAME).Count() > 0) { // If so, use the GetActiveObject method to obtain the process and cast it to an Application object. outlookApplication = Marshal.GetActiveObject(ApplicationConstants.OUTLOOK_APPLICATION_NAME) as Microsoft.Office.Interop.Outlook.Application; Microsoft.Office.Interop.Outlook.NameSpace nameSpace = outlookApplication.GetNamespace(ApplicationConstants.OUTLOOK_NAME_SPACE); nameSpace.Logon(“”, “”, Missing.Value, Missing.Value); nameSpace = null; outlookApplication.ItemSend += outlookApplication_ItemSend; } log.Info(“Outlook Item Send event registered […]

使用CreateProcessAsUser和CreateEnvironmentBlock创建进程后,不会设置Clientname

我编写了一个在本地系统帐户下运行的C#服务。 当用户登录到Terminalserver时,我用它来生成进程。 该服务实现OnSessionChange方法,并使用相应的SessionID接收SessionChangeDescription消息。 我使用此SessionID通过WTSQueryUserToken从用户获取访问令牌。 我将此标记转换为主标记并将其传递给CreateEnvironmentBlock以检索指向用户环境变量的指针。 经过一些进一步的准备工作后,我调用CreateProcessAsUser函数最终将我的进程作为最近登录的用户在winsta0\default桌面上生成。 当我使用ProcessExplorer调查该过程时,我发现进程上下文中没有CLIENTNAME环境变量。 然而,应用程序需要这个变量。 我不知道我做错了什么。 或许我错过了一些东西。 应该加载用户配置文件,因为我在用户登录时做出反应。 是否可能存在一些时间问题? 或者CLIENTNAME var是否以任何其他方式应用于进程? 以下是我调用CreateEnvironmentBlock函数的方法: private static IntPtr GetEnvironmentFromToken(IntPtr token) { // Get a pointer to the environment variables from the specified user access token IntPtr newEnvironment = IntPtr.Zero; if (!WinApi.CreateEnvironmentBlock(ref newEnvironment, token, false)) { newEnvironment = IntPtr.Zero; } return newEnvironment; } 如果您需要更多信息或代码示例,请随时提出。

CreateProcessAsUser不绘制GUI

我有一个在“SYSTEM”帐户下运行的Windows服务,它检查每个登录用户是否正在运行特定的应用程序。 如果应用程序未运行,则服务启动它(在相应的用户名下)。 我正在尝试使用CreateProcessAsUser()来实现我的目标。 该服务确实以相应的用户名启动应用程序,但未绘制GUI。 (是的,我确保启用“允许服务与桌面交互”复选框)。 系统:XP SP3,语言:C# 以下是一些可能感兴趣的代码: PROCESS_INFORMATION processInfo = new PROCESS_INFORMATION(); startInfo.cb = Marshal.SizeOf(startInfo); startInfo.lpDesktop = “winsta0\\default”; bResult = Win32.CreateProcessAsUser(hToken, null, strCommand, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref startInfo, out processInfo); 据我了解,设置startInfo.lpDesktop =“winsta0 \ default”; 应该使用相应用户的桌面。 甚至与此处所述相反: http : //support.microsoft.com/kb/165194 ,我尝试将lpDesktop设置为null,或者根本不设置它,两者都给出相同的结果:进程以预期的名称启动用户和我可以看到窗口标题栏的一部分。 “不可见”窗口拦截鼠标单击事件,按预期处理它们。 它只是没有吸引自己。 有人熟悉这样的问题,知道我做错了什么吗?

Windows服务锁定assembly负载

我有一个用C#编写的Windows服务。 它还包括一个独立的控制台模式,用于调试目的。 它几乎在每台运行的计算机上运行良好,但是当你尝试启动它时,我们遇到了这个服务锁定的情况,然后它因超时而被杀死。 但是当它在同一台机器上以控制台模式运行时,启动正常。 调试很痛苦,因为我实际上无法访问正在发生的机器,我必须通过人工代理。 但经过一系列的试错调试后,我终于将原因缩小到assembly负载。 当它遇到对特定dll中任何数据类型的第一次引用时,它会根据日志文件在那里停止。 它甚至没有例外,它只是锁定。 [编辑]进一步检查后,它似乎没有永久锁定,实际完成加载库只需要大约40秒,这足以让Windows服务决定终止进程。 有任何线索如何调试这种情况? 这是关于我可以用它重现它的最简单的解决方案。 “之前”显示,但“在”和“之后”不显示。 private static void LoadAssembly() { Log(“During”); MyNameSpace.MyClass x = new MyNameSpace.MyClass(); } static void Main(string[] args) { try { // Leaving out code to handle command line parameters // … // Log(“Before”); LoadAssembly(); Log(“After”); if (Environment.UserInteractive) { Log(“Starting in console mode”); ConnectionManager.Listen(); } […]

Windows服务中未处理的exception

我创建了一个运行多个任务的.net c#windows服务。 我在其中包含exception处理但我想设置一个全局处理程序来捕获Windows服务中未处理的exception,我该怎么做?