C# } } #端区 #地区邮件通知 /// ///邮寄通知 ///应用启动和退出时激发邮件 /// /// 无效邮件通知(字符串strStatus) { 如果(strStatus==“已启动”) { var template=新邮件模板() .WithB
} } #端区 #地区邮件通知 /// ///邮寄通知 ///应用启动和退出时激发邮件 /// /// 无效邮件通知(字符串strStatus) { 如果(strStatus==“已启动”) { var template=新邮件模板() .WithBody(“嗨,C# } } #端区 #地区邮件通知 /// ///邮寄通知 ///应用启动和退出时激发邮件 /// /// 无效邮件通知(字符串strStatus) { 如果(strStatus==“已启动”) { var template=新邮件模板() .WithB,c#,sql-server,windows-services,sqldependency,C#,Sql Server,Windows Services,Sqldependency,} } #端区 #地区邮件通知 /// ///邮寄通知 ///应用启动和退出时激发邮件 /// /// 无效邮件通知(字符串strStatus) { 如果(strStatus==“已启动”) { var template=新邮件模板() .WithBody(“嗨,部件索引器开始日期”+DateTime.Now.ToLongDateString()) .WithSubject(“部件索引器已启动”) .Wissender(“bbasupport@bba-reman.com) .有最近的(“t
部件索引器开始日期”+DateTime.Now.ToLongDateString()) .WithSubject(“部件索引器已启动”) .Wissender(“bbasupport@bba-reman.com) .有最近的(“tridip@bba-reman.com) .Send(); //eventLog1.WriteEntry(“邮件触发”); } 否则如果(strStatus==“已停止”) { var template=新邮件模板() .WithBody(“嗨,
部件索引器停止日期”+DateTime.Now.ToLongDateString()) .使用Subject(“部件索引器已停止”) .Wissender(“bbasupport@bba-reman.com) .有最近的(“tridip@bba-reman.com) .Send(); //eventLog1.WriteEntry(“邮件触发”); } } #端区 #顶部区域 受保护的覆盖void OnStop() { System.Data.SqlClient.SqlDependency.Stop(connectionString); 邮件通知(“停止”); //eventLog1.WriteEntry(“部件索引器停止日期:+DateTime.Now.ToLongDateString()); } #端区 } }
您在服务的恢复选项卡上设置了哪些选项?@Donal:不清楚您想问什么?@Donal:我没有为我的服务设置任何恢复选项,只是有默认设置。默认设置为:第一次失败--重新启动服务。如果我停止服务而不是重新启动,我的服务会再次启动吗?恢复选项卡仅在服务出现故障时使用-如果您手动关闭它,则不会使用。SQLDependency的问题是它只允许少量请求,我为监控应用程序提供了SQLDependencies,但是它被证明是容易出错的,而且很容易成功,我想微软说它一次只允许10个“依赖项”,然后它就重置了——我认为问题在于关闭连接
public partial class PartIndexer : ServiceBase
{
static string connectionString = "MyConnection String;Pooling=true;Connect Timeout=20;";
SqlDependency dep;
public PartIndexer()
{
InitializeComponent();
}
#region OnStart
protected override void OnStart(string[] args)
{
System.Data.SqlClient.SqlDependency.Stop(connectionString);
System.Data.SqlClient.SqlDependency.Start(connectionString);
RegisterNotification();
MailNotify("STARTED");
}
#endregion
#region RegisterNotification
/// <summary>
/// RegisterNotification
/// this is main routine which will monitor data change in ContentChangeLog table
/// </summary>
private void RegisterNotification()
{
string tmpdata = "";
//eventLog1.WriteEntry("RegisterNotification invoked");
try
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT ActivityDate FROM [bba-reman].ContentChangeLog";
dep = new SqlDependency(cmd);
dep.OnChange += new OnChangeEventHandler(OnDataChange);
SqlDataReader dr = cmd.ExecuteReader();
{
while (dr.Read())
{
if (dr[0] != DBNull.Value)
{
tmpdata = dr[0].ToString();
}
}
}
dr.Dispose();
cmd.Dispose();
}
}
finally
{
//SqlDependency.Stop(connStr);
}
}
#endregion
#region OnDataChange
/// <summary>
/// OnDataChange
/// OnDataChange will fire when after data change found in ContentChangeLog table
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void OnDataChange(object sender, SqlNotificationEventArgs e)
{
SqlDependency dep = sender as SqlDependency;
dep.OnChange -= new OnChangeEventHandler(OnDataChange);
SendMailNotification();
RegisterNotification();
}
#endregion
#region StartIndex
/// <summary>
/// StartIndex
/// this routine will call web service in bba reman website which will invoke routine to re-index data
/// </summary>
void SendMailNotification()
{
// does some job
}
#endregion
#region MailNotify
/// <summary>
/// MailNotify
/// fire mail when apps start & exit
/// </summary>
/// <param name="strStatus"></param>
void MailNotify(string strStatus)
{
if (strStatus == "STARTED")
{
var template = new MailTemplate()
.WithBody("HI,<br><br>Part Indexer Started Date " + DateTime.Now.ToLongDateString())
.WithSubject("Part Indexer Started")
.WithSender("xxxxx")
.WithRecepient("xxxx")
.Send();
}
else if (strStatus == "STOPPED")
{
var template = new MailTemplate()
.WithBody("HI,<br><br>Part Indexer stopped Date " + DateTime.Now.ToLongDateString())
.WithSubject("Part Indexer Stopped")
.WithSender("xxx")
.WithRecepient("xxx")
.Send();
}
}
#endregion
#region OnStop
protected override void OnStop()
{
System.Data.SqlClient.SqlDependency.Stop(connectionString);
MailNotify("STOPPED");
}
#endregion
}
void OnDataChange(object sender, SqlNotificationEventArgs e)
{
((SqlDependency)sender).OnChange -= OnDataChange;
//SqlDependency dep = sender as SqlDependency;
//dep.OnChange -= new OnChangeEventHandler(OnDataChange);
if (e.Source == SqlNotificationSource.Timeout)
{
// just restart notification
RegisterNotification();
return;
}
else if (e.Source != SqlNotificationSource.Data)
{
ReStartService();
}
SendMailNotification();
RegisterNotification();
}
if (e.Source == SqlNotificationSource.Timeout)
{
// just restart notification
RegisterNotification();
return;
}
else if (e.Source != SqlNotificationSource.Data)
{
Environment.Exit(1);
}
#region all using
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
#endregion
namespace PartIndexerService
{
public partial class PartIndexer : ServiceBase
{
static string connectionString = "server=sql08-2.orcsweb.com;uid=bba-reman;password=_bba_1227_kol;database=BBAreman;Pooling=true;Connect Timeout=20;";
SqlDependency dep;
public PartIndexer()
{
InitializeComponent();
}
#region OnStart
protected override void OnStart(string[] args)
{
RegisterNotification();
MailNotify("STARTED");
}
#endregion
#region RegisterNotification
/// <summary>
/// RegisterNotification
/// this is main routine which will monitor data change in ContentChangeLog table
/// </summary>
private void RegisterNotification()
{
string tmpdata = "";
System.Data.SqlClient.SqlDependency.Stop(connectionString);
System.Data.SqlClient.SqlDependency.Start(connectionString);
try
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT ActivityDate FROM [bba-reman].ContentChangeLog";
dep = new SqlDependency(cmd);
dep.OnChange += new OnChangeEventHandler(OnDataChange);
SqlDataReader dr = cmd.ExecuteReader();
{
while (dr.Read())
{
if (dr[0] != DBNull.Value)
{
tmpdata = dr[0].ToString();
}
}
}
dr.Dispose();
cmd.Dispose();
}
}
finally
{
//SqlDependency.Stop(connStr);
}
}
#endregion
public void ReStartService()
{
ServiceController service = new ServiceController("PartIndexer");
if ((service.Status.Equals(ServiceControllerStatus.Stopped)) || (service.Status.Equals(ServiceControllerStatus.StopPending)))
{
service.Start();
}
else
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
}
}
#region OnDataChange
/// <summary>
/// OnDataChange
/// OnDataChange will fire when after data change found in ContentChangeLog table
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void OnDataChange(object sender, SqlNotificationEventArgs e)
{
((SqlDependency)sender).OnChange -= OnDataChange;
if (e.Source == SqlNotificationSource.Timeout)
{
var template = new MailTemplate()
.WithBody("HI,<br><br>Part Indexer Service Exception Timeout occur " + DateTime.Now.ToLongDateString())
.WithSubject("Part Indexer Service Exception Timeout occur")
.WithSender("bbasupport@bba-reman.com")
.WithRecepient("tridip@bba-reman.com")
.Send();
RegisterNotification();
return;
}
else if (e.Source != SqlNotificationSource.Data)
{
var template = new MailTemplate()
.WithBody("HI,<br><br>Part Indexer Service Exception SqlNotificationSource.Data " + DateTime.Now.ToLongDateString())
.WithSubject("Part Indexer Service Exception SqlNotificationSource.Data")
.WithSender("bbasupport@bba-reman.com")
.WithRecepient("tridip@bba-reman.com")
.Send();
Environment.Exit(1);
}
StartIndex();
RegisterNotification();
}
#endregion
#region StartIndex
/// <summary>
/// StartIndex
/// this routine will call web service in bba reman website which will invoke routine to re-index data
/// </summary>
void StartIndex()
{
//eventLog1.WriteEntry("Web Service called start for indexing data");
PartIndexerWS.AuthHeader oAuth = new PartIndexerWS.AuthHeader();
oAuth.Username = "Admin";
oAuth.Password = "Admin";
PartIndexerWS.SearchDataIndex DataIndex = new PartIndexerWS.SearchDataIndex();
DataIndex.AuthHeaderValue = oAuth;
try
{
DataIndex.StartIndex();
//eventLog1.WriteEntry("Web Service called stop for indexing data");
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message.ToString());
//eventLog1.WriteEntry("Web Service call error "+ex.Message.ToString());
}
}
#endregion
#region MailNotify
/// <summary>
/// MailNotify
/// fire mail when apps start & exit
/// </summary>
/// <param name="strStatus"></param>
void MailNotify(string strStatus)
{
if (strStatus == "STARTED")
{
var template = new MailTemplate()
.WithBody("HI,<br><br>Part Indexer Started Date " + DateTime.Now.ToLongDateString())
.WithSubject("Part Indexer Started")
.WithSender("bbasupport@bba-reman.com")
.WithRecepient("tridip@bba-reman.com")
.Send();
//eventLog1.WriteEntry("mail fired ");
}
else if (strStatus == "STOPPED")
{
var template = new MailTemplate()
.WithBody("HI,<br><br>Part Indexer stopped Date " + DateTime.Now.ToLongDateString())
.WithSubject("Part Indexer Stopped")
.WithSender("bbasupport@bba-reman.com")
.WithRecepient("tridip@bba-reman.com")
.Send();
//eventLog1.WriteEntry("mail fired ");
}
}
#endregion
#region OnStop
protected override void OnStop()
{
System.Data.SqlClient.SqlDependency.Stop(connectionString);
MailNotify("STOPPED");
//eventLog1.WriteEntry("Part Indexer stopped Date : " + DateTime.Now.ToLongDateString());
}
#endregion
}
}