UnityWebApiController.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using Newtonsoft.Json;
  4. using Newtonsoft.Json.Linq;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.IO;
  9. using System.Runtime.Serialization.Formatters.Binary;
  10. using System.Reflection;
  11. using Microsoft.Extensions.Configuration;
  12. using Microsoft.AspNetCore.Authorization;
  13. using System.Collections;
  14. using Cksoft.Unity;
  15. using DllUnityBll;
  16. using DllHsmsWeb;
  17. using Cksoft.Unity.Log4NetConfig;
  18. //using DllMsgPackage;
  19. //using HttpHelp = DllUnityBll.HttpHelp;
  20. namespace DllUnityWebApi
  21. {
  22. [Route("UnityWebApi/[controller]/[action]")]
  23. [ApiController]
  24. [AllowAnonymous]
  25. public class UnityWebApiController : ControllerBase
  26. {
  27. //[HttpGet]
  28. //public ActionResult<IEnumerable<string>> Get()
  29. //{
  30. // return new string[] { "value1", "value2", "第一个WEBAPI" };
  31. //}
  32. #region 测试样例,供参考
  33. [AcceptVerbs("GET", "HEAD")]
  34. [ActionName("TestFun")]
  35. public ActionResult<IEnumerable<string>> TestFun()
  36. {
  37. return new string[] { "value1", "value2", "分离后,使用函数名访问的第一个WEBAPI" };
  38. }
  39. [AcceptVerbs("POST", "HEAD")]
  40. [ActionName("OperFun")]
  41. public ActionResult<JObject> OperFun(JObject pjson)
  42. {
  43. HttpRequest request = HttpContext.Request;
  44. //return new string[] { "value1", "value2", "分离后,使用函数名访问的第一个WEBAPI" };
  45. //string fcode = pjson["fcode"].ToString();
  46. JObject temp = new JObject();
  47. temp.Add("name", "张细泽");
  48. return temp;
  49. }
  50. [AcceptVerbs("POST", "HEAD")]
  51. [ActionName("ReadData")]
  52. public ActionResult<string> ReadData(JObject pars)
  53. {
  54. HttpRequest request = HttpContext.Request;
  55. string sqlstr = request.Form["sqlstr"].ToString();
  56. string tablename = request.Form["tablename"].ToString();
  57. UnityBll acc = new UnityBll();
  58. string errorinfo = "";
  59. //string sqlstr = "SELECT * FROM msgdata";
  60. //string tablename = "msgdata";
  61. DataSet reds = acc.Select(sqlstr, tablename, ref errorinfo);
  62. //if (tablename == "msgdata")
  63. // Thread.Sleep(1000 * 10);
  64. //return new string[] { "value1", "value2", "分离后,使用函数名访问的第一个WEBAPI" };
  65. //string fcode = pjson["fcode"].ToString();
  66. //JObject temp = new JObject();
  67. //temp.Add("name", "张细泽");
  68. //byte[] datas = MsgPackage.MsgPackage.DataSetToBytes(reds, ref errorinfo);
  69. //reds.Tables[0].ToJson();
  70. byte[] datas = LocDataSetToBytes(reds, ref errorinfo);
  71. DataSet ttds = LocBytesToDataSet(datas, ref errorinfo);
  72. return reds.Tables[0].ToJson();
  73. }
  74. [AcceptVerbs("POST", "HEAD")]
  75. [ActionName("ReadData03")]
  76. //[DisableRequestSizeLimit]
  77. [RequestSizeLimit(100_000_000)]
  78. public ActionResult<string> ReadData03(string value)
  79. {
  80. //DataSet ttds = GetDataSet();
  81. HttpRequest request = HttpContext.Request;
  82. long len = (long)request.ContentLength;
  83. byte[] datas = new byte[len];
  84. long lev = len;
  85. while (lev > 0)
  86. {
  87. byte[] buffers = null;
  88. int max = 0;
  89. if (lev <= 1024)
  90. {
  91. buffers = new byte[lev];
  92. max = (int)lev;
  93. }
  94. else
  95. {
  96. buffers = new byte[1024];
  97. max = 1024;
  98. }
  99. int readlen = request.Body.Read(buffers, 0, max);
  100. if (readlen > 0)
  101. {
  102. Array.Copy(buffers, 0, datas, len - lev, readlen);
  103. }
  104. lev = lev - readlen;
  105. }
  106. string jsonstr = System.Text.Encoding.Default.GetString(datas);
  107. DataTable ttdt = JsonConvert.DeserializeObject<DataTable>(jsonstr);
  108. return ttdt.ToJson();
  109. ////for(int i=0;i<datas.Length;i++)
  110. ////{
  111. //// WriteLog(datas[i].ToString());
  112. ////}
  113. //string errorinf = "";
  114. //DataSet tempds = DllMsgPackage.MsgPackage.BytesToDataSet(datas,ref errorinf);
  115. //string sqlstr = System.Text.Encoding.Default.GetString(datas);
  116. //DataTable tempdt = JsonConvert.DeserializeObject<DataTable>(sqlstr);
  117. //DataTable redt = JsonConvert.DeserializeObject<DataTable>(sqlstr);
  118. //return redt.ToJson();
  119. }
  120. //把数据集序列化成二进制流
  121. private byte[] LocDataSetToBytes(DataSet ds, ref string errorinfo)
  122. {
  123. try
  124. {
  125. BinaryFormatter ser = new BinaryFormatter();
  126. MemoryStream unCompressMS = new MemoryStream();
  127. ds.RemotingFormat = SerializationFormat.Binary;
  128. ser.Serialize(unCompressMS, ds);
  129. byte[] bytes = unCompressMS.ToArray();
  130. unCompressMS.Close();
  131. return bytes;
  132. }
  133. catch (Exception ex)
  134. {
  135. errorinfo = ex.ToString();
  136. return null;
  137. }
  138. }
  139. //将二进制转换为DataSet
  140. private DataSet LocBytesToDataSet(byte[] datas, ref string errorinfo)
  141. {
  142. try
  143. {
  144. MemoryStream ms = new MemoryStream(datas);
  145. BinaryFormatter bf = new BinaryFormatter();
  146. object obj = bf.Deserialize(ms);
  147. DataSet ds = (DataSet)obj;
  148. ms.Close();
  149. return ds;
  150. }
  151. catch (Exception ex)
  152. {
  153. errorinfo = ex.Message.ToString();
  154. return null;
  155. }
  156. }
  157. private DataSet GetDataSet()
  158. {
  159. string path = "d:\\web.txt";
  160. StreamReader sr = new StreamReader(path);
  161. String line;
  162. byte[] datas = new byte[19499];
  163. for (int i = 0; i < 19499; i++)
  164. {
  165. line = sr.ReadLine();
  166. datas[i] = byte.Parse(line);
  167. }
  168. //while ((line = sr.ReadLine()) != null)
  169. //{
  170. // Console.WriteLine(line.ToString());
  171. //}
  172. string errorinfo = "";
  173. DataSet tempds = LocBytesToDataSet(datas, ref errorinfo);
  174. return tempds;
  175. }
  176. public void WriteLog(string strLog)
  177. {
  178. string sFileName = "d:\\web.txt";
  179. FileStream fs;
  180. StreamWriter sw;
  181. if (System.IO.File.Exists(sFileName))
  182. {
  183. fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write);
  184. }
  185. else
  186. {
  187. fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write);
  188. }
  189. sw = new StreamWriter(fs);
  190. sw.WriteLine(strLog);
  191. sw.Close();
  192. fs.Close();
  193. }
  194. [AcceptVerbs("POST", "HEAD")]
  195. [ActionName("ReadData02")]
  196. public ActionResult<byte[]> ReadData02(JObject pars)
  197. {
  198. HttpRequest request = HttpContext.Request;
  199. string sqlstr = request.Form["sqlstr"].ToString();
  200. string tablename = request.Form["tablename"].ToString();
  201. UnityBll acc = new UnityBll();
  202. string errorinfo = "";
  203. //string sqlstr = "SELECT * FROM msgdata";
  204. //string tablename = "msgdata";
  205. DataSet reds = acc.Select(sqlstr, tablename, ref errorinfo);
  206. //return new string[] { "value1", "value2", "分离后,使用函数名访问的第一个WEBAPI" };
  207. //string fcode = pjson["fcode"].ToString();
  208. //JObject temp = new JObject();
  209. //temp.Add("name", "张细泽");
  210. byte[] datas = DllMsgPackage.MsgPackage.DataSetToBytes(reds, ref errorinfo);
  211. //reds.Tables[0].ToJson();
  212. return datas;
  213. }
  214. #endregion
  215. [AcceptVerbs("POST", "HEAD")]
  216. [ActionName("Select")]
  217. //[DisableRequestSizeLimit]
  218. //[RequestSizeLimit(100_000_000)]
  219. public ActionResult<string> Select()
  220. {
  221. try
  222. {
  223. HttpRequest request = HttpContext.Request;
  224. string errorinfo = "";
  225. DataSet orgds = HttpHelp.GetDataSetFromHttpRequest(request, ref errorinfo);
  226. if (errorinfo != "")
  227. {
  228. JObject errobj = new JObject();
  229. errobj.Add("errorinfo", errorinfo);
  230. return JsonConvert.SerializeObject(errobj);
  231. }
  232. DataRow orgrow = orgds.Tables[0].Rows[0];
  233. string dbcode = orgrow["dbcode"].ToString();
  234. string sqlstr = orgrow["sqlstr"].ToString();
  235. string tablename = orgrow["tablename"].ToString();
  236. UnityBll acc = new UnityBll();
  237. DataSet reds = acc.Select(dbcode, sqlstr, tablename, ref errorinfo);
  238. if (errorinfo != "")
  239. {
  240. JObject errobj = new JObject();
  241. errobj.Add("errorinfo", errorinfo);
  242. return JsonConvert.SerializeObject(errobj);
  243. }
  244. string resultstr = MsgPackage.DataSetToStr(reds, ref errorinfo);
  245. if (errorinfo != "")
  246. {
  247. JObject errobj = new JObject();
  248. errobj.Add("errorinfo", errorinfo);
  249. return JsonConvert.SerializeObject(errobj);
  250. }
  251. return resultstr;
  252. }
  253. catch (Exception ex)
  254. {
  255. JObject errobj = new JObject();
  256. errobj.Add("errorinfo", ex.Message.ToString());
  257. return JsonConvert.SerializeObject(errobj);
  258. }
  259. }
  260. [AcceptVerbs("POST", "HEAD")]
  261. [ActionName("SelectForEntity")]
  262. //[DisableRequestSizeLimit]
  263. //[RequestSizeLimit(100_000_000)]
  264. public ActionResult<string> SelectForEntity()
  265. {
  266. try
  267. {
  268. HttpRequest request = HttpContext.Request;
  269. string errorinfo = "";
  270. string jsonstr = HttpHelp.GetJsonStrFromHttpRequest(request, ref errorinfo);
  271. if (errorinfo != "")
  272. {
  273. JObject errobj = new JObject();
  274. errobj.Add("errorinfo", errorinfo);
  275. return JsonConvert.SerializeObject(errobj);
  276. }
  277. SqlStr mysqlstr = JsonConvert.DeserializeObject<SqlStr>(jsonstr);
  278. string dbcode = mysqlstr.dbcode;
  279. string sqlstr = mysqlstr.sqlstr;
  280. string tablename = mysqlstr.tablename;
  281. UnityBll acc = new UnityBll();
  282. IEnumerable<object> redt = acc.SelectForEntity<object>(dbcode, sqlstr, tablename, ref errorinfo);
  283. JObject resultobj = new JObject();
  284. if (errorinfo != "")
  285. {
  286. resultobj.Add("errorinfo", errorinfo);
  287. return JsonConvert.SerializeObject(resultobj);
  288. }
  289. //string resultstr = JsonConvert.SerializeObject("");
  290. string resultstr = JsonConvert.SerializeObject(redt);
  291. if (errorinfo != "")
  292. {
  293. resultobj.Add("errorinfo", errorinfo);
  294. return JsonConvert.SerializeObject(resultobj);
  295. }
  296. resultobj.Add("resultentitys", resultstr);
  297. return JsonConvert.SerializeObject(resultobj);
  298. }
  299. catch (Exception ex)
  300. {
  301. JObject errobj = new JObject();
  302. errobj.Add("errorinfo", ex.Message.ToString());
  303. return JsonConvert.SerializeObject(errobj);
  304. }
  305. }
  306. [AcceptVerbs("POST", "HEAD")]
  307. [ActionName("SelectForCondition")]
  308. //[DisableRequestSizeLimit]
  309. //[RequestSizeLimit(100_000_000)]
  310. public ActionResult<string> SelectForCondition()
  311. {
  312. try
  313. {
  314. HttpRequest request = HttpContext.Request;
  315. string errorinfo = "";
  316. string jsonstr = HttpHelp.GetJsonStrFromHttpRequest(request, ref errorinfo);
  317. if (errorinfo != "")
  318. {
  319. JObject errobj = new JObject();
  320. errobj.Add("errorinfo", errorinfo);
  321. return JsonConvert.SerializeObject(errobj);
  322. }
  323. SqlStr mysqlstr = JsonConvert.DeserializeObject<SqlStr>(jsonstr);
  324. string dbcode = mysqlstr.dbcode;
  325. string sqlstr = mysqlstr.sqlstr;
  326. string tablename = mysqlstr.tablename;
  327. JObject resultobj = new JObject();
  328. object entity = EntityHelper.CreateEntity(mysqlstr.dllname, mysqlstr.namespacename, mysqlstr.classname, ref errorinfo);
  329. if (entity == null)
  330. {
  331. resultobj.Add("errorinfo", errorinfo);
  332. return JsonConvert.SerializeObject(resultobj);
  333. }
  334. UnityBll acc = new UnityBll();
  335. IEnumerable<object> redt = acc.SelectForEntity<object>(dbcode, mysqlstr.condition, entity, ref errorinfo);
  336. if (errorinfo != "")
  337. {
  338. resultobj.Add("errorinfo", errorinfo);
  339. return JsonConvert.SerializeObject(resultobj);
  340. }
  341. //string resultstr = JsonConvert.SerializeObject("");
  342. string resultstr = JsonConvert.SerializeObject(redt);
  343. if (errorinfo != "")
  344. {
  345. resultobj.Add("errorinfo", errorinfo);
  346. return JsonConvert.SerializeObject(resultobj);
  347. }
  348. resultobj.Add("resultentitys", resultstr);
  349. return JsonConvert.SerializeObject(resultobj);
  350. }
  351. catch (Exception ex)
  352. {
  353. JObject errobj = new JObject();
  354. errobj.Add("errorinfo", ex.Message.ToString());
  355. return JsonConvert.SerializeObject(errobj);
  356. }
  357. }
  358. [AcceptVerbs("POST", "HEAD")]
  359. [ActionName("CallFunction")]
  360. //[DisableRequestSizeLimit]
  361. //[RequestSizeLimit(100_000_000)]
  362. public ActionResult<string> CallFunction()
  363. {
  364. try
  365. {
  366. HttpRequest request = HttpContext.Request;
  367. string errorinfo = "";
  368. DataSet orgds = HttpHelp.GetDataSetFromHttpRequest(request, ref errorinfo);
  369. if (errorinfo != "")
  370. {
  371. JObject errobj = new JObject();
  372. errobj.Add("errorinfo", errorinfo);
  373. return JsonConvert.SerializeObject(errobj);
  374. }
  375. //根据调用信息动态加载类库及方法
  376. //Assembly.LoadFrom()
  377. DataSet reds = CallFunction(orgds, ref errorinfo);
  378. if (errorinfo != "")
  379. {
  380. JObject errobj = new JObject();
  381. errobj.Add("errorinfo", errorinfo);
  382. return JsonConvert.SerializeObject(errobj);
  383. }
  384. string resultstr = MsgPackage.DataSetToStr(reds, ref errorinfo);
  385. if (errorinfo != "")
  386. {
  387. JObject errobj = new JObject();
  388. errobj.Add("errorinfo", errorinfo);
  389. return JsonConvert.SerializeObject(errobj);
  390. }
  391. return resultstr;
  392. }
  393. catch (Exception ex)
  394. {
  395. JObject errobj = new JObject();
  396. errobj.Add("errorinfo", ex.Message.ToString());
  397. return JsonConvert.SerializeObject(errobj);
  398. }
  399. }
  400. private DataSet CallFunction(DataSet ds, ref string errorinfo)
  401. {
  402. int language = 0;
  403. try
  404. {
  405. if (null == ds)
  406. {
  407. errorinfo = "传入数据为空。";
  408. return null;
  409. }
  410. //language = int.Parse(ds.Tables["&order&"].Rows[0]["language"].ToString());
  411. string dllname = ds.Tables["&order&"].Rows[0]["dllname"].ToString();//完整的类名称,包括名称空间
  412. string methodname = ds.Tables["&order&"].Rows[0]["methodname"].ToString();//要调用的方法名称
  413. //methodname = GetOthersName(dllname, methodname);
  414. //int userid = int.Parse(ds.Tables["&order&"].Rows[0]["userid"].ToString());
  415. //int companyid = int.Parse(ds.Tables["&order&"].Rows[0]["companyid"].ToString());
  416. string[] strs = dllname.Split('.');
  417. string filePrefix = AppConfigurtaionServices.Configuration.GetValue<string>("Prifix");
  418. string filepath = filePrefix + strs[0] + ".dll";
  419. filepath = AppDomain.CurrentDomain.BaseDirectory + @"\" + strs[0] + ".dll";
  420. //dllname = strs[1];
  421. ///////文件流调用模式,这样可以直接覆盖文件而无需重启服务器
  422. //string filename = GetFileName(filepath);
  423. //byte[] filebytes = DllAssemblyAcc.AssemblyList.GetFileStream(filepath, filename, ref errorinfo);
  424. //if (filebytes == null)
  425. //{
  426. // return null;
  427. //}
  428. //Assembly assemblyObj = Assembly.Load(filebytes);
  429. //byte[] filebytes = File.ReadAllBytes(filepath);
  430. //Assembly assemblyObj = Assembly.Load(filebytes);
  431. //直接加载文件调用,需要重启服务器
  432. Assembly assemblyObj = Assembly.LoadFrom(filepath);
  433. if (assemblyObj == null)
  434. {
  435. errorinfo = "未找到指定名称的程序集!";
  436. return null;
  437. }
  438. string dbcode = "sqlconn";
  439. //string connstring = AppConfigurtaionServices.Configuration.GetConnectionString(dbcode);
  440. Type asstype = assemblyObj.GetType(dllname);//Type
  441. object[] paramlists = { dbcode };
  442. object obj = (object)Activator.CreateInstance(asstype, paramlists);
  443. //object obj = (object)assemblyObj.CreateInstance(dllname); //反射创建
  444. if (obj == null)
  445. {
  446. errorinfo = "创建指定的类实例发生错误!";
  447. return null;
  448. }
  449. MethodInfo method = obj.GetType().GetMethod(methodname);
  450. object[] pars = new object[] { ds, "", 1 };
  451. object returnval = method.Invoke(obj, pars);
  452. errorinfo = (string)pars[1];
  453. int errortype = (int)pars[2];
  454. if (errorinfo != "")
  455. {
  456. return null;
  457. }
  458. return (DataSet)returnval;
  459. }
  460. catch (Exception ex)
  461. {
  462. errorinfo = ex.Message.ToString();
  463. return null;
  464. }
  465. }
  466. [AcceptVerbs("POST", "HEAD")]
  467. [ActionName("CallFunctionForEntity")]
  468. //[DisableRequestSizeLimit]
  469. //[RequestSizeLimit(100_000_000)]
  470. public ActionResult<string> CallFunctionForEntity()
  471. {
  472. try
  473. {
  474. HttpRequest request = HttpContext.Request;
  475. string errorinfo = "";
  476. string jsonstr = HttpHelp.GetJsonStrFromHttpRequest(request, ref errorinfo);
  477. if (errorinfo != "")
  478. {
  479. JObject errobj = new JObject();
  480. errobj.Add("errorinfo", errorinfo);
  481. return JsonConvert.SerializeObject(errobj);
  482. }
  483. Hashtable orgds = JsonConvert.DeserializeObject<Hashtable>(jsonstr);
  484. //根据调用信息动态加载类库及方法
  485. //Assembly.LoadFrom()
  486. Hashtable reds = CallFunctionForEntity(orgds, ref errorinfo);
  487. if (errorinfo != "")
  488. {
  489. JObject errobj = new JObject();
  490. errobj.Add("errorinfo", errorinfo);
  491. return JsonConvert.SerializeObject(errobj);
  492. }
  493. string resultstr = JsonConvert.SerializeObject(reds);
  494. if (errorinfo != "")
  495. {
  496. JObject errobj = new JObject();
  497. errobj.Add("errorinfo", errorinfo);
  498. return JsonConvert.SerializeObject(errobj);
  499. }
  500. return resultstr;
  501. }
  502. catch (Exception ex)
  503. {
  504. JObject errobj = new JObject();
  505. errobj.Add("errorinfo", ex.Message.ToString());
  506. return JsonConvert.SerializeObject(errobj);
  507. }
  508. }
  509. private Hashtable CallFunctionForEntity(Hashtable ds, ref string errorinfo)
  510. {
  511. int language = 0;
  512. try
  513. {
  514. if (null == ds)
  515. {
  516. errorinfo = "传入数据为空。";
  517. return null;
  518. }
  519. //string str = (string)ds[""];
  520. ConstOrder order = JsonConvert.DeserializeObject<ConstOrder>((string)ds["ConstOrder"]);
  521. string dllname = order.dllname;//完整的类名称,包括名称空间
  522. string methodname = order.methodname;//要调用的方法名称
  523. string[] strs = dllname.Split('.');
  524. string filename = "";
  525. for (int i = 0; i < strs.Length - 1; i++)
  526. {
  527. if (filename == "")
  528. {
  529. filename = strs[i];
  530. }
  531. else
  532. {
  533. filename += "." + strs[i];
  534. }
  535. }
  536. string filepath = @"E:\DotNetProject\DotNetCore\WebMainFrame\WebMainFrame\bin\Debug\netcoreapp2.1\" + filename + ".dll";
  537. filepath = AppDomain.CurrentDomain.BaseDirectory + @"\" + filename + ".dll";
  538. //dllname = strs[1];
  539. ///////文件流调用模式,这样可以直接覆盖文件而无需重启服务器
  540. //string filename = GetFileName(filepath);
  541. //byte[] filebytes = DllAssemblyAcc.AssemblyList.GetFileStream(filepath, filename, ref errorinfo);
  542. //if (filebytes == null)
  543. //{
  544. // return null;
  545. //}
  546. //Assembly assemblyObj = Assembly.Load(filebytes);
  547. //byte[] filebytes = File.ReadAllBytes(filepath);
  548. //Assembly assemblyObj = Assembly.Load(filebytes);
  549. //直接加载文件调用,需要重启服务器
  550. Assembly assemblyObj = Assembly.LoadFrom(filepath);
  551. if (assemblyObj == null)
  552. {
  553. errorinfo = "未找到指定名称的程序集!";
  554. return null;
  555. }
  556. string dbcode = "sqlconn";
  557. if (!string.IsNullOrEmpty(order.DbCode))
  558. {
  559. dbcode = order.DbCode;
  560. }
  561. //string connstring = AppConfigurtaionServices.Configuration.GetConnectionString(dbcode);
  562. Type asstype = assemblyObj.GetType(dllname);//Type
  563. object[] paramlists = { dbcode };
  564. object obj = (object)Activator.CreateInstance(asstype, paramlists);
  565. //object obj = (object)assemblyObj.CreateInstance(dllname); //反射创建
  566. if (obj == null)
  567. {
  568. errorinfo = "创建指定的类实例发生错误!";
  569. return null;
  570. }
  571. MethodInfo method = obj.GetType().GetMethod(methodname);
  572. object[] pars = new object[] { ds, "", 1 };
  573. object returnval = method.Invoke(obj, pars);
  574. errorinfo = (string)pars[1];
  575. int errortype = (int)pars[2];
  576. (obj as IDisposable).Dispose();
  577. if (errorinfo != "")
  578. {
  579. return null;
  580. }
  581. return (Hashtable)returnval;
  582. }
  583. catch (Exception ex)
  584. {
  585. errorinfo = ex.Message.ToString();
  586. return null;
  587. }
  588. }
  589. [AcceptVerbs("POST", "HEAD")]
  590. [ActionName("CallFunctionForOneEntity")]
  591. //[DisableRequestSizeLimit]
  592. //[RequestSizeLimit(100_000_000)]
  593. public ActionResult<string> CallFunctionForOneEntity()
  594. {
  595. try
  596. {
  597. HttpRequest request = HttpContext.Request;
  598. string errorinfo = "";
  599. string jsonstr = HttpHelp.GetJsonStrFromHttpRequest(request, ref errorinfo);
  600. if (errorinfo != "")
  601. {
  602. JObject errobj = new JObject();
  603. errobj.Add("errorinfo", errorinfo);
  604. return JsonConvert.SerializeObject(errobj);
  605. }
  606. Hashtable orgds = JsonConvert.DeserializeObject<Hashtable>(jsonstr);
  607. //根据调用信息动态加载类库及方法
  608. //Assembly.LoadFrom()
  609. Hashtable reds = CallFunctionForEntity1(orgds, ref errorinfo);
  610. if (errorinfo != "")
  611. {
  612. JObject errobj = new JObject();
  613. errobj.Add("errorinfo", errorinfo);
  614. return JsonConvert.SerializeObject(errobj);
  615. }
  616. string resultstr = JsonConvert.SerializeObject(reds);
  617. if (errorinfo != "")
  618. {
  619. JObject errobj = new JObject();
  620. errobj.Add("errorinfo", errorinfo);
  621. return JsonConvert.SerializeObject(errobj);
  622. }
  623. return resultstr;
  624. }
  625. catch (Exception ex)
  626. {
  627. JObject errobj = new JObject();
  628. errobj.Add("errorinfo", ex.Message.ToString());
  629. return JsonConvert.SerializeObject(errobj);
  630. }
  631. }
  632. private Hashtable CallFunctionForEntity1(Hashtable ds, ref string errorinfo)
  633. {
  634. try
  635. {
  636. if (null == ds)
  637. {
  638. errorinfo = "传入数据为空。";
  639. return null;
  640. }
  641. //string str = (string)ds[""];
  642. ConstOrder order = JsonConvert.DeserializeObject<ConstOrder>((string)ds[nameof(ConstOrder)]);
  643. string dllname = order.dllname;//完整的类名称,包括名称空间
  644. string methodname = order.methodname;//要调用的方法名称
  645. string[] strs = dllname.Split('.');
  646. string filename = "";
  647. for (int i = 0; i < strs.Length - 1; i++)
  648. {
  649. if (filename == "")
  650. {
  651. filename = strs[i];
  652. }
  653. else
  654. {
  655. filename += "." + strs[i];
  656. }
  657. }
  658. string filepath = @"E:\DotNetProject\DotNetCore\WebMainFrame\WebMainFrame\bin\Debug\netcoreapp2.1\" + filename + ".dll";
  659. filepath = AppDomain.CurrentDomain.BaseDirectory + @"\" + filename + ".dll";
  660. //直接加载文件调用,需要重启服务器
  661. Assembly assemblyObj = Assembly.LoadFrom(filepath);
  662. if (assemblyObj == null)
  663. {
  664. errorinfo = "未找到指定名称的程序集!";
  665. return null;
  666. }
  667. string dbcode = "sqlconn";
  668. string entityname = ds["entityname"].ToString();
  669. Type entitytype = Type.GetType(entityname);
  670. //object objtype= System.Activator.CreateInstance(entitytype);
  671. object orgobj = JsonConvert.DeserializeObject<object>((string)ds[entityname]);
  672. //string connstring = AppConfigurtaionServices.Configuration.GetConnectionString(dbcode);
  673. Type asstype = assemblyObj.GetType(dllname);//Type
  674. object[] paramlists = { dbcode };
  675. object obj = (object)Activator.CreateInstance(asstype, paramlists);
  676. //object obj = (object)assemblyObj.CreateInstance(dllname); //反射创建
  677. if (obj == null)
  678. {
  679. errorinfo = "创建指定的类实例发生错误!";
  680. return null;
  681. }
  682. MethodInfo method = obj.GetType().GetMethod(methodname);
  683. object[] pars = new object[] { orgobj, order, "", 1 };
  684. object returnval = method.Invoke(obj, pars);
  685. errorinfo = (string)pars[1];
  686. int errortype = (int)pars[2];
  687. (obj as IDisposable).Dispose();
  688. if (errorinfo != "")
  689. {
  690. return null;
  691. }
  692. Hashtable reds = new Hashtable();
  693. reds.Add("result", JsonConvert.SerializeObject(returnval));
  694. return reds;
  695. }
  696. catch (Exception ex)
  697. {
  698. errorinfo = ex.Message.ToString();
  699. return null;
  700. }
  701. }
  702. [AcceptVerbs("POST", "HEAD")]
  703. [ActionName("CallFunctionForEntityNoDb")]
  704. //[DisableRequestSizeLimit]
  705. //[RequestSizeLimit(100_000_000)]
  706. public ActionResult<string> CallFunctionForEntityNoDb()
  707. {
  708. try
  709. {
  710. HttpRequest request = HttpContext.Request;
  711. string errorinfo = "";
  712. string jsonstr = HttpHelp.GetJsonStrFromHttpRequest(request, ref errorinfo);
  713. if (errorinfo != "")
  714. {
  715. JObject errobj = new JObject();
  716. errobj.Add("errorinfo", errorinfo);
  717. LogHelper<UnityWebApiController>.LogError("解析请求出错:" + errorinfo, string.Empty, string.Empty);
  718. return JsonConvert.SerializeObject(errobj);
  719. }
  720. Hashtable orgds = JsonConvert.DeserializeObject<Hashtable>(jsonstr);
  721. //根据调用信息动态加载类库及方法
  722. //Assembly.LoadFrom()
  723. Hashtable reds = CallFunctionForEntityNoDb(orgds, ref errorinfo);
  724. if (errorinfo != "")
  725. {
  726. JObject errobj = new JObject();
  727. errobj.Add("errorinfo", errorinfo);
  728. LogHelper<UnityWebApiController>.LogError("调用CallFunctionForEntityNoDb方法出错:" + errorinfo, string.Empty, string.Empty);
  729. return JsonConvert.SerializeObject(errobj);
  730. }
  731. string resultstr = JsonConvert.SerializeObject(reds);
  732. if (errorinfo != "")
  733. {
  734. JObject errobj = new JObject();
  735. errobj.Add("errorinfo", errorinfo);
  736. return JsonConvert.SerializeObject(errobj);
  737. }
  738. return resultstr;
  739. }
  740. catch (Exception ex)
  741. {
  742. JObject errobj = new JObject();
  743. errobj.Add("errorinfo", ex.Message.ToString());
  744. return JsonConvert.SerializeObject(errobj);
  745. }
  746. }
  747. private Hashtable CallFunctionForEntityNoDb(Hashtable ds, ref string errorinfo)
  748. {
  749. try
  750. {
  751. if (null == ds)
  752. {
  753. errorinfo = "传入数据为空。";
  754. LogHelper<UnityWebApiController>.LogError("传入数据为空:" + ds.Keys.ToJson(), string.Empty, string.Empty);
  755. return null;
  756. }
  757. //string str = (string)ds[""];
  758. ConstOrder order = JsonConvert.DeserializeObject<ConstOrder>((string)ds["ConstOrder"]);
  759. string dllname = order.dllname;//完整的类名称,包括名称空间
  760. string methodname = order.methodname;//要调用的方法名称
  761. string[] strs = dllname.Split('.');
  762. string filename = "";
  763. for (int i = 0; i < strs.Length - 1; i++)
  764. {
  765. if (filename == "")
  766. {
  767. filename = strs[i];
  768. }
  769. else
  770. {
  771. filename += "." + strs[i];
  772. }
  773. }
  774. string filepath = @"E:\DotNetProject\DotNetCore\WebMainFrame\WebMainFrame\bin\Debug\netcoreapp2.1\" + filename + ".dll";
  775. filepath = AppDomain.CurrentDomain.BaseDirectory + @"\" + filename + ".dll";
  776. //直接加载文件调用,需要重启服务器
  777. Assembly assemblyObj = Assembly.LoadFrom(filepath);
  778. if (assemblyObj == null)
  779. {
  780. errorinfo = "未找到指定名称的程序集!";
  781. LogHelper<UnityWebApiController>.LogError("未找到指定名称的程序集", string.Empty, string.Empty);
  782. return null;
  783. }
  784. string dbcode = "sqlconn";
  785. if (!string.IsNullOrEmpty(order.DbCode))
  786. {
  787. dbcode = order.DbCode;
  788. }
  789. //string connstring = AppConfigurtaionServices.Configuration.GetConnectionString(dbcode);
  790. Type asstype = assemblyObj.GetType(dllname);//Type
  791. object obj = (object)Activator.CreateInstance(asstype, null);
  792. //object obj = (object)assemblyObj.CreateInstance(dllname); //反射创建
  793. if (obj == null)
  794. {
  795. LogHelper<UnityWebApiController>.LogError("创建指定的类实例发生错误", string.Empty, string.Empty);
  796. errorinfo = "创建指定的类实例发生错误!";
  797. return null;
  798. }
  799. MethodInfo method = obj.GetType().GetMethod(methodname);
  800. object[] pars = new object[] { ds, "" };
  801. object returnval = method.Invoke(obj, pars);
  802. errorinfo = (string)pars[1];
  803. LogHelper<UnityWebApiController>.LogError(errorinfo, string.Empty, string.Empty);
  804. return (Hashtable)returnval;
  805. }
  806. catch (Exception ex)
  807. {
  808. LogHelper<UnityWebApiController>.LogError("创建指定的类实例发生错误:" + ex.ToString(), string.Empty, string.Empty);
  809. errorinfo = ex.Message.ToString();
  810. return null;
  811. }
  812. }
  813. [AcceptVerbs("POST", "HEAD")]
  814. [ActionName("SendOrder")]
  815. //[DisableRequestSizeLimit]
  816. //[RequestSizeLimit(100_000_000)]
  817. public ActionResult<string> SendOrder()
  818. {
  819. try
  820. {
  821. HttpRequest request = HttpContext.Request;
  822. string errorinfo = "";
  823. string jsonstr = HttpHelp.GetJsonStrFromHttpRequest(request, ref errorinfo);
  824. if (errorinfo != "")
  825. {
  826. JObject errobj = new JObject();
  827. errobj.Add("errorinfo", errorinfo);
  828. return JsonConvert.SerializeObject(errobj);
  829. }
  830. Hashtable orgds = JsonConvert.DeserializeObject<Hashtable>(jsonstr);
  831. HsmsWeb temp = new HsmsWeb();
  832. Hashtable reds = temp.SendOrder(orgds, ref errorinfo);
  833. if (errorinfo != "")
  834. {
  835. JObject errobj = new JObject();
  836. errobj.Add("errorinfo", errorinfo);
  837. return JsonConvert.SerializeObject(errobj);
  838. }
  839. string resultstr = JsonConvert.SerializeObject(reds);
  840. if (errorinfo != "")
  841. {
  842. JObject errobj = new JObject();
  843. errobj.Add("errorinfo", errorinfo);
  844. return JsonConvert.SerializeObject(errobj);
  845. }
  846. return resultstr;
  847. }
  848. catch (Exception ex)
  849. {
  850. JObject errobj = new JObject();
  851. errobj.Add("errorinfo", ex.Message.ToString());
  852. return JsonConvert.SerializeObject(errobj);
  853. }
  854. }
  855. }
  856. }