ASP.NET原创框架六-应用基类函数介绍

asp.net应用程序开发功能(ASP.NET原创框架六-应用基类函数介绍)(1)

接上集继续对框架基类函数介绍

本框架所有应用模块支持分布式部署,在web.config中可配置服务名

<add key="P2P服务名" value="WEB102" />

<add key="P2P任务消息启动" value="true" />

<add key="P2P是否处理系统任务" value="true" />

其中"P2P服务名":分布部署的服务名

"P2P任务消息启动":是否启用处理P2P任务消息

"P2P是否处理系统任务":是否处理来自系统的任务

任务和消息的派发请查阅ASP.NET原创框六-应用基类函数介绍

本框架同时支持API的分别部署

API执行方式包括通过MSMQ可靠消息队列方式,WCF方式,URL方式

public partial class XNNodeMsgServiceClass 处理MSMQ消息

通过反射动态加载API模块

动态记载API模块函数

private bool LoadMode()

{

m_RFModeApplicationClasses.Clear();

m_RFClassToModeApplication.Clear();

m_RFFullClassTypes.Clear();

m_RFModeShareClassTypes.Clear();

m_AllModeTypeArray.Clear();

m_BaseJsonFunctionHash.Clear();

m_ClassNameToRealName.Clear();

m_BaseJsonFunctionHash.Add("InitLock", "");

List<DirectoryInfo> m_ModePathList;//获得目录

GetJsonMode(out m_ModePathList);

int i = 0;

for (i = 0; i < m_ModePathList.Count; i )

{

DirectoryInfo TheFolder = (DirectoryInfo)m_ModePathList[i];

List<string> m_OneModeClassArray = null;

if (TheFolder.Name != "shareDll" && TheFolder.Name != "核心模块")

{

m_OneModeClassArray = new List<string>();

m_RFModeApplicationClasses.Add(TheFolder.Name, m_OneModeClassArray);

}

LoadOneJsonMode(TheFolder, m_OneModeClassArray);

}

return true;

}

private void LoadDataModel(DirectoryInfo TheFolder)

{

string filename;

foreach (FileInfo NextFile in TheFolder.GetFiles())

{

string rawfilename;

filename = NextFile.Name;

rawfilename = filename;

if (filename.IndexOf(".dll") == -1)

continue;

filename = TheFolder.FullName "\\" filename;

ArrayList m_TObjList = new ArrayList();

try

{

Assembly assembly = null;

assembly = Assembly.LoadFrom(filename);

foreach (Type t in assembly.GetTypes())

{

if (t.IsClass)

{

var attributes = t.GetCustomAttributes(typeof(TableAttribute), true);

if (attributes != null)

{

if (attributes.Length > 0)

{

TableAttribute myAttribute = attributes[0] as TableAttribute;

if (myAttribute != null)

{

string TableName = myAttribute.Name;

string ClassFullName = t.FullName;

RFOneDataModel m_RFOneDataModel = new RFOneDataModel();

m_RFOneDataModel.ClassFullName = ClassFullName;

m_RFOneDataModel.TableName = TableName;

m_RFOneDataModel.t = t;

m_RFDataModelDic[TableName] = m_RFOneDataModel;

}

}

}

}

}

}

catch (Exception e)

{

WriteLog("LoadShareDll:" e.ToString());

}

return;

}

}

private void LoadOneJsonMode(DirectoryInfo TheFolder, List<string> m_OneModeClassArray)

{

string filename = "";

string ModeName = TheFolder.Name;

string sRealModeName = ModeName.ToLower();

if (sRealModeName == "sharedll")

{

LoadDataModel(TheFolder);

return;

}

Dictionary<string, OneModeDll> m_DirModeDll = new Dictionary<string, OneModeDll>();

foreach (FileInfo NextFile in TheFolder.GetFiles())

{

string rawfilename;

filename = NextFile.Name;

rawfilename = filename;

if (filename.IndexOf(".dll") == -1)

continue;

filename = TheFolder.FullName "\\" filename;

ArrayList m_TObjList = new ArrayList();

try

{

Assembly assembly = null;

assembly = Assembly.LoadFrom(filename);

foreach (Type t in assembly.GetTypes())

{

try

{

BaseDllMode.BasePath = BasePath;

}

catch (System.Exception e)

{

}

Type m_TT = t;

bool bAdd = false;

while (true)

{

if (m_TT.BaseType.FullName != "System.Object")

{

if (m_TT.BaseType.FullName.IndexOf("BaseDllMode") != -1)

{

bAdd = true;

break;

}

else

m_TT = m_TT.BaseType;

}

else

break;

}

if (bAdd)

{

if (m_OneModeClassArray != null)

m_OneModeClassArray.Add(t.FullName);

m_TObjList.Add(t);

m_ClassNameToRealName[t.FullName.ToLower()] = t.FullName;

if (m_RFClassToModeApplication.ContainsKey(t.FullName))

{

m_RFClassToModeApplication[t.FullName] = ModeName;

}

else

m_RFClassToModeApplication.Add(t.FullName, ModeName);

}

}

Type[] m_ObjList = new Type[m_TObjList.Count];

OneModeDll m_OneModeDll = new OneModeDll();

m_OneModeDll.m_TypeArray = new List<Type>();

int nIndex = TheFolder.FullName.IndexOf(BasePath);

if (nIndex != -1)

{

string sModePath = TheFolder.FullName.Substring(BasePath.Length, TheFolder.FullName.Length - BasePath.Length);

m_OneModeDll.m_ModePath = sModePath;

}

int nCnt = m_TObjList.Count;

m_OneModeDll.m_JsonModeInfoArray = new List<RFOneJsonModeInfo>();

int i = 0;

for (i = 0; i < nCnt; i )

{

m_ObjList[i] = (Type)m_TObjList[i];

string Name = ((Type)m_TObjList[i]).FullName;

if (!m_RFFullClassTypes.ContainsKey(Name))

{

RFOneFullClassInfo m_RFOneFullClassInfo = new RFOneFullClassInfo();

m_RFOneFullClassInfo.ModePath = m_OneModeDll.m_ModePath;

m_RFOneFullClassInfo.filename = rawfilename;

m_RFOneFullClassInfo.t = m_ObjList[i];

m_RFOneFullClassInfo.ModeName = ModeName;

m_RFOneFullClassInfo.m_MethodInfos = Hashtable.Synchronized(new Hashtable());

m_RFOneFullClassInfo.m_FastDeletgates = Hashtable.Synchronized(new Hashtable());

m_RFOneFullClassInfo.m_InvokeInfos = Hashtable.Synchronized(new Hashtable());

m_RFFullClassTypes.Add(Name, m_RFOneFullClassInfo);

if (m_RFServerInfoST.m_ClassArray==null)

{

m_RFServerInfoST.m_ClassArray = new ArrayList();

}

m_RFServerInfoST.m_ClassArray.Add(Name);

}

if (!m_RFModeShareClassTypes.ContainsKey(Name))

{

RFOneFullClassInfo m_RFOneFullClassInfo = new RFOneFullClassInfo();

m_RFOneFullClassInfo.ModePath = m_OneModeDll.m_ModePath;

m_RFOneFullClassInfo.filename = rawfilename;

m_RFOneFullClassInfo.t = m_ObjList[i];

m_RFOneFullClassInfo.ModeName = ModeName;

m_RFOneFullClassInfo.m_MethodInfos = Hashtable.Synchronized(new Hashtable());

m_RFOneFullClassInfo.m_FastDeletgates = Hashtable.Synchronized(new Hashtable());

m_RFOneFullClassInfo.m_InvokeInfos = Hashtable.Synchronized(new Hashtable());

m_RFModeShareClassTypes.Add(Name, m_RFOneFullClassInfo);

}

MethodInfo TMethod;

Type m_T;

if (GetFunction(m_ObjList[i], "SetEngineInterface", out m_T))

{

m_T.InvokeMember("SetEngineInterface",

BindingFlags.DeclaredOnly |

BindingFlags.Public |

BindingFlags.Static | BindingFlags.InvokeMethod,

null,

null,

new Object[] { this });

}

if (GetFunction(m_ObjList[i], "SetCoreInterface", out m_T))

{

m_T.InvokeMember("SetCoreInterface",

BindingFlags.DeclaredOnly |

BindingFlags.Public |

BindingFlags.static | BindingFlags.InvokeMethod,

null,

null,

new Object[] { this });

}

if (GetFunction(m_ObjList[i], "SetJsonMode", out m_T))

{

m_T.InvokeMember("SetJsonMode",

BindingFlags.DeclaredOnly |

BindingFlags.Public |

BindingFlags.Static | BindingFlags.InvokeMethod,

null,

null,

new Object[] { m_OneModeDll.m_ModePath, m_ObjList[i].FullName });

}

if (!GetFunction(m_ObjList[i], "GetModeInfos", out m_T))

{

m_OneModeDll.m_JsonModeInfoArray.Add(null);

}

else

{

RFOneJsonModeInfo m_RFOneJsonModeInfo= m_T.InvokeMember("GetModeInfos",

BindingFlags.DeclaredOnly |

BindingFlags.Public |

BindingFlags.Static | BindingFlags.InvokeMethod,

null,

null,

new Object[] { }) as RFOneJsonModeInfo;

if (m_RFOneJsonModeInfo == null)

{

m_RFOneJsonModeInfo = new RFOneJsonModeInfo();

m_RFOneJsonModeInfo.m_EventInfoArray = new ArrayList();

m_RFOneJsonModeInfo.m_EventNameArray = new ArrayList();

m_RFOneJsonModeInfo.m_FunctionInfoArray = new ArrayList();

m_RFOneJsonModeInfo.m_FunctionNameArray = new ArrayList();

}

m_OneModeDll.m_JsonModeInfoArray.Add(m_RFOneJsonModeInfo);

}

m_OneModeDll.m_TypeArray.Add(m_ObjList[i]);

}

m_OneModeDll.filename = rawfilename;

//m_ModeTypeArray.Add(m_OneMode);

m_DirModeDll.Add(m_OneModeDll.m_ModePath, m_OneModeDll);

}

catch (System.Exception e)

{

WriteLog("LoadOneJsonMode:" e.ToString());

}

}

if (m_AllModeTypeArray.ContainsKey(ModeName))

{

m_AllModeTypeArray[ModeName] = m_DirModeDll;

}

else

m_AllModeTypeArray.Add(ModeName, m_DirModeDll);

}

动态执行API函数

public ReturnWCFJson RunFunction(string FullClassName, string FunctionName, Object m_InParame, IWCFEvent m_IWCFEvent = null)

{

ReturnWCFJson m_ReturnWCFJson = new ReturnWCFJson();

m_ReturnWCFJson.m_ReturnSTATE = ReturnSTATE.NOCALL;

RFOneFullClassInfo m_RFOneFullClassInfo;

if(!GetOneClassInfo(FullClassName,out m_RFOneFullClassInfo))

{

m_ReturnWCFJson.sMsg = "未找到要运行的类:" FullClassName;

return m_ReturnWCFJson;

}

try

{

return m_RFOneFullClassInfo.t.InvokeMember(FunctionName,

BindingFlags.DeclaredOnly |

BindingFlags.Public |

BindingFlags.Static | BindingFlags.InvokeMethod,

null,

null,

new Object[] { m_InParame, m_IWCFEvent }) as ReturnWCFJson;

}

catch (Exception e)

{

string sError = "调用报错:RunFunction:类" m_RFOneFullClassInfo.t.FullName ",函数:" FunctionName "," e.ToString();

WriteLog(sError);

m_ReturnWCFJson.sMsg = sError;

return m_ReturnWCFJson;

}

}

API派生自BaseDllMode

如一个API例子CoreDLL派生自BaseDllMode

函数返回ReturnWCFJson,函数输入参数是Object m_Parame,IWCFEvent m_IWCFEvent

m_IWCFEvent为WCF消息接口,用于发送自定义消息

public partial class CoreDLL : BaseDllMode

{

static private string m_JsonModePath, m_JsonModeName;

static private bool bInitDB = false;

static private ReaderWriterLockSlim m_RW = new ReaderWriterLockSlim();

static private Dictionary<string, NodeIDStringFactory> m_IDFactoryDIC = new Dictionary<string, NodeIDStringFactory>();

static public void SetJsonMode(string sPath, string sName)

{

m_JsonModePath = sPath;

m_JsonModeName = sName;

}

[BaseModeMethod("CanNoLogin:true,CanEnable:true")]

static public ReturnWCFJson HelloWorld(Object m_Parame,IWCFEvent m_IWCFEvent)

{

ReturnWCFJson m_ReturnWCFJson = new ReturnWCFJson();

m_ReturnWCFJson.m_ReturnSTATE = ReturnSTATE.CALLRETOK;

m_ReturnWCFJson.sMsg = "你好,模块内调用";

//WriteLog("调用函数HelloWorld", false);

if (m_IWCFEvent != null)

m_IWCFEvent.PostEvent("Hello", m_ReturnWCFJson);

int i;

List<OneRow> m_List = new List<OneRow>();

for (i = 0; i < 50;i )

{

OneRow m_OneRow = new OneRow();

m_OneRow.A = i;

m_OneRow.Name = "名:" i.ToString();

m_List.Add(m_OneRow);

}

m_ReturnWCFJson.m_OBJ = m_List;

//m_ReturnWCFJson.m_OBJ = "你哈";

return m_ReturnWCFJson;

}

[BaseModeMethod("CanNoLogin:true,CanEnable:true")]

static public ReturnWCFJson HelloRows(Object m_Parame, IWCFEvent m_IWCFEvent)

{

ReturnWCFJson m_ReturnWCFJson = new ReturnWCFJson();

m_ReturnWCFJson.m_ReturnSTATE = ReturnSTATE.CALLRETOK;

m_ReturnWCFJson.sMsg = "你好,模块内调用";

//WriteLog("调用函数HelloWorld", false);

if (m_IWCFEvent != null)

m_IWCFEvent.PostEvent("Hello", m_ReturnWCFJson);

int i;

List<OneRow> m_List = new List<OneRow>();

for (i = 0; i < 50; i )

{

OneRow m_OneRow = new OneRow();

m_OneRow.A = i;

m_OneRow.Name = "名:" i.ToString();

m_List.Add(m_OneRow);

}

m_ReturnWCFJson.m_OBJ = m_List;

//m_ReturnWCFJson.m_OBJ = "你哈";

return m_ReturnWCFJson;

}

[BaseModeMethod("CanNoLogin:true,CanEnable:true")]

static public ReturnWCFJson CreateNodeUUID(Object m_Parame, IWCFEvent m_IWCFEvent)

{

ReturnWCFJson m_ReturnWCFJson = new ReturnWCFJson();

m_ReturnWCFJson.m_ReturnSTATE = ReturnSTATE.NOCALL;

try

{

Hashtable m_HH = new Hashtable();

JsonHelper.OBJToHashTable(m_Parame,m_HH);

string dbno = null;

string tradecode=null;

if(KeyIsOK(m_HH,"tradecode"))

{

tradecode= m_HH["tradecode"].ToString();

}

else

{

m_ReturnWCFJson.m_ReturnSTATE = ReturnSTATE.CALLRETFAIL;

m_ReturnWCFJson.sMsg = "未赋行业编码";

return m_ReturnWCFJson;

}

if(KeyIsOK(m_HH,"dbno"))

{

dbno = m_HH["dbno"].ToString();

}

else

{

m_ReturnWCFJson.m_ReturnSTATE = ReturnSTATE.CALLRETFAIL;

m_ReturnWCFJson.sMsg = "未赋库编码";

return m_ReturnWCFJson;

}

string id=createRealUUID(dbno, tradecode);

if(string.IsNullOrWhiteSpace(id))

{

m_ReturnWCFJson.m_ReturnSTATE = ReturnSTATE.CALLRETFAIL;

m_ReturnWCFJson.sMsg="生成id失败";

return m_ReturnWCFJson;

}

m_HH.Clear();

m_HH.Add("id", id);

m_HH.Add("dbno", dbno);

m_ReturnWCFJson.m_ReturnSTATE=ReturnSTATE.CALLRETOK;

m_ReturnWCFJson.m_OBJ = m_HH;

}

catch

{

//m_ReturnWCFJson.bOK = false;

m_ReturnWCFJson.sMsg = "创建id失败";

}

return m_ReturnWCFJson;

}

#region 获得表主键uuid

static private string createGlobalUUID()

{

string uuid = System.Guid.NewGuid().ToString().Replace("-", "");

return uuid;

}

static private string createGlobalTimeUUID()

{

string sDBNO = "000";// CallServerName;

string tradecode = "000";

return createRealUUID(sDBNO, tradecode);

}

static private string createRealUUID(string sDBNO,string tradecode)

{

if(tradecode=="SYS")

{

tradecode = "000";

}

string key = sDBNO "_" tradecode;

NodeIDStringFactory m_NodeIDStringFactory = null;

try

{

m_RW.EnterReadLock();

if (m_IDFactoryDIC.ContainsKey(key))

{

m_NodeIDStringFactory = m_IDFactoryDIC[key];

}

else

{

}

}

catch

{

}

finally

{

m_RW.ExitReadLock();

}

if (m_NodeIDStringFactory == null)

{

try

{

m_RW.ExitWriteLock();

if (m_IDFactoryDIC.ContainsKey(key))

{

m_NodeIDStringFactory = m_IDFactoryDIC[key];

}

}

catch

{

m_NodeIDStringFactory = new NodeIDStringFactory(sDBNO, tradecode);

m_IDFactoryDIC[key] = m_NodeIDStringFactory;

}

finally

{

m_RW.ExitWriteLock();

}

}

if (m_NodeIDStringFactory != null)

return m_NodeIDStringFactory.nextId();

else

{

return "";

}

}

static private string createNewIDFromTradeCode(string tradecode, out string sDBNO)

{

sDBNO = "";

{

OneDB m_OneDB;

if (tradecode == "SYS")

{

m_OneDB = NodeDBEngine.GetMinDBNO(null);

tradecode = "000";

}

else

{

m_OneDB = NodeDBEngine.GetMinDBNO(tradecode);

}

if (m_OneDB == null)

return "";

sDBNO = m_OneDB.dbno;

return createRealUUID(sDBNO, tradecode);

}

return "";

}

static private string createNewIDFromParentID(string id, string tradecode, out string sDBNO)

{

sDBNO = "";

DateTime t;

string tradeCodeOut;

NodeIDStringFactory.getIdInfo(id, out sDBNO, out tradeCodeOut, out t);

id = createRealUUID(sDBNO, tradecode);

if (tradecode == "SYS")

tradecode = "000";

id = NodeIDStringFactory.Replace(id, sDBNO, tradecode);

return id;

}

#endregion

}

class OneRow

{

public int A;

public string Name;

}

BaseJsonMode调用

一、调用MSMQ webapi 函数说明

protected static bool CallNodeMSMQWebAPIFunction(string sUrl, object m_Parame, string sReturnUrl = null);

sUrl:访问的url地址

m_Parame:输入参数

sReturnUrl:返回地址

protected static bool CallNodeMSMQWebAPIFunction(string sUrl, object m_Parame, out string msguuid, string sReturnUrl = null);

sUrl:访问的url地址

m_Parame:输入参数

msguuid:消息唯一id

sReturnUrl:返回地址

使用例子

ReturnJson m_ReturnJson = new ReturnJson();

Hashtable m_HH = new Hashtable();

m_HH.Add("hh", "你好");

BaseJsonMode.CallNodeMSMQWebAPIFunction("http://163.com", "程序");

二、调用URL

protected static bool PostUrlToMsgService(string sUrl, object m_OBJ, string sReturnUrl = null);

sUrl:访问地址

m_OBJ:参数

sReturnUrl:执行完毕返回url

例子:

ReturnJson m_ReturnJson = new ReturnJson();

m_ReturnJson.bOK = true;

Hashtable m_HH = new Hashtable();

m_HH.Add("hh", "你好");

PostUrlToMsgService("http://localhost:3664/MessageAndWork.MWMemo/Hello_RawJson.ajax", m_HH, "http://localhost:3664/MessageAndWork.MWMemo/Hello_RawJson.ajax");

三、MSMQ调用api

protected static bool CallNodeMSMQNodeAPIFunction(string fullclassname, string functionname, object m_Parame, string sReturnUrl = null);

fullclassname:类名

functionname:函数名

m_Parame:参数

sReturnUrl:执行后返回url

protected static bool CallNodeMSMQNodeAPIFunction(string fullclassname, string functionname, object m_Parame, out string msguuid, string sReturnUrl = null);

fullclassname:类名

functionname:函数名

m_Parame:参数

msguuid:执行返回的消息唯一id

sReturnUrl:执行后返回url

例子:

ReturnJson m_ReturnJson = new ReturnJson();

m_ReturnJson.bOK = true;

Hashtable m_HH = new Hashtable();

m_HH.Add("hh", "你好");

CallNodeMSMQNodeAPIFunction("NodeCoreDLL.CoreDLL", "HelloWorld", m_HH, "http://localhost:3664/MessageAndWork.MWMemo/Hello_RawJson.ajax");

四、通过web方式调用

protected static ReturnWeb CallNodeWebService(string FullClassName, string FunctionName, string Key, object m_Parame);

FullClassName:类名

FunctionName:函数名

Key:执行的唯一id

m_Parame:参数

使用例子

Hashtable m_HH = new Hashtable();

m_HH.Add("hh", "你好");

ReturnWeb m_ReturnWeb=CallNodeWebService("NodeCoreDLL.CoreDLL", "HelloWorld", "1", m_HH);

五、导出excel文件

static protected bool ExportFile(HttpContext ctx, DataSet pSet, List<Hashtable> m_ColOrderHHList, out string FileMsg)

pSet:数据

m_ColOrderHHList:对于列名对应的文字说明

FileMsg:导出的文件名或错误信息

,