C#으로 아래 그림1과 같은 윈도우 서비스 프로그래밍을 만들어보겠습니다.
그림 1
그럼 어떤 서비스를 만들어볼까요?
사이트 상태점검을 주기적으로 하는 서비스를 만들어 볼까 합니다.
서버관리자라면 한번쯤 경험해보셨을 퇴근 이후에 사이트 이상으로 인하여 고생하셨을 텐데요.
이 문제를 빠르게 캐치할 수 있게 도와주는 프로그램을 만들어 보겠습니다.
대략적인 기능은 아래와 같습니다.
1. Config.ini의 환경설정 파일을 둔다.
2. 주기적으로 지정된 사이트 페이지를 호출 해서 사이트 이상유무를 파악한다.
3. 사이트 이상유무 발생시 관리자에게 SMS나 E-Mail을 보낸다. 경우에 따라서는 웹 서버를 재부팅 할 수 도 있습니다.
자 그럼 만들어 볼까요?
.net Framework 1.1기반에 Visual Sudio .NET 2003으로 만들어 보겠습니다.
Visual Sudio .NET은 그림2와 같이 윈도우서비스 프로그래밍 템플릿을 기본적으로 제공합니다.
파일 -> 새로만들기 -> 프로젝트 -> Visual C# 프로젝트 -> Windows 서비스
그림2
프로젝트를 생성하게 되면 기본적으로 윈도우 서비스 프로그래밍을 하기 위한 소스가 제공됩니다.(그림3) 저희는 그 안에 구현될 로직만 삽입하면 되는거죠 ^^
그림3
1. Config.ini파일 생성 및 PropertyClass 생성
우선 환경설정 파일을 생성한 이후 ini파일을 가져오기 위한 class를 만들어 보겠습니다.
Config.ini파일을 그림4와 같이 생성해주세요.
heartbeat : 서비스가 주기적으로 호출할 시간입니다. 여기는 60초로 설정했네요
timeout : 지정된 url로 호출 시 요청에 대한 제한 시간을 설정합니다. 여기서는 30초로 설정했습니다.
Config.ini를 읽기위한 PropertyClass를 만들어 보겠습니다.
PropertyClass.cs
public class PropertyClass
{
public PropertyClass()
{
}
static string url;
static string timeOut_interval;
static string heartBeat_interval;
public static string Url
{
get{return url;}
set{url = value;}
}
public static string HeartBeat_interval
{
get{return heartBeat_interval;}
set{heartBeat_interval= value;}
}
public static string TimeOut_interval
{
get{return timeOut_interval;}
set{timeOut_interval= value;}
}
}
2. Config.ini을 읽어 드리기 위한 IniFile 구현
IniFile.cs
public class IniFile
{
public string path;
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,
string key,string val,string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,
string key,string def, StringBuilder retVal,
int size,string filePath);
public IniFile(string INIPath)
{
path = INIPath;
}
public void IniWriteValue(string Section,string Key,string Value)
{
WritePrivateProfileString(Section,Key,Value,this.path);
}
public string IniReadValue(string Section,string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,"",temp, 255, this.path);
return temp.ToString();
}
}
다음 강좌에서는 본격적으로 Thread를 이용하여 웹어플리케이션을 호출하는 Service를 만들어보겠습니다. ^^
'.net' 카테고리의 다른 글
Windows Service Programming With C# [2] (0) | 2011.01.20 |
---|---|
Ping Class (2) | 2009.01.23 |
XML XmlAttribute다루기(삭제, 수정, 추가) (0) | 2009.01.13 |