Windows 服务可在 Windows 7 上运行,但不能在 Windows Server 2003 上运行

发布于 2024-11-14 09:23:32 字数 1048 浏览 0 评论 0原文

问题已解决,请参阅我的帖子底部。

因此,我有一个简单的 Windows 服务,它监视特定文件夹并使用 Web 服务将进入该文件夹的文件上传到服务器。

它在我使用 Windows 7 的计算机上工作正常,但当我尝试在 Windows Server 2003 上启动它时,我收到错误:错误 1053:服务未及时响应启动或控制请求。但仅仅几秒钟后我就收到了这条消息。

我创建了 ServicesPipeTimeout 并将其设置为 60000 毫秒。

我尝试使用 sc query 命令从命令行运行它,发现 WIN32_EXIT_CODE 为 0,我认为这意味着该服务甚至不会尝试启动,因为它在启动之前发现了错误。

在事件查看器中,我收到错误 7000 和 7009。 我是windows服务器的管理员。 我唯一没有尝试过的是我从微软找到的修补程序,但我不想使用它,因为据我了解,它是在服务实际超时时使用的。http://support.microsoft.com/ kb/886695

我已经尝试了我能想到的一切,有什么我遗漏的吗?

Gísli

编辑:重新安装了 .NET 框架,现在我收到一个新错误,说找不到服务控制器。

编辑:我使用安装项目设置服务,而不是使用 installutil 命令。这是因为我需要在安装过程中获取用户输入并将其保存在注册表中。

编辑:我已经安装了.NET 4.0框架,如果不这样做就不可能安装该服务。

除了我上面写的之外,我还尝试过: 重新启动。 重新安装。 我尝试更改服务需要访问的文件的权限。 在注册表编辑器中更改权限。 编辑代码,使 onStart 函数仅启动一个线程。

我认为这是某种权限问题,但我没有太多处理 Windows 服务器的经验。

解决方案: 事实证明这是两个独立的问题。 .NET 框架必须修复,并且我必须删除启动服务时的 try/catch 子句。由于某种原因(我不知道),try catch 块做了一些事情,导致无法在 Windows Server 2003 中启动该服务,但它在 Windows 7 上运行良好。

了解这是为什么会非常有趣。

感谢您的所有帮助。

吉斯利

Solved the problem see the bottom of my post.

So I have a simple windows service that watches a specific folder and upploads files that come into it to a server using a web service.

It's working fine on my machine using windows 7 but when I try to start it on a windows server 2003, I receive an error: Error 1053: the service did not respond to start or control request in a timely fashion. But I get this message after only a few seconds.

I have created the ServicesPipeTimeout and set it to 60000 milliseconds.

I have tried running it from command line using sc query command and found out that the WIN32_EXIT_CODE is 0, which I think means that the service doesn´t even try to start because it find an error before it starts.

In the event viewer I get errors 7000 and 7009.
I am the Administrator on the windows server.
The only thing I haven´t tried is a hotfix I found from microsoft but I don´t want to use it because as I understand it, it is for when the service actually times out.http://support.microsoft.com/kb/886695

I have tried everything I can think of, is there anything that I am missing?

Gísli

EDIT: Re-installed the .NET framework and now I get a new error, Saying that the service controller can not be found.

EDIT: I setup the service with a setup project, not using the installutil command. This is because I need to get user input during the installation and save that in the registry.

EDIT: I have installed the .NET 4.0 framework, it wasn´t possible to install the service with out doing that.

In addition to what I wrote above I have also tried:
Rebooting.
Re-installing.
I have tried to change the permissions on the files that the service needs to access.
Changing permissions in the registry editor.
Edited the code so that the onStart function only starts one thread.

I think it is some kind of permission problem but I don´t have much experience dealing with Windows server.

Solution:
It turned out to be two seperate problems. The .NET framwork had to be repaired and I had to remove the try/catch clause that I had when starting the service. For some reason (unknown to me) the try catch block did something that made it impossible to start the service in a windows server 2003 but it ran fine on windows 7.

It would be very interesting to know why this is.

Thanks for all the help.

Gísli

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧时浪漫 2024-11-21 09:23:32

您是否在 Server 2003 PC 上安装了正确版本的 .NET Framework? Windows 7 上的标准功能需要在较旧的操作系统上手动安装。

你说“我已经尝试了我能想到的一切”。请编辑问题以显示您已经尝试过的操作,这样我们就不会建议您已经做过的操作。

编辑:
另请尝试融合日志查看器。将其设置为记录失败然后启动您的服务。点击刷新,然后查看是否记录了任何错误。双击某行可了解更多详细信息。

Have you installed the right version of .NET Framework on the Server 2003 PC? What comes as standard on Windows 7 will need to be installed manually on an older OS.

You say "I have tried everything I can think of". Please edit the question to show what you have already tried so we don't suggest something you have already done.

EDIT:
Try also the Fusion Log viewer. Set it to log failures then start your service. Hit refresh then see if any errors are logged. Double-click a line for more details.

花之痕靓丽 2024-11-21 09:23:32

我猜您是使用 .NET 2.0 安装了 .NET 3.5 服务? InstallUtil 将工作,因为 CLR 相同,但由于 .NET 3.5 依赖项,该服务不会启动。

参见:

如何安装开发的 Windows 服务在.NET 3.5 中?

I'd guess that you installed the .NET 3.5 service using .NET 2.0? InstallUtil will work since the CLR is the same, but the service won't start because of .NET 3.5 dependencies.

See Also:

How to install a Windows service developed in .NET 3.5?

£噩梦荏苒 2024-11-21 09:23:32

在您的 OnStart 代码中,您可以将所有内容包装在 try-catch 块中,并使用以下内容将异常消息和堆栈跟踪写入文件:

File.WriteAllText(@"C:\Temp\MyServiceLog.txt", exp.Message + exp.StackTrace);

这将帮助您分析问题。另外,我使用一种非常简单的方法来调试我的服务:如果会话是交互式的,我将它们包装在 WinForms 包装器中。为此,我需要以下内容:

  1. 创建服务类实例的表单
  2. 我的服务类中的新方法 DoStartDoStop,它们是公共的并调用受保护的 < code>OnStart 和 OnStop 方法
  3. 在上面表单的 OnLoadOnClose 处理程序中添加新代码,因此 DoStartDoStop相应地调用服务实例的方法

然后我将以下代码添加到 Program.cs 中的 Main 方法中:

if (Environment.UserInteractive)
{
    Application.Run(new FormServiceHoster());
}
else
{
    // ... old code to create service instance.
}

这样您就可以使“服务”作为当您只需在 Visual Studio 中按 F5 或在资源管理器中双击 EXE 时,即可应用程序。当服务实际作为服务运行时,表单代码将被忽略。

通常,您可以通过附加到进程来调试服务,但是,这不适用于调试启动和停止代码。此方法对调试服务启动和停止代码有很大帮助。

您需要手动添加对 System.Windows.Forms 和 System.Drawing 的引用。

In your OnStart code you could wrap everything in a try-catch block and write the exception message and stack trace to a file using something like:

File.WriteAllText(@"C:\Temp\MyServiceLog.txt", exp.Message + exp.StackTrace);

This will help you analyze the problem. Also, I'm using a very simple way of debugging my services: I'm wrapping them in a WinForms wrapper if the session is interactive. For that, I need the following:

  1. A form that creates an instance of the service class
  2. New methods DoStart and DoStop in my service class, which are public and call the protected OnStart and OnStop methods
  3. New code in above form's OnLoad and OnClose handlers, so the DoStart and DoStop methods of the service instance are called accordingly

Then I add the following code to the Main method in Program.cs:

if (Environment.UserInteractive)
{
    Application.Run(new FormServiceHoster());
}
else
{
    // ... old code to create service instance.
}

This way you're making the "service" run as an application when you just hit F5 in Visual Studio or double click the EXE in the Explorer. When the service is actually run as a service, the Forms-code is ignored.

Usually you can debug services by attaching to the process, however, that doesn't work for debugging the start and stop code. This method helps a great deal debugging the service start and stop code.

You will need to add references to System.Windows.Forms and System.Drawing manually.

纵情客 2024-11-21 09:23:32

我敢打赌,您希望使用像 log4net 这样的日志记录包,这可能有助于判断您的程序走了多远。

OnStart 事件中的代码实际上花费的时间是否可能比您预期的要长。如果是这样,您可以将该代码移至一个线程,以便 OnStart 事件完成(因此 Windows 认为该服务已启动),同时您的线程继续工作。

您是否以管理员身份运行该服务?如果是这样,通常可以排除权限问题。 (如果仍然出错,则可以排除 KB 886695 问题,因为该问题似乎仅适用于本地系统)。

由于您使用的是 .NET 4.0,您是否确定安装了完整版本而不是客户端配置文件,或者您的应用程序可以使用客户端配置文件?

This is the point where I bet you wish you had used some logging package like log4net, which might help tell how far your program gets.

Is it possible that the code in the OnStart event is actually taking longer than you expect. If so, you could move that code to a thread, so that the OnStart event finishes (and so Windows reckons the service has started) while your thread continues working.

Are you running the service as admin? If so, that usually rules out permissions issues. (If it still goes wrong, it will rule out the KB 886695 issue as that seems to only apply to local system).

Since you are using .NET 4.0 are you sure that either, the full version is installed, rather than the Client Profile, or that your application works with the client profile?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文