项目大概描述:把工厂数据导入到WEBDB中去, 供工厂的客户看生产进程
涉及技术:window service / web service / xml / silverlight / web DataGrid / CSS / …
一,windows service
为了达到时时更新数据,用到windows service 做为此功能程序的宿主方法如下:
1,新建立一个windows service 工程
(如果有需要可以对service1.cs文件进行重命名)双击打开service1.cs 进入design画面 右击如下图加一个add installer
(在这design画面里有一个转到code [click here to switch code view ] 的连接
在onstart下面做导的操作, 在这里新定义了一个类去操作
public partial class ImportHost : ServiceBase { public ImportHost() { InitializeComponent(); } protected override void OnStart(string[] args) { monitorData monitoData = new monitorData();//要完成的功能的类 } protected override void OnStop() { } }
)
再打开刚才add出来的installer 做如下设置 ,及设置后对应的服务的效果
在Program.cs加如下代码,表示当程序运行的时候去运行服务
static class Program { static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new ImportHost()//服务所在的类 }; ServiceBase.Run(ServicesToRun); } }
这样一个服务window服务就做好了
当然monitorData monitoData = new monitorData() 类里面要时时更新就得要定时器且这个定时器要是(类内全局的)去完成,下面讲一讲定时器的用法
Timer threadingTimer;(在vs里有几个定时器,如form的定时器 这里用的是using System.Threading;下的定时器)
threadingTimer = new Timer(new TimerCallback(DBTimerPoint), null, timerIntervalTemp, Timeout.Infinite); /*Callback:[DBTimerPoint]指定一个时间点到达的回调函数 State:一个包含回调方法要使用的信息的对象,或者为空引用(Visual Basic 中为 Nothing)。 dueTime:调用 callback 之前延迟的时间量(以毫秒为单位)。指定 Timeout.Infinite 以防止计时器开始计时。指定零 (0) 以立即启动计时器。 Period:指写时间间隔(以毫秒为单位)。指定 Timeout.Infinite表示只触发一次 */ public void DBTimerPoint(object sernder)//回调方法 { string errorString = string.Empty; Mutex DBOperateMutex = new Mutex(false, "DBOperateMutex"); //互斥类:好多个线程相互之间的联系,不产生冲突和重复,这需要用到互斥对象,即:System.Threading 命名空间中的 Mutex 类。 DBOperateMutex.WaitOne();//等待以DBOperateMutex命名的线程完成 try { ... } catch (Exception e) { .. } finally { DBOperateMutex.ReleaseMutex();//释放掉这个mutex,让下一个进行 threadingTimer = new Timer(new TimerCallback(DBTimerPoint), null, timerInterval, Timeout.Infinite); //触发下一计时器,且也只计时一次 } }
通过上面的注释可以看出,在这种情况下要注意进程的相互排斥
在这里还用到了xml作为配置文件,这里定义了一个clsXML的类,有静态方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml;//这个空间下面,在SL里是在System.Xml.Linq下面 using System.IO; class clsXML { public static string getvalue(string _xmlPath, string _id, string _propertyName) { string rntValue = string.Empty; if (Path.IsPathRooted(_xmlPath) == false)//是否是根目录 { if (_xmlPath.Trim().Substring(0, 1) == ".") // AppDomain.CurrentDomain.BaseDirectory应用程序的路径 _xmlPath = AppDomain.CurrentDomain.BaseDirectory + _xmlPath.Trim().Substring(1); else _xmlPath = AppDomain.CurrentDomain.BaseDirectory + _xmlPath.Trim(); } if (!File.Exists(_xmlPath))//判断文件是否存在 { return string.Empty; } XmlDocument xmlDoc = new XmlDocument();//新建立一个XmlDocumnet 在SL里不xdocument 而不是xmldocumnet xmlDoc.Load(_xmlPath);//加载XML文件 //得到id =指定值的 节点 XmlElement xmlnode = (XmlElement)xmlDoc.SelectSingleNode("//*[@id='" + _id.ToString().Trim() + "']"); if (xmlnode != null)//如果节点不为空 { rntValue = xmlnode.Attributes[_propertyName].Value;//得到指定属性的值 /*XML像这种 <SP id ="SP " tableName ="mytable" columNmae="*" ></SP_Master> */ if (rntValue.Trim() == "") rntValue = string.Empty; } return rntValue; } public static string getvalue(string _id, string _propertyName) { //windowsService.Properties.Settings.Default.configFilePat 得到以configFilePath命名的配置文件的路径 return clsXML.getvalue(windowsService.Properties.Settings.Default.configFilePath, _id, _propertyName); } public static string getvalue(string _id) { string rntValue = string.Empty; string _xmlPath = windowsServicePart.Properties.Settings.Default.configFilePath; if (Path.IsPathRooted(_xmlPath) == false) { if (_xmlPath.Trim().Substring(0, 1) == ".") _xmlPath = AppDomain.CurrentDomain.BaseDirectory + _xmlPath.Trim().Substring(1); else _xmlPath = AppDomain.CurrentDomain.BaseDirectory + _xmlPath.Trim(); } if (!File.Exists(_xmlPath)) { return string.Empty; } try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(_xmlPath); XmlElement xmlnode = (XmlElement)xmlDoc.SelectSingleNode("//*[@id='" + _id.ToString().Trim() + "']");//同上 if (xmlnode != null) { /*XML的样式 <webService id ="webServiceUrl"> http://192.168.230.15/WebserviceUsage/Service.asmx </webService> */ rntValue = xmlnode.InnerText;//直接得到它中间的内容 if (rntValue.Trim() == "") rntValue = string.Empty; } } catch { return string.Empty; } } }
上面是读一个XML的一些基本操作
二 web Service
请看对webService 的一点总结 对一些web service 的知识做一些分析
三 web
四 silverlight / web 和silverlight的互动
浙公网安备 33010602011771号