123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.IO;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Reflection;
- using Microsoft.Extensions.Configuration;
- using Microsoft.AspNetCore.Authorization;
- using System.Collections;
- using Cksoft.Unity;
- using DllUnityBll;
- using DllHsmsWeb;
- using Cksoft.Unity.Log4NetConfig;
- //using DllMsgPackage;
- //using HttpHelp = DllUnityBll.HttpHelp;
- namespace DllUnityWebApi
- {
- [Route("UnityWebApi/[controller]/[action]")]
- [ApiController]
- [AllowAnonymous]
- public class UnityWebApiController : ControllerBase
- {
- //[HttpGet]
- //public ActionResult<IEnumerable<string>> Get()
- //{
- // return new string[] { "value1", "value2", "第一个WEBAPI" };
- //}
- #region 测试样例,供参考
- [AcceptVerbs("GET", "HEAD")]
- [ActionName("TestFun")]
- public ActionResult<IEnumerable<string>> TestFun()
- {
- return new string[] { "value1", "value2", "分离后,使用函数名访问的第一个WEBAPI" };
- }
- [AcceptVerbs("POST", "HEAD")]
- [ActionName("OperFun")]
- public ActionResult<JObject> OperFun(JObject pjson)
- {
- HttpRequest request = HttpContext.Request;
- //return new string[] { "value1", "value2", "分离后,使用函数名访问的第一个WEBAPI" };
- //string fcode = pjson["fcode"].ToString();
- JObject temp = new JObject();
- temp.Add("name", "张细泽");
- return temp;
- }
- [AcceptVerbs("POST", "HEAD")]
- [ActionName("ReadData")]
- public ActionResult<string> ReadData(JObject pars)
- {
- HttpRequest request = HttpContext.Request;
- string sqlstr = request.Form["sqlstr"].ToString();
- string tablename = request.Form["tablename"].ToString();
- UnityBll acc = new UnityBll();
- string errorinfo = "";
- //string sqlstr = "SELECT * FROM msgdata";
- //string tablename = "msgdata";
- DataSet reds = acc.Select(sqlstr, tablename, ref errorinfo);
- //if (tablename == "msgdata")
- // Thread.Sleep(1000 * 10);
- //return new string[] { "value1", "value2", "分离后,使用函数名访问的第一个WEBAPI" };
- //string fcode = pjson["fcode"].ToString();
- //JObject temp = new JObject();
- //temp.Add("name", "张细泽");
- //byte[] datas = MsgPackage.MsgPackage.DataSetToBytes(reds, ref errorinfo);
- //reds.Tables[0].ToJson();
- byte[] datas = LocDataSetToBytes(reds, ref errorinfo);
- DataSet ttds = LocBytesToDataSet(datas, ref errorinfo);
- return reds.Tables[0].ToJson();
- }
- [AcceptVerbs("POST", "HEAD")]
- [ActionName("ReadData03")]
- //[DisableRequestSizeLimit]
- [RequestSizeLimit(100_000_000)]
- public ActionResult<string> ReadData03(string value)
- {
- //DataSet ttds = GetDataSet();
- HttpRequest request = HttpContext.Request;
- long len = (long)request.ContentLength;
- byte[] datas = new byte[len];
- long lev = len;
- while (lev > 0)
- {
- byte[] buffers = null;
- int max = 0;
- if (lev <= 1024)
- {
- buffers = new byte[lev];
- max = (int)lev;
- }
- else
- {
- buffers = new byte[1024];
- max = 1024;
- }
- int readlen = request.Body.Read(buffers, 0, max);
- if (readlen > 0)
- {
- Array.Copy(buffers, 0, datas, len - lev, readlen);
- }
- lev = lev - readlen;
- }
- string jsonstr = System.Text.Encoding.Default.GetString(datas);
- DataTable ttdt = JsonConvert.DeserializeObject<DataTable>(jsonstr);
- return ttdt.ToJson();
- ////for(int i=0;i<datas.Length;i++)
- ////{
- //// WriteLog(datas[i].ToString());
- ////}
- //string errorinf = "";
- //DataSet tempds = DllMsgPackage.MsgPackage.BytesToDataSet(datas,ref errorinf);
- //string sqlstr = System.Text.Encoding.Default.GetString(datas);
- //DataTable tempdt = JsonConvert.DeserializeObject<DataTable>(sqlstr);
- //DataTable redt = JsonConvert.DeserializeObject<DataTable>(sqlstr);
- //return redt.ToJson();
- }
- //把数据集序列化成二进制流
- private byte[] LocDataSetToBytes(DataSet ds, ref string errorinfo)
- {
- try
- {
- BinaryFormatter ser = new BinaryFormatter();
- MemoryStream unCompressMS = new MemoryStream();
- ds.RemotingFormat = SerializationFormat.Binary;
- ser.Serialize(unCompressMS, ds);
- byte[] bytes = unCompressMS.ToArray();
- unCompressMS.Close();
- return bytes;
- }
- catch (Exception ex)
- {
- errorinfo = ex.ToString();
- return null;
- }
- }
- //将二进制转换为DataSet
- private DataSet LocBytesToDataSet(byte[] datas, ref string errorinfo)
- {
- try
- {
- MemoryStream ms = new MemoryStream(datas);
- BinaryFormatter bf = new BinaryFormatter();
- object obj = bf.Deserialize(ms);
- DataSet ds = (DataSet)obj;
- ms.Close();
- return ds;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- private DataSet GetDataSet()
- {
- string path = "d:\\web.txt";
- StreamReader sr = new StreamReader(path);
- String line;
- byte[] datas = new byte[19499];
- for (int i = 0; i < 19499; i++)
- {
- line = sr.ReadLine();
- datas[i] = byte.Parse(line);
- }
- //while ((line = sr.ReadLine()) != null)
- //{
- // Console.WriteLine(line.ToString());
- //}
- string errorinfo = "";
- DataSet tempds = LocBytesToDataSet(datas, ref errorinfo);
- return tempds;
- }
- public void WriteLog(string strLog)
- {
- string sFileName = "d:\\web.txt";
- FileStream fs;
- StreamWriter sw;
- if (System.IO.File.Exists(sFileName))
- {
- fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write);
- }
- else
- {
- fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write);
- }
- sw = new StreamWriter(fs);
- sw.WriteLine(strLog);
- sw.Close();
- fs.Close();
- }
- [AcceptVerbs("POST", "HEAD")]
- [ActionName("ReadData02")]
- public ActionResult<byte[]> ReadData02(JObject pars)
- {
- HttpRequest request = HttpContext.Request;
- string sqlstr = request.Form["sqlstr"].ToString();
- string tablename = request.Form["tablename"].ToString();
- UnityBll acc = new UnityBll();
- string errorinfo = "";
- //string sqlstr = "SELECT * FROM msgdata";
- //string tablename = "msgdata";
- DataSet reds = acc.Select(sqlstr, tablename, ref errorinfo);
- //return new string[] { "value1", "value2", "分离后,使用函数名访问的第一个WEBAPI" };
- //string fcode = pjson["fcode"].ToString();
- //JObject temp = new JObject();
- //temp.Add("name", "张细泽");
- byte[] datas = DllMsgPackage.MsgPackage.DataSetToBytes(reds, ref errorinfo);
- //reds.Tables[0].ToJson();
- return datas;
- }
- #endregion
- [AcceptVerbs("POST", "HEAD")]
- [ActionName("Select")]
- //[DisableRequestSizeLimit]
- //[RequestSizeLimit(100_000_000)]
- public ActionResult<string> Select()
- {
- try
- {
- HttpRequest request = HttpContext.Request;
- string errorinfo = "";
- DataSet orgds = HttpHelp.GetDataSetFromHttpRequest(request, ref errorinfo);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(errobj);
- }
- DataRow orgrow = orgds.Tables[0].Rows[0];
- string dbcode = orgrow["dbcode"].ToString();
- string sqlstr = orgrow["sqlstr"].ToString();
- string tablename = orgrow["tablename"].ToString();
- UnityBll acc = new UnityBll();
- DataSet reds = acc.Select(dbcode, sqlstr, tablename, ref errorinfo);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(errobj);
- }
- string resultstr = MsgPackage.DataSetToStr(reds, ref errorinfo);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(errobj);
- }
- return resultstr;
- }
- catch (Exception ex)
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", ex.Message.ToString());
- return JsonConvert.SerializeObject(errobj);
- }
- }
- [AcceptVerbs("POST", "HEAD")]
- [ActionName("SelectForEntity")]
- //[DisableRequestSizeLimit]
- //[RequestSizeLimit(100_000_000)]
- public ActionResult<string> SelectForEntity()
- {
- try
- {
- HttpRequest request = HttpContext.Request;
- string errorinfo = "";
- string jsonstr = HttpHelp.GetJsonStrFromHttpRequest(request, ref errorinfo);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(errobj);
- }
- SqlStr mysqlstr = JsonConvert.DeserializeObject<SqlStr>(jsonstr);
- string dbcode = mysqlstr.dbcode;
- string sqlstr = mysqlstr.sqlstr;
- string tablename = mysqlstr.tablename;
- UnityBll acc = new UnityBll();
- IEnumerable<object> redt = acc.SelectForEntity<object>(dbcode, sqlstr, tablename, ref errorinfo);
- JObject resultobj = new JObject();
- if (errorinfo != "")
- {
- resultobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(resultobj);
- }
- //string resultstr = JsonConvert.SerializeObject("");
- string resultstr = JsonConvert.SerializeObject(redt);
- if (errorinfo != "")
- {
- resultobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(resultobj);
- }
- resultobj.Add("resultentitys", resultstr);
- return JsonConvert.SerializeObject(resultobj);
- }
- catch (Exception ex)
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", ex.Message.ToString());
- return JsonConvert.SerializeObject(errobj);
- }
- }
- [AcceptVerbs("POST", "HEAD")]
- [ActionName("SelectForCondition")]
- //[DisableRequestSizeLimit]
- //[RequestSizeLimit(100_000_000)]
- public ActionResult<string> SelectForCondition()
- {
- try
- {
- HttpRequest request = HttpContext.Request;
- string errorinfo = "";
- string jsonstr = HttpHelp.GetJsonStrFromHttpRequest(request, ref errorinfo);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(errobj);
- }
- SqlStr mysqlstr = JsonConvert.DeserializeObject<SqlStr>(jsonstr);
- string dbcode = mysqlstr.dbcode;
- string sqlstr = mysqlstr.sqlstr;
- string tablename = mysqlstr.tablename;
- JObject resultobj = new JObject();
- object entity = EntityHelper.CreateEntity(mysqlstr.dllname, mysqlstr.namespacename, mysqlstr.classname, ref errorinfo);
- if (entity == null)
- {
- resultobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(resultobj);
- }
- UnityBll acc = new UnityBll();
- IEnumerable<object> redt = acc.SelectForEntity<object>(dbcode, mysqlstr.condition, entity, ref errorinfo);
- if (errorinfo != "")
- {
- resultobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(resultobj);
- }
- //string resultstr = JsonConvert.SerializeObject("");
- string resultstr = JsonConvert.SerializeObject(redt);
- if (errorinfo != "")
- {
- resultobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(resultobj);
- }
- resultobj.Add("resultentitys", resultstr);
- return JsonConvert.SerializeObject(resultobj);
- }
- catch (Exception ex)
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", ex.Message.ToString());
- return JsonConvert.SerializeObject(errobj);
- }
- }
- [AcceptVerbs("POST", "HEAD")]
- [ActionName("CallFunction")]
- //[DisableRequestSizeLimit]
- //[RequestSizeLimit(100_000_000)]
- public ActionResult<string> CallFunction()
- {
- try
- {
- HttpRequest request = HttpContext.Request;
- string errorinfo = "";
- DataSet orgds = HttpHelp.GetDataSetFromHttpRequest(request, ref errorinfo);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(errobj);
- }
- //根据调用信息动态加载类库及方法
- //Assembly.LoadFrom()
- DataSet reds = CallFunction(orgds, ref errorinfo);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(errobj);
- }
- string resultstr = MsgPackage.DataSetToStr(reds, ref errorinfo);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(errobj);
- }
- return resultstr;
- }
- catch (Exception ex)
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", ex.Message.ToString());
- return JsonConvert.SerializeObject(errobj);
- }
- }
- private DataSet CallFunction(DataSet ds, ref string errorinfo)
- {
- int language = 0;
- try
- {
- if (null == ds)
- {
- errorinfo = "传入数据为空。";
- return null;
- }
- //language = int.Parse(ds.Tables["&order&"].Rows[0]["language"].ToString());
- string dllname = ds.Tables["&order&"].Rows[0]["dllname"].ToString();//完整的类名称,包括名称空间
- string methodname = ds.Tables["&order&"].Rows[0]["methodname"].ToString();//要调用的方法名称
- //methodname = GetOthersName(dllname, methodname);
- //int userid = int.Parse(ds.Tables["&order&"].Rows[0]["userid"].ToString());
- //int companyid = int.Parse(ds.Tables["&order&"].Rows[0]["companyid"].ToString());
- string[] strs = dllname.Split('.');
- string filePrefix = AppConfigurtaionServices.Configuration.GetValue<string>("Prifix");
- string filepath = filePrefix + strs[0] + ".dll";
- filepath = AppDomain.CurrentDomain.BaseDirectory + @"\" + strs[0] + ".dll";
- //dllname = strs[1];
- ///////文件流调用模式,这样可以直接覆盖文件而无需重启服务器
- //string filename = GetFileName(filepath);
- //byte[] filebytes = DllAssemblyAcc.AssemblyList.GetFileStream(filepath, filename, ref errorinfo);
- //if (filebytes == null)
- //{
- // return null;
- //}
- //Assembly assemblyObj = Assembly.Load(filebytes);
- //byte[] filebytes = File.ReadAllBytes(filepath);
- //Assembly assemblyObj = Assembly.Load(filebytes);
- //直接加载文件调用,需要重启服务器
- Assembly assemblyObj = Assembly.LoadFrom(filepath);
- if (assemblyObj == null)
- {
- errorinfo = "未找到指定名称的程序集!";
- return null;
- }
- string dbcode = "sqlconn";
- //string connstring = AppConfigurtaionServices.Configuration.GetConnectionString(dbcode);
- Type asstype = assemblyObj.GetType(dllname);//Type
- object[] paramlists = { dbcode };
- object obj = (object)Activator.CreateInstance(asstype, paramlists);
- //object obj = (object)assemblyObj.CreateInstance(dllname); //反射创建
- if (obj == null)
- {
- errorinfo = "创建指定的类实例发生错误!";
- return null;
- }
- MethodInfo method = obj.GetType().GetMethod(methodname);
- object[] pars = new object[] { ds, "", 1 };
- object returnval = method.Invoke(obj, pars);
- errorinfo = (string)pars[1];
- int errortype = (int)pars[2];
- if (errorinfo != "")
- {
- return null;
- }
- return (DataSet)returnval;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- [AcceptVerbs("POST", "HEAD")]
- [ActionName("CallFunctionForEntity")]
- //[DisableRequestSizeLimit]
- //[RequestSizeLimit(100_000_000)]
- public ActionResult<string> CallFunctionForEntity()
- {
- try
- {
- HttpRequest request = HttpContext.Request;
- string errorinfo = "";
- string jsonstr = HttpHelp.GetJsonStrFromHttpRequest(request, ref errorinfo);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(errobj);
- }
- Hashtable orgds = JsonConvert.DeserializeObject<Hashtable>(jsonstr);
- //根据调用信息动态加载类库及方法
- //Assembly.LoadFrom()
- Hashtable reds = CallFunctionForEntity(orgds, ref errorinfo);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(errobj);
- }
- string resultstr = JsonConvert.SerializeObject(reds);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(errobj);
- }
- return resultstr;
- }
- catch (Exception ex)
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", ex.Message.ToString());
- return JsonConvert.SerializeObject(errobj);
- }
- }
- private Hashtable CallFunctionForEntity(Hashtable ds, ref string errorinfo)
- {
- int language = 0;
- try
- {
- if (null == ds)
- {
- errorinfo = "传入数据为空。";
- return null;
- }
- //string str = (string)ds[""];
- ConstOrder order = JsonConvert.DeserializeObject<ConstOrder>((string)ds["ConstOrder"]);
- string dllname = order.dllname;//完整的类名称,包括名称空间
- string methodname = order.methodname;//要调用的方法名称
- string[] strs = dllname.Split('.');
- string filename = "";
- for (int i = 0; i < strs.Length - 1; i++)
- {
- if (filename == "")
- {
- filename = strs[i];
- }
- else
- {
- filename += "." + strs[i];
- }
- }
- string filepath = @"E:\DotNetProject\DotNetCore\WebMainFrame\WebMainFrame\bin\Debug\netcoreapp2.1\" + filename + ".dll";
- filepath = AppDomain.CurrentDomain.BaseDirectory + @"\" + filename + ".dll";
- //dllname = strs[1];
- ///////文件流调用模式,这样可以直接覆盖文件而无需重启服务器
- //string filename = GetFileName(filepath);
- //byte[] filebytes = DllAssemblyAcc.AssemblyList.GetFileStream(filepath, filename, ref errorinfo);
- //if (filebytes == null)
- //{
- // return null;
- //}
- //Assembly assemblyObj = Assembly.Load(filebytes);
- //byte[] filebytes = File.ReadAllBytes(filepath);
- //Assembly assemblyObj = Assembly.Load(filebytes);
- //直接加载文件调用,需要重启服务器
- Assembly assemblyObj = Assembly.LoadFrom(filepath);
- if (assemblyObj == null)
- {
- errorinfo = "未找到指定名称的程序集!";
- return null;
- }
- string dbcode = "sqlconn";
- if (!string.IsNullOrEmpty(order.DbCode))
- {
- dbcode = order.DbCode;
- }
- //string connstring = AppConfigurtaionServices.Configuration.GetConnectionString(dbcode);
- Type asstype = assemblyObj.GetType(dllname);//Type
- object[] paramlists = { dbcode };
- object obj = (object)Activator.CreateInstance(asstype, paramlists);
- //object obj = (object)assemblyObj.CreateInstance(dllname); //反射创建
- if (obj == null)
- {
- errorinfo = "创建指定的类实例发生错误!";
- return null;
- }
- MethodInfo method = obj.GetType().GetMethod(methodname);
- object[] pars = new object[] { ds, "", 1 };
- object returnval = method.Invoke(obj, pars);
- errorinfo = (string)pars[1];
- int errortype = (int)pars[2];
- (obj as IDisposable).Dispose();
- if (errorinfo != "")
- {
- return null;
- }
- return (Hashtable)returnval;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- [AcceptVerbs("POST", "HEAD")]
- [ActionName("CallFunctionForOneEntity")]
- //[DisableRequestSizeLimit]
- //[RequestSizeLimit(100_000_000)]
- public ActionResult<string> CallFunctionForOneEntity()
- {
- try
- {
- HttpRequest request = HttpContext.Request;
- string errorinfo = "";
- string jsonstr = HttpHelp.GetJsonStrFromHttpRequest(request, ref errorinfo);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(errobj);
- }
- Hashtable orgds = JsonConvert.DeserializeObject<Hashtable>(jsonstr);
- //根据调用信息动态加载类库及方法
- //Assembly.LoadFrom()
- Hashtable reds = CallFunctionForEntity1(orgds, ref errorinfo);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(errobj);
- }
- string resultstr = JsonConvert.SerializeObject(reds);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(errobj);
- }
- return resultstr;
- }
- catch (Exception ex)
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", ex.Message.ToString());
- return JsonConvert.SerializeObject(errobj);
- }
- }
- private Hashtable CallFunctionForEntity1(Hashtable ds, ref string errorinfo)
- {
- try
- {
- if (null == ds)
- {
- errorinfo = "传入数据为空。";
- return null;
- }
- //string str = (string)ds[""];
- ConstOrder order = JsonConvert.DeserializeObject<ConstOrder>((string)ds[nameof(ConstOrder)]);
- string dllname = order.dllname;//完整的类名称,包括名称空间
- string methodname = order.methodname;//要调用的方法名称
- string[] strs = dllname.Split('.');
- string filename = "";
- for (int i = 0; i < strs.Length - 1; i++)
- {
- if (filename == "")
- {
- filename = strs[i];
- }
- else
- {
- filename += "." + strs[i];
- }
- }
- string filepath = @"E:\DotNetProject\DotNetCore\WebMainFrame\WebMainFrame\bin\Debug\netcoreapp2.1\" + filename + ".dll";
- filepath = AppDomain.CurrentDomain.BaseDirectory + @"\" + filename + ".dll";
- //直接加载文件调用,需要重启服务器
- Assembly assemblyObj = Assembly.LoadFrom(filepath);
- if (assemblyObj == null)
- {
- errorinfo = "未找到指定名称的程序集!";
- return null;
- }
- string dbcode = "sqlconn";
- string entityname = ds["entityname"].ToString();
- Type entitytype = Type.GetType(entityname);
- //object objtype= System.Activator.CreateInstance(entitytype);
- object orgobj = JsonConvert.DeserializeObject<object>((string)ds[entityname]);
- //string connstring = AppConfigurtaionServices.Configuration.GetConnectionString(dbcode);
- Type asstype = assemblyObj.GetType(dllname);//Type
- object[] paramlists = { dbcode };
- object obj = (object)Activator.CreateInstance(asstype, paramlists);
- //object obj = (object)assemblyObj.CreateInstance(dllname); //反射创建
- if (obj == null)
- {
- errorinfo = "创建指定的类实例发生错误!";
- return null;
- }
- MethodInfo method = obj.GetType().GetMethod(methodname);
- object[] pars = new object[] { orgobj, order, "", 1 };
- object returnval = method.Invoke(obj, pars);
- errorinfo = (string)pars[1];
- int errortype = (int)pars[2];
- (obj as IDisposable).Dispose();
- if (errorinfo != "")
- {
- return null;
- }
- Hashtable reds = new Hashtable();
- reds.Add("result", JsonConvert.SerializeObject(returnval));
- return reds;
- }
- catch (Exception ex)
- {
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- [AcceptVerbs("POST", "HEAD")]
- [ActionName("CallFunctionForEntityNoDb")]
- //[DisableRequestSizeLimit]
- //[RequestSizeLimit(100_000_000)]
- public ActionResult<string> CallFunctionForEntityNoDb()
- {
- try
- {
- HttpRequest request = HttpContext.Request;
- string errorinfo = "";
- string jsonstr = HttpHelp.GetJsonStrFromHttpRequest(request, ref errorinfo);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- LogHelper<UnityWebApiController>.LogError("解析请求出错:" + errorinfo, string.Empty, string.Empty);
- return JsonConvert.SerializeObject(errobj);
- }
- Hashtable orgds = JsonConvert.DeserializeObject<Hashtable>(jsonstr);
- //根据调用信息动态加载类库及方法
- //Assembly.LoadFrom()
- Hashtable reds = CallFunctionForEntityNoDb(orgds, ref errorinfo);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- LogHelper<UnityWebApiController>.LogError("调用CallFunctionForEntityNoDb方法出错:" + errorinfo, string.Empty, string.Empty);
- return JsonConvert.SerializeObject(errobj);
- }
- string resultstr = JsonConvert.SerializeObject(reds);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(errobj);
- }
- return resultstr;
- }
- catch (Exception ex)
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", ex.Message.ToString());
- return JsonConvert.SerializeObject(errobj);
- }
- }
- private Hashtable CallFunctionForEntityNoDb(Hashtable ds, ref string errorinfo)
- {
- try
- {
- if (null == ds)
- {
- errorinfo = "传入数据为空。";
- LogHelper<UnityWebApiController>.LogError("传入数据为空:" + ds.Keys.ToJson(), string.Empty, string.Empty);
- return null;
- }
- //string str = (string)ds[""];
- ConstOrder order = JsonConvert.DeserializeObject<ConstOrder>((string)ds["ConstOrder"]);
- string dllname = order.dllname;//完整的类名称,包括名称空间
- string methodname = order.methodname;//要调用的方法名称
- string[] strs = dllname.Split('.');
- string filename = "";
- for (int i = 0; i < strs.Length - 1; i++)
- {
- if (filename == "")
- {
- filename = strs[i];
- }
- else
- {
- filename += "." + strs[i];
- }
- }
- string filepath = @"E:\DotNetProject\DotNetCore\WebMainFrame\WebMainFrame\bin\Debug\netcoreapp2.1\" + filename + ".dll";
- filepath = AppDomain.CurrentDomain.BaseDirectory + @"\" + filename + ".dll";
- //直接加载文件调用,需要重启服务器
- Assembly assemblyObj = Assembly.LoadFrom(filepath);
- if (assemblyObj == null)
- {
- errorinfo = "未找到指定名称的程序集!";
- LogHelper<UnityWebApiController>.LogError("未找到指定名称的程序集", string.Empty, string.Empty);
- return null;
- }
- string dbcode = "sqlconn";
- if (!string.IsNullOrEmpty(order.DbCode))
- {
- dbcode = order.DbCode;
- }
- //string connstring = AppConfigurtaionServices.Configuration.GetConnectionString(dbcode);
- Type asstype = assemblyObj.GetType(dllname);//Type
- object obj = (object)Activator.CreateInstance(asstype, null);
- //object obj = (object)assemblyObj.CreateInstance(dllname); //反射创建
- if (obj == null)
- {
- LogHelper<UnityWebApiController>.LogError("创建指定的类实例发生错误", string.Empty, string.Empty);
- errorinfo = "创建指定的类实例发生错误!";
- return null;
- }
- MethodInfo method = obj.GetType().GetMethod(methodname);
- object[] pars = new object[] { ds, "" };
- object returnval = method.Invoke(obj, pars);
- errorinfo = (string)pars[1];
- LogHelper<UnityWebApiController>.LogError(errorinfo, string.Empty, string.Empty);
- return (Hashtable)returnval;
- }
- catch (Exception ex)
- {
- LogHelper<UnityWebApiController>.LogError("创建指定的类实例发生错误:" + ex.ToString(), string.Empty, string.Empty);
- errorinfo = ex.Message.ToString();
- return null;
- }
- }
- [AcceptVerbs("POST", "HEAD")]
- [ActionName("SendOrder")]
- //[DisableRequestSizeLimit]
- //[RequestSizeLimit(100_000_000)]
- public ActionResult<string> SendOrder()
- {
- try
- {
- HttpRequest request = HttpContext.Request;
- string errorinfo = "";
- string jsonstr = HttpHelp.GetJsonStrFromHttpRequest(request, ref errorinfo);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(errobj);
- }
- Hashtable orgds = JsonConvert.DeserializeObject<Hashtable>(jsonstr);
- HsmsWeb temp = new HsmsWeb();
- Hashtable reds = temp.SendOrder(orgds, ref errorinfo);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(errobj);
- }
- string resultstr = JsonConvert.SerializeObject(reds);
- if (errorinfo != "")
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", errorinfo);
- return JsonConvert.SerializeObject(errobj);
- }
- return resultstr;
- }
- catch (Exception ex)
- {
- JObject errobj = new JObject();
- errobj.Add("errorinfo", ex.Message.ToString());
- return JsonConvert.SerializeObject(errobj);
- }
- }
- }
- }
|