C#一个windows服务项目如何生成和安装?

lein30 2006-05-17 11:57:39
我用得是vs.net 2005
我先创建service工程,编好了代码。然后点击菜单栏-》项目-》添加新项,然后选择
"安装程序类"。

按f5最后弹个窗口说是要installutil安装,然后我用installutil安装,报错
Exception occurred while initializing the installation:
System.BadImageFormatException: The format of the file 'Info_center_sender_trigger.exe' is invalid..
怎么解决?
谢谢90。





然后出来两个.cs,修改它们成下面得的代码:
intaller1.cs
——————————————————————————————————
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace WindowsService1
{
[RunInstaller(true)]
public partial class Installer1 : Installer
{
private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller processInstaller;
public Installer1()
{
InitializeComponent();
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();

// Service will run under system account
processInstaller.Account = ServiceAccount.LocalSystem;
// Service will have Start Type of Manual

serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = "Infomation center sender trigger";

Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
}
——————————————————————————————————————————
installer1.designer1.cs
namespace WindowsService1
{
partial class Installer1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region 组件设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}

#endregion
}
}

按f5,所有代码被锁定,然后弹了个对话窗口,大概说是要用intallutil安装完成;
我察看项目文件夹发现bin 目录和obj目录下都多了两个目录:debug和release,都只有release目录下有exe文件,我执行:


C:\Documents and Settings\Administrator>installutil "……\Projects\WindowsService1\WindowsService1\obj\Release\Info_center_sender_trigger.exe"

Microsoft (R) .NET Framework Installation utility Version 1.1.4322.573
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.

Exception occurred while initializing the installation:
System.BadImageFormatException: The format of the file 'Info_center_sender_trigger.exe' is invalid..

点击_center_sender_trigger.exe执行,结果还是那个要求用installutil安装得信息。
怎么办呢?
...全文
517 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lein30 2006-06-09
  • 打赏
  • 举报
回复
算了,不懂线程
Knight94 2006-06-07
  • 打赏
  • 举报
回复
你把这部分用线程去做,不要在onstart中等待时间过长。
lein30 2006-06-07
  • 打赏
  • 举报
回复
onstart中:
rcdsvctxt.write_str(this.log_filename, "Service start at " + DateTime.Now.ToLongTimeString() + " " + DateTime.Now.ToLongDateString());
httpins = new http_request("http://192.168.1.177/c.php");
process_main();
rcdsvctxt.write_str 是写个文本日志,
http_request 是一个类,初始化,设置其一个变量(private String url)值为("http://192.168.1.177/c.php"),


process_main():让http_request请求浏览那个url,并根据返回的内容写日志,10秒钟后再执行process_main()自己,达到监视一个web页的功能。
Knight94 2006-05-31
  • 打赏
  • 举报
回复
你在服务的onstart中做了什么操作。
lein30 2006-05-31
  • 打赏
  • 举报
回复
在services.msc中找到了那个服务点击“启动服务”,进度条在前进但是半途会卡住,最终弹个对话框启动服务超时,服务没有响应——但服务却是启动成功了的;怎么样通知services.msc服务已经启动呢?
lein30 2006-05-19
  • 打赏
  • 举报
回复
不是安装服务,服务已经安装起了,是在启动得时候遇到得问题
yx10 2006-05-19
  • 打赏
  • 举报
回复
服务已经安装,你不能直接执行哪个exe文件,你要控制面板->管理工具里 不查看服务运行情况
zhandaxia 2006-05-19
  • 打赏
  • 举报
回复
在日志文件里能看到服务启动出现的问题
或者把服务先修改成可调试的控制台程序,等调试全部通过之后再去部署服务。
copico 2006-05-18
  • 打赏
  • 举报
回复
可以做一个bat文件来安装服务
lein30 2006-05-18
  • 打赏
  • 举报
回复
上面得问题已经解决见:http://www.coderpub.com/article/CS_0016.htm,比较详细,看得懂

现在有个问题:我得服务用mmc得服务管理控制台启动,总是要在中途卡住,最后说个什么超时,但是其状态已经是启动了,我该怎么告诉控制台程序我得服务已经启动了?

谢谢

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧