前两天接了个小任务,需求:
1、winform客户端传送操作指令到服务器端,服务器端备份原有数据库。
2、服务器根据传入参数计算更新备份数据库。
3、服务器发送备份数据到客户端。
需求很简单,也考虑过socket、wcf、webservice。也看了一些园子里面的贴,但没有搞明白这三种方式的区别和优缺点。(希望看到次贴的大虾们给小弟留个言万分感谢)最好还是选用webservice 原因很简单,开发快。
1、创建WebService项目
2、发布WebService
3、Web引用WebService
遇到问题:
1、发布后局域网其他机器无法访问。
解决办法:防火墙、匿名访问。
2、HttpContext没有被实例化、app.config配置文件错误。
解决办法:跟踪后发现项目中引用了webService项目的dll文件,导致调用的是dll文件中的方法”RuleService.DataSource.RuleSource web= new RuleService.DataSource.RuleSource();“ 所以调试是HttpContext报了没有被实例化的错误。因为HttpContext只是在响应用户请求的时候才会返回正确的实例,我这里就直接调用方法,所以报了没实例化的错误。应该直接调用web引用中的方法”WebReference1.RuleServer web = new WebReference1.RuleServer();“。app.config配置问题,configSections最好是根的第一个节点。
服务器端:
public RuleServer() { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); }
[WebMethod(Description = "获取RuleBase")] public byte[] GetRuleBase(List<RuleSource> ruleSources, string ProjectName) { RuleOperation ruleOperation = new RuleOperation(ruleSources, ProjectName);
return ruleOperation.Operation(); } public RuleServer() { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); }
[WebMethod(Description = "获取RuleBase")] public byte[] GetRuleBase(List<RuleSource> ruleSources, string ProjectName) { RuleOperation ruleOperation = new RuleOperation(ruleSources, ProjectName);
return ruleOperation.Operation(); }
public class RuleOperation { private List<RuleSource> _ruleSource; private string _path = "/WebService/bin/RuleDB/{0}.sdb"; private string _sourcePath = string.Empty; private string _distPath = string.Empty; private string _projectName = string.Empty;
public RuleOperation(List<RuleSource> ruleSources, string ProjectName) { _ruleSource = ruleSources; _projectName = ProjectName; _sourcePath = string.Format(_path, "BDES_Rule"); _distPath = string.Format(_path, ProjectName); }
public byte[] Operation() { if (_ruleSource.Count <= 0) return null; CopyRuleBase(); UpdateRuleBase(); return GetRuleBase(); }
/// <summary> /// 备份 /// </summary> public void CopyRuleBase() { File.Copy(HttpContext.Current.Server.MapPath(_sourcePath), HttpContext.Current.Server.MapPath(_distPath), true); }
/// <summary> /// 更新 /// </summary> public void UpdateRuleBase() { SqlLiteHelper sqlLiteHelper = new SqlLiteHelper(_projectName); sqlLiteHelper.CountRule(_ruleSource); }
#region 发送
/// <summary> /// 把文件转换成流 /// </summary> /// <returns></returns> public byte[] GetRuleBase() { return GetBinaryFile(HttpContext.Current.Server.MapPath(_distPath)); }
/// <summary> /// 将文件转化成2进制流 ConvertFileStreamToByteBuffer /// </summary> /// <param name="filename"></param> /// <returns></returns> public byte[] GetBinaryFile(string filename) { if (File.Exists(filename)) { try { //打开现有文件以进行读取。 FileStream s = File.OpenRead(filename); return ConvertStreamToByteBuffer(s); } catch { return new byte[0]; } } else { return new byte[0]; } }
/// <summary> /// 将流文件转化成2进制字节数组Convert FileStream Into Byte /// </summary> /// <param name="theStream"></param> /// <returns></returns> public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream) { int b1; System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
while ((b1 = theStream.ReadByte()) != -1) { tempStream.WriteByte(((byte)b1)); } return tempStream.ToArray(); } #endregion
}
客户端:
WebReference1.RuleServer web = new WebReference1.RuleServer();
byte[] rule = web.GetRuleBase(ruleSources, projectName); System.IO.File.WriteAllBytes("D:/" + projectName + ".sdb", rule);
this.txtRuleBaseName.Text = projectName;
MessageBox.Show("更新完成!"); WebReference1.RuleServer web = new WebReference1.RuleServer();
byte[] rule = web.GetRuleBase(ruleSources, projectName); System.IO.File.WriteAllBytes("D:/" + projectName + ".sdb", rule);
this.txtRuleBaseName.Text = projectName;
MessageBox.Show("更新完成!");