如何配置c# application的运行环境

syscoder 2010-04-19 11:10:35
以前没做过windows的开发
拿别人的一个东西照葫芦描瓢,目的是做一个服务器程序,
没有界面,每天定时启动
开发环境下程序已经测试通过
需要把这个程序放到Server上

没有什么Windows的开发常识,请大家指点:
我打算把开发环境中debug下bin目录里的所有exe文件以及dll文件直接copy到
Server上,然后在windows中配置让那个exe文件定期执行,这样做会不会有问
题?我在本机测试了下,好像可以
但如果拿到刚装完操作系统的Server上,Server需要安装那些东西?

source用到的库文件除了system的外,还有个
using Sybase.Data.AseClient.dll
在bin目录下有个Sybase.Data.AseClient.dll的文件。(这些都是照搬的,不太了解细节)
问得可能比较幼稚,见笑了
...全文
311 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
zsuswy 2010-04-20
  • 打赏
  • 举报
回复
Copy一把:

一、新建项目

打开VS2005,新建一个“Windows 服务”项目。

二、添加Timer

展开“工具箱”,在“组件”标签下找到“Timer”双击,这时就添加了一个Timer组件,修改“Name”属性为“timEmail”、“Enabled”为“false”、“Interval”为“60000”。

接下来要做一些修补工作,不知是VS2005的BUG还是我没找着地方,在VS2003下是不存在该问题的:刚从“组件”下添加的“Timer”按理说应该来自“System.Timers命名空间”,也只有“System.Timers.Timer”才能在Windows服务程序中正常工作,但是现在这个Timer却是属于“System.Windows.Forms.Timer”的。所以得稍作修改,打开“.Designer.cs”文件,修改如下:

#region 组件设计器生成的代码
//........以上略
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
//this.timEmail = new System.Windows.Forms.Timer(this.components);原
this.timEmail = new System.Timers.Timer();//改
this.timEmail.Interval = 60000;
this.ServiceName = "Service1";
}
#endregion
//private System.Windows.Forms.Timer timEmail;原
private System.Timers.Timer timEmail;//改 三、添加配置文件

服务每次调用配置文件,获取一些基本参数,这样一些变更就可直接修改配置文件而不必修改代码。新建ServiceConfig.xml存放于项目“Bin\Debug\”下:

<?xml version="1.0" encoding="utf-8" ?>
<serviceConfig>
<serviceItem
name="sendEmail"
enable="true"
elapsed="60000"
connectionString="your database connection..."
smtp="smtp address"
account="your email account..."
password="your password..." >
</serviceItem>
</serviceConfig>四、以下是实现代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Xml;//操作配置文件
using System.IO;//写日志
using System.Threading;//使用线程

namespace ClientWindowsService
{
public partial class ClientService : ServiceBase
{
public ClientService()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
//服务启动
this.timEmail.Enabled = true;
this.tSendEmail();
}

protected override void OnStop()
{
//服务停止
this.timEmail.Enabled = false;
}

private void timEmail_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
//定时器
this.tSendEmail();
}

//开启新进程发送邮件
private void tSendEmail()
{
Thread t = new Thread(new ThreadStart(sendEmail));
t.Start();
}

//发送邮件函数
private void sendEmail()
{
XmlDocument doc = new XmlDocument();
//添加System.Windows.Forms引用,获取执行目录
string configFile = System.Windows.Forms.Application.StartupPath.ToString() + "\ServiceConfig.xml";
doc.Load(@configFile);
XmlElement root = doc.DocumentElement;
foreach (XmlNode node in root)
{
//如果配置文件中开启服务
if (node.Attributes["name"].Value == "sendEmail" && node.Attributes["enable"].Value == "true")
{
try
{
//读取数据库,发送邮件操作,略
}
catch (Exception error)
{
//写错误日志
using (StreamWriter sw = new StreamWriter(System.Windows.Forms.Application.StartupPath.ToString() + @"" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", true, System.Text.Encoding.UTF8))
{
sw.WriteLine(DateTime.Now.ToString() + ":");
sw.WriteLine(error.ToString());
sw.WriteLine("---------------------------------------------");
sw.Close();
}
}
}
}//end foreach
}

}//end class
}//end namespace五、布署服务

在设计模式下右键-->添加安装程序-->设置serviceProcessInstaller1的Account为LocalSystem

设置serviceInstaller1的StartType为Automatic

编译

在命令模式下执行:%systemroot%\microsoft.net\framework\v2.0.50727\installUtil.exe D:\项目目录\bin\Debug\可执行文件名.exe
在每次需要修改Windows服务时,这就会要求你卸载和重新安装这个服务。不过要注意在卸载这个服务前,最好确保服务管理控制台已经关闭,这会是一个很好的习惯。如果没有这样操作的话,你可能在卸载和重安装Windows服务时会遇到麻烦。仅卸载服务的话,可以执行相的InstallUtil命令用于注销服务,不过要在后面加一个/u命令开关。

调试Windows服务

  从另外的角度度看,调试Windows服务绝不同于一个普通的应用程序。调试Windows服务要求的步骤更多。服务不能象你对普通应用程序做的那样,只要简单地在开发环境下执行就可以调试了。服务必须首先被安装和启动,这一点在前面部分我们已经做到了。为了便于跟踪调试代码,一旦服务被启动,你就要用Visual Studio把运行的进程附加进来(attach)。记住,对你的Windows服务做的任何修改都要对这个服务进行卸载和重安装。
wuyq11 2010-04-19
  • 打赏
  • 举报
回复
使用window服务定时执行
配置相关组件和安装框架
添加安装部署项目生成安装程序安装

17,741

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 .NET Framework
社区管理员
  • .NET Framework社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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