


Develop WeChat public platform with asp.net (2) Multi-layer architecture framework construction and entrance implementation
Feb 23, 2017 pm 02:03 PMWe have designed a relatively complete database in the previous article, and this article starts with the code. First, execute the database script designed in the previous article in the database to generate the database, and then create the project in VS. In order to facilitate understanding and viewing, I designed very straightforward class names and file names without namespace prefixes.
Adopt interface method, a total of 8 projects: 7 class libraries and one MVC project, respectively:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? to Access interface IBLL, specific implementation BLL ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? for
## ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????tityFramework context object and some cache management, business logic layer and data access I will hand over the interface production (implementation) work of the layer to Spring.NET injection implementation. After the framework is built, it is as follows:
After the framework is built, add the database and add it in DAL (note that DAL is not datamodel) New item, select data--ADO.NET entity data model:
Give it a name, let’s call it WeixinModel, select Generate from database, configure Connect the database to the previously generated database, follow the next step, and finally load it into edmx. Right-click on edmx - add code generation item, select code:
Select DbContext Generator, and then save Click edmx, then copy edmx and weixinmodel.tt to DataModel, delete edmx and weixinmodel.tt in DAL, open weixinmodel.tt in datamodel and save it. In addition, it needs to be declared in WeiXinModel.Context.cs retained in DAL. datamodel namespace.
Now that the framework and data model are available, add references according to the correct reference levels in DAL, IDAL, BLL, and IBLL, and write a few common methods, and then you can start using them in the display layer.
//添加 public T AddEntity<T>(DbContext db,T entity) where T : class { db.Entry<T>(entity).State = EntityState.Added; db.SaveChanges(); return entity; } //修改 public bool UpdateEntity<T>(DbContext db,T entity) where T : class { db.Set<T>().Attach(entity); db.Entry<T>(entity).State = EntityState.Modified; db.SaveChanges(); return true; } //刪除 public bool DeleteEntity<T>(DbContext db,T entity) where T : class { db.Set<T>().Attach(entity); db.Entry<T>(entity).State = EntityState.Deleted; db.SaveChanges(); return true; } // 返回一個對象 public T InfoEntities<T>(DbContext db, Expression<Func<T, bool>> whereLambda) where T : class { return db.Set<T>().Where<T>(whereLambda).FirstOrDefault(); }Write the interface and business logic layer accordingly.
現(xiàn)在來到顯示層,默認(rèn)的MVC項目是返回VIEW, 這里我們不需要返回頁面, 把home中的index改成Void返回類型, 接下去就是接收微信發(fā)來的請求進(jìn)行判斷了,驗證請求----接收POST數(shù)據(jù)---分析XML----解析成自己想要的數(shù)據(jù)
入口:首先驗證消息來源是微信服務(wù)器,然后解析收到的xml,解析成功有數(shù)據(jù)則執(zhí)行LookMsgType方法來進(jìn)行處理
private IBLL.IDoWei BLLWei { set; get; } public DbContext dbHome { get; set; } private string token { get; set; } Dictionary<string, string> xmlModel = new Dictionary<string, string>(); public void Index() { dbHome=FContext.WeiXinDbContext(); //xml字符串 string xmlData = string.Empty; //請求類型 string method=Request.HttpMethod.ToLower(); string signature = Request.QueryString["signature"]; string timestamp = Request.QueryString["timestamp"]; string nonce = Request.QueryString["nonce"]; //驗證接入和每次請求驗證真實性 if (method == "get") { if (CheckSign(signature,timestamp,nonce)) { Often.ResponseToEnd(Request.QueryString["echostr"]); } else { Response.Status = "403"; Often.ResponseToEnd(""); } } //處理接收到的POST消息 else if (method == "post") { using (Stream stream = Request.InputStream) { Byte[] byteData = new Byte[stream.Length]; stream.Read(byteData, 0, (Int32)stream.Length); xmlData = Encoding.UTF8.GetString(byteData); } if (!string.IsNullOrEmpty(xmlData)) { try { xmlModel = ReadXml.GetXmlModel(xmlData); } catch { //未能正確處理 給微信服務(wù)器回復(fù)默認(rèn)值 Often.ResponseToEnd(""); } } if (xmlModel.Count > 0) { string msgType = ReadXml.ReadModel("MsgType", xmlModel); LookMsgType(msgType); } } else//除了post和get外 如head皆視為非法請求 { Response.Status = "403"; Often.ResponseToEnd(""); } dbHome.Dispose(); }
這里用到的驗證方法:
/// <summary> /// 驗證簽名 /// </summary> /// <param name="signature"></param> /// <param name="timestamp"></param> /// <param name="nonce"></param> /// <returns></returns> public bool CheckSign(string signature, string timestamp, string nonce) { List<string> list = new List<string>(); list.Add(token); list.Add(timestamp); list.Add(nonce); //默認(rèn)排序 list.Sort(); string tmpStr = string.Empty; list.All(l => { tmpStr += l; return true; }); tmpStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1"); //驗證 if (tmpStr == signature) { return true; } return false; }
倉儲中的EF上下文:
public static DbContext WeiXinDbContext() { DbContext dbcontext =new WeiXinEntities(); //創(chuàng)建 dbcontext.Configuration.AutoDetectChangesEnabled = false;//自動檢測配置更改 dbcontext.Configuration.LazyLoadingEnabled = true;//延遲加載 dbcontext.Configuration.ValidateOnSaveEnabled = false;//自動跟蹤 return dbcontext; }
Common中的解析微信發(fā)來的XML方法
//把接收到的XML轉(zhuǎn)為字典 public static Dictionary<string, string> GetXmlModel(string xmlStr) { XmlDocument doc = new XmlDocument(); doc.LoadXml(xmlStr); Dictionary<string, string> mo = new Dictionary<string, string>(); var data = doc.DocumentElement.ChildNodes; for (int i = 0; i < data.Count; i++) { mo.Add(data.Item(i).LocalName, data.Item(i).InnerText); } return mo; } ////從字典中讀取指定的值 public static string ReadModel(string key, Dictionary<string, string> model) { string str = ""; model.TryGetValue(key, out str); if (str== null) str = ""; return str; }
好了,入口以及驗證相關(guān)的都解決了,下一篇開始微信消息處理LookMsgType方法實現(xiàn)
更多Develop WeChat public platform with asp.net (2) Multi-layer architecture framework construction and entrance implementation相關(guān)文章請關(guān)注PHP中文網(wǎng)!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
