在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
ASP.NET MVC 中应用Windows服务以及Webservice服务开发分布式定时器 大家好这是我第二次写博客 ,写的不好请大家多多谅解, 希望大家可以多多指正。 二:我稍微的整理了一下关于这个分布式定时器需求: 3.第二种情况:根据不为空的电话号码则Webservice API 查询不到客户的CustomerGUID/CustomerID那么需要在过河兵系统的Survey表格中插入AnswerSheetID 和SurveyID 4.每天夜里23:30 定时更新任务跟踪管理系统的数据以及添加删除过河兵系统的数据 5.分布式定时器的作用是定时更新任务跟踪管理系统和过河兵系统的用户以及问卷的数据。 6.Windows 定时器的源代码 private static readonly SurveyHandle _surveyHandle = new SurveyHandle();
private static readonly WebServerInterFaceHandle _webServerInterFaceHandle = new WebServerInterFaceHandle();
public ServiceTime()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
//定义定时器
Timer myTimer = new Timer(1000);
myTimer.Elapsed += new ElapsedEventHandler(TaskTimeAction.ServiceTime);
myTimer.Enabled = true;
myTimer.AutoReset = true;
using (StreamWriter sw = new StreamWriter("D:\\任务跟踪管理定时器日志.txt", true))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "定时器服务启动");
}
}
protected override void OnStop()
{
using (StreamWriter sw = new StreamWriter("D:\\任务跟踪管理定时器日志.txt", true))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Stop.");
}
}
/// <summary>
/// 定时器委托任务 调用的方法
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
public static void ServiceTime(object source, ElapsedEventArgs e)
{
SurveyController surveyController = new SurveyController();
if (DateTime.Now.ToString("HH:mm:ss") == "23:30:00")
{
Content = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss" + "夜里23:30");
List<AnswerSheet> answerSheetList = _surveyHandle.GetAnswerGuidList();
SurveyAndAnswerSheet surveyAndAnswer = new SurveyAndAnswerSheet();
foreach (var items in answerSheetList)
{
#region 定时同步更新任务跟踪管理系统以及过河兵系统的用户以及问卷的数据
AnswerSheet answerSheet = _surveyHandle.GetAnswerSheet(items.ID);
Survey survey = _surveyHandle.GetSurvey(answerSheet.SurveyID);
//进行查询AnswerIndex中的详细的内容
//AnswerSheet answerSheetModel= _surveyHandle.GetAnswerSheetDetail(items.ID);
string model = _webServerInterFaceHandle.GetCustomerJson(items.CustomerID, items.CustomerPhone);
using (StreamWriter sw = new StreamWriter("D:\\任务跟踪管理系统定时器日志.txt", true))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "答卷编号:" + items.ID + "客户编号:" + model);
}
if (model != null)
{
string[] getCustomerID = model.Split('|');
string Image = null;
if (answerSheet.AttachList != null)
{
foreach (var image in answerSheet.AttachList)
{
Image += image.imgURL.Split('|').Last();
}
}
answerSheet.CustomerGUID = getCustomerID[0];
answerSheet.CustomerID = getCustomerID[1];
surveyAndAnswer.AnswerSheet = answerSheet;
surveyAndAnswer.Survey = survey;
surveyController.AnswerSheetEdit(surveyAndAnswer, "/Uploads/" + Image);
}
#endregion
}
using (StreamWriter sw = new StreamWriter("D:\\任务跟踪管理系统定时器日志.txt", true))
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + ":" + "结束时间");
}
}
}
private SelectCusutomer.WebUseSevrices webServices = new SelectCusutomer.JieChuDianUseSevrices();//真实环境
private SelectCustomer2.WebUseSevrices _webServices = new SelectCustomer2.JieChuDianUseSevrices();//测试环境
三:为什么要写Windows服务和Webservice 服务 <?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="ContactPoint" value="server=;database=TaskTrackData;uid=sa;password=sa;multipleactiveresultsets=True" />
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
</providers>
</roleManager>
</system.web>
</configuration>
六:安装服务通过命令
CMD 以管理员身份打开然后执行就可以了。
C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe D:\WindowsService\WindowsService.exe
C:\Windows\Microsoft.NET\Framework\v4.0.30319\installutil.exe -u D:\WindowsService\WindowsService.exe
以上内容均属于原创,转载请标明。谢谢! |
请发表评论