diff --git a/Api/CsiConnection.cs b/Api/CsiConnection.cs index b014102..a40effc 100644 --- a/Api/CsiConnection.cs +++ b/Api/CsiConnection.cs @@ -8,24 +8,32 @@ namespace InSiteXmlClient4Core.Api /// /// 连接 /// - internal class CsiConnection : ICsiConnection + public class CsiConnection : ICsiConnection { private Hashtable _sessions = new Hashtable(); private string _host; private int _port; private int _timeout; + public CsiConnection(string host, int port) { this._host = host; this._port = port; this._timeout = 0; + } + /// + /// 记录会话 + /// + public bool LogSesssion + { + get { return mServerConnection.LogXml; } + set { mServerConnection.LogXml = value; } } + private ServerConnection mServerConnection = new ServerConnection(); - - - public ICsiSession CreateSession(string userName, string password, string sessionName) + public ICsiSession CreateSession(string userName, string password, string sessionName) { lock (this) { @@ -78,7 +86,6 @@ public int SetConnectionTimeout(int timeout) return _timeout; } - public string Submit(string requestXml) => - this.mServerConnection.Submit(this._host, this._port, requestXml); + public string Submit(string requestXml) => this.mServerConnection.Submit(this._host, this._port, requestXml); } } \ No newline at end of file diff --git a/Api/CsiRecordset.cs b/Api/CsiRecordset.cs new file mode 100644 index 0000000..8166758 --- /dev/null +++ b/Api/CsiRecordset.cs @@ -0,0 +1,212 @@ +using InSiteXmlClient4Core.Exceptions; +using InSiteXmlClient4Core.InterFace; +using System; +using System.Collections; +using System.Xml; + +namespace InSiteXmlClient4Core.Api +{ + internal class CsiRecordsetField : CsiXmlElement, ICsiRecordsetField, ICsiXmlElement + { + public CsiRecordsetField(ICsiDocument doc, XmlElement element) : base(doc, element) + { + + } + public CsiRecordsetField(ICsiDocument doc, string name, ICsiXmlElement parent) : base(doc, name, parent) + { + + } + + public virtual string GetName() + { + return this.GetElementName(); + } + + public virtual string GetValue() + { + string result = string.Empty; + try + { + XmlElement element = base.GetDomElement(); + if (element.HasChildNodes && element.ChildNodes.Count > 0) + { + string value = element.FirstChild.Value; + result = ((value != null) ? value : ""); + } + } + catch (Exception) + { + result = ""; + } + return result; + } + } + + internal class CsiRecordset : CsiXmlElement, ICsiRecordset, ICsiXmlElement + { + public CsiRecordset(ICsiDocument doc, XmlElement domElement) + : base(doc, domElement) + { + + } + + public CsiRecordset(ICsiDocument doc, ICsiXmlElement oParent) + : base(doc, "__recordSet", oParent) + { + } + + private XmlNode mCurrentElement; + + public Array GetFields() + { + Array result; + try + { + ArrayList arrayList = new ArrayList(); + bool flag = this.mCurrentElement != null; + if (flag) + { + XmlNodeList childNodes = this.mCurrentElement.ChildNodes; + for (int i = 0; i < childNodes.Count; i++) + { + XmlNode xmlNode = childNodes[i]; + bool flag2 = xmlNode.NodeType == XmlNodeType.Element; + if (flag2) + { + arrayList.Add(new CsiRecordsetField(this.GetOwnerDocument(), xmlNode as XmlElement)); + } + } + } + result = arrayList.ToArray(); + } + catch (Exception exp) + { + throw new CsiClientException(-1L, exp, base.GetType().FullName + ".GetFields()"); + } + return result; + } + + public virtual long GetRecordCount() + { + return CsiXmlHelper.GetChildCount(base.GetDomElement()); + } + + private XmlNode GoToNextElementNode(XmlNode element, bool bForward) + { + XmlNode result; + try + { + XmlNode xmlNode = element; + while (xmlNode != null && (xmlNode.NodeType != XmlNodeType.Element || !xmlNode.Name.Equals("__row"))) + { + if (bForward) + { + xmlNode = xmlNode.NextSibling; + } + else + { + xmlNode = xmlNode.PreviousSibling; + } + } + result = xmlNode; + } + catch (Exception exp) + { + throw new CsiClientException(-1L, exp, base.GetType().FullName + ".GoToNextElementNode()"); + } + return result; + } + + public void MoveFirst() + { + try + { + this.mCurrentElement = this.GoToNextElementNode(base.GetDomElement().FirstChild, true); + } + catch (Exception exp) + { + throw new CsiClientException(-1L, exp, base.GetType().FullName + ".MoveFirst()"); + } + } + + public void MoveLast() + { + try + { + this.mCurrentElement = this.GoToNextElementNode(base.GetDomElement().LastChild, false); + } + catch (Exception exp) + { + throw new CsiClientException(-1L, exp, base.GetType().FullName + ".MoveLast()"); + } + } + + public void MoveNext() + { + try + { + bool flag = this.mCurrentElement == null; + if (flag) + { + this.MoveFirst(); + } + else + { + XmlNode nextSibling = this.mCurrentElement.NextSibling; + bool flag2 = nextSibling == null; + if (flag2) + { + this.MoveFirst(); + } + else + { + this.mCurrentElement = this.GoToNextElementNode(nextSibling, true); + bool flag3 = this.mCurrentElement == null; + if (flag3) + { + this.MoveLast(); + } + } + } + } + catch (Exception ex) + { + throw new CsiClientException(-1L, ex.Message, base.GetType().FullName + ".MoveNext()"); + } + } + + public void MovePrevious() + { + try + { + bool flag = this.mCurrentElement == null; + if (flag) + { + this.MoveFirst(); + } + else + { + XmlNode previousSibling = this.mCurrentElement.PreviousSibling; + bool flag2 = previousSibling == null; + if (flag2) + { + this.MoveFirst(); + } + else + { + this.mCurrentElement = this.GoToNextElementNode(previousSibling, false); + bool flag3 = this.mCurrentElement == null; + if (flag3) + { + this.MoveFirst(); + } + } + } + } + catch (Exception exp) + { + throw new CsiClientException(-1L, exp, base.GetType().FullName + ".MovePrevious()"); + } + } + } +} diff --git a/Api/CsiSelectionValuesEx.cs b/Api/CsiSelectionValuesEx.cs index 20f0924..0453ebd 100644 --- a/Api/CsiSelectionValuesEx.cs +++ b/Api/CsiSelectionValuesEx.cs @@ -24,7 +24,7 @@ public virtual ICsiRecordset GetRecordset() { return this.FindChildByName("__recordSet") as ICsiRecordset; } - catch (Exception ex) + catch (Exception) { return (ICsiRecordset)null; } @@ -36,7 +36,7 @@ public virtual ICsiRecordsetHeader GetRecordsetHeader() { return this.FindChildByName("__recordSetHeader") as ICsiRecordsetHeader; } - catch (Exception ex) + catch (Exception) { return (ICsiRecordsetHeader)null; } @@ -46,14 +46,14 @@ public virtual long GetRecordCount() { long num = 0; var childByName = this.FindChildByName("__responseData") as CsiDataField; - if ( childByName!=null) + if (childByName != null) { - ICsiDataField childByNam= childByName .FindChildByName("__recordCount") as ICsiDataField; + ICsiDataField childByNam = childByName.FindChildByName("__recordCount") as ICsiDataField; try { num = long.Parse(childByNam.GetValue()); } - catch (Exception ex) + catch (Exception) { } } diff --git a/Api/CsiXmlHelper.cs b/Api/CsiXmlHelper.cs index d9f35e0..06553b7 100644 --- a/Api/CsiXmlHelper.cs +++ b/Api/CsiXmlHelper.cs @@ -72,7 +72,7 @@ public static ICsiXmlElement CreateCsiElement(ICsiDocument document, XmlElement { typeName = "Camstar.XMLClient.API.CsiXmlElement"; } - Type type = Type.GetType(typeName); + Type type = Type.GetType(typeName, true); Type type2 = typeof(ICsiDocument); Type type3 = typeof(XmlElement); element3 = (ICsiXmlElement)type.GetConstructor(new[] { type2, type3 }).Invoke(new object[] { document, element }); diff --git a/CamstarCommon.cs b/CamstarCommon.cs index c2c76a4..9b8b1e4 100644 --- a/CamstarCommon.cs +++ b/CamstarCommon.cs @@ -21,21 +21,23 @@ public class CamstarCommon private ICsiDocument _document; private ICsiService _service; private Guid _sessionId; + private InsiteLoginModel _lastLoginModel; + public Guid SessionId => _sessionId; public static IConfiguration Configuration { get; set; } - + /// /// 初始化 /// /// 工厂 - public CamstarCommon(string factory,IConfiguration configuration) + public CamstarCommon(string factory, IConfiguration configuration) { if (string.IsNullOrEmpty(factory)) { throw new Exception("工厂参数不能为空"); } - var model = GetConfigModel(factory,configuration); + var model = GetConfigModel(factory, configuration); if (model is InsiteLoginModel loginModel) { _session = null; @@ -46,13 +48,22 @@ public CamstarCommon(string factory,IConfiguration configuration) _sessionId = Guid.NewGuid(); _connection = _client.CreateConnection(loginModel.Host, loginModel.Port); _session = _connection.CreateSession(loginModel.User, loginModel.Password, _sessionId.ToString()); + _lastLoginModel = model; } - } - private InsiteLoginModel GetConfigModel(string factory ,IConfiguration configuration=null) + /// + /// 获取最近的登录模型信息 + /// + /// + public InsiteLoginModel GetLoginModel() { - if (configuration!=null) + return _lastLoginModel; + } + + private InsiteLoginModel GetConfigModel(string factory, IConfiguration configuration = null) + { + if (configuration != null) { Configuration = configuration; } @@ -62,15 +73,15 @@ private InsiteLoginModel GetConfigModel(string factory ,IConfiguration configura .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true }) .Build(); } - + var model = new InsiteLoginModel(); model.Host = Configuration["MESFactory:" + factory + ":Host"]; - model.Port =Convert.ToInt32( Configuration["MESFactory:" + factory + ":Port"]??"2281"); - model.User= Configuration["MESFactory:" + factory + ":User"]; - model.Password= Configuration["MESFactory:" + factory + ":Password"]; + model.Port = Convert.ToInt32(Configuration["MESFactory:" + factory + ":Port"] ?? "2281"); + model.User = Configuration["MESFactory:" + factory + ":User"]; + model.Password = Configuration["MESFactory:" + factory + ":Password"]; return model; } - + @@ -116,11 +127,16 @@ public CamstarCommon(string host, int port, string userName, string password) _sessionId = Guid.NewGuid(); _connection = _client.CreateConnection(host, port); _session = _connection.CreateSession(userName, password, _sessionId.ToString()); - } - + public CsiClient Client + { + get + { + return _client; + } + } #region Document操作 @@ -299,8 +315,8 @@ public bool Execute(Action action) this._document.ResponseData()?.GetOwnerDocument()?.AsXml()); ; } - - + + } /// @@ -404,9 +420,9 @@ public ICsiNamedObject Changes(string name) /// 版本 /// 是否使用默认版本 - public ICsiRevisionedObject Changes(string name,string rev,bool useRor) + public ICsiRevisionedObject Changes(string name, string rev, bool useRor) { - this.InputData().RevisionedObjectField("ObjectToChange").SetRef(name,rev,useRor); + this.InputData().RevisionedObjectField("ObjectToChange").SetRef(name, rev, useRor); this.Perform(PerformType.Load); return this.InputData().RevisionedObjectField("ObjectChanges"); } @@ -417,7 +433,7 @@ public ICsiRevisionedObject Changes(string name,string rev,bool useRor) /// 版本 /// 删除所有 - public (bool Status, string Message) Delete(string name, string rev,bool deleteAllRev=false) + public (bool Status, string Message) Delete(string name, string rev, bool deleteAllRev = false) { this.InputData().RevisionedObjectField("ObjectToChange").SetRef(name, rev, false); this.Perform(PerformType.Delete); @@ -431,7 +447,7 @@ public ICsiRevisionedObject Changes(string name,string rev,bool useRor) public ICsiNamedObject New(string name) { this.Perform(PerformType.New); - var objectChanges= this.InputData().NamedObjectField("ObjectChanges"); + var objectChanges = this.InputData().NamedObjectField("ObjectChanges"); objectChanges.DataField("Name").SetValue(name); return objectChanges; } diff --git a/InSiteXmlClient4Core.csproj b/InSiteXmlClient4Core.csproj index 7831208..f7799bc 100644 --- a/InSiteXmlClient4Core.csproj +++ b/InSiteXmlClient4Core.csproj @@ -4,12 +4,12 @@ netstandard2.0 李德镇 - Camstar MES系统API交互库,用于连接Camstar MES系统,使用在.net平台 + Camstar MES系统API交互库,用于连接Camstar MES系统,使用在.net standard 2.0平台 true - 1.0.5 + 1.0.6 7.1 - 1.0.5.0 - 1.0.5.0 + 1.0.0.0 + 1.0.6.0 diff --git a/InterFace/ICsiRecordset.cs b/InterFace/ICsiRecordset.cs index ed62161..00a6e77 100644 --- a/InterFace/ICsiRecordset.cs +++ b/InterFace/ICsiRecordset.cs @@ -34,4 +34,11 @@ public interface ICsiRecordset : ICsiXmlElement /// void MovePrevious(); } + + public interface ICsiRecordsetField : ICsiXmlElement + { + string GetName(); + + string GetValue(); + } } \ No newline at end of file diff --git a/README.md b/README.md index 487813a..ae8d38e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,12 @@ # InSiteXmlClient4Core + +https://github.com/307209239/InSiteXmlClient4Core.git +版本:1.0.5 +28b387609fdd37a408e8e3f4158f4cc351cdc417 + +1.0.6 +增加ICsiRecordset,ICsiRecordsetField接口的实现,调整获取类型时的异常报错! + 工作原因经常使用camstar的 InsiteXMLClient类库做二次开发,但是只能在4.X环境下使用,对于日益繁荣的.net core生态,花费了些时间把原有的类库重新封装为.net core 类库,并在实际环境中测试通过。 1.类库不再使用camstar的命名方式,接口统一以I开头 diff --git a/Utility/ServerConnection.cs b/Utility/ServerConnection.cs index 87c0f20..ebb2e7f 100644 --- a/Utility/ServerConnection.cs +++ b/Utility/ServerConnection.cs @@ -170,7 +170,7 @@ public virtual bool Connect(string hostName, int portNo) { mTcpClient = null; throw new Exception($"连接服务器:{Host} 端口{Port} 失败,{ex.Message}"); - + } return flag; @@ -203,7 +203,7 @@ public virtual bool Send() } catch (Exception ex) { - + throw new Exception($"发送失败:{ex.Message}"); } @@ -235,7 +235,7 @@ public virtual bool Receive(out string outputXml) } catch (Exception ex) { - + throw new Exception($"接收失败:{ex.Message}"); } @@ -272,8 +272,7 @@ public virtual string Submit(string host, int port, string inboundXML, string in return Submit(host, port, inboundXML, inboundXMLFilePath, logXML, null); } - public virtual string Submit(string host, int port, string inboundXML, string inboundXMLFilePath, bool logXML, - string xmlLogPath) + public virtual string Submit(string host, int port, string inboundXML, string inboundXMLFilePath, bool logXML, string xmlLogPath) { if (!StringUtil.IsEmptyString(host)) mHost = host; @@ -345,7 +344,6 @@ public virtual string Submit() { Disconnect(); } - return outputXML; } } @@ -435,8 +433,8 @@ protected virtual string MaskPassword(string document) protected virtual string EncryptPassword(string document) { var str1 = document; - var xmlWriter = (XmlWriter) null; - var reader = (XmlReader) null; + var xmlWriter = (XmlWriter)null; + var reader = (XmlReader)null; var flag1 = false; try { @@ -474,7 +472,7 @@ protected virtual string EncryptPassword(string document) { if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "__InSite") { - var str2 = (string) null; + var str2 = (string)null; xmlWriter.WriteStartElement(reader.LocalName); for (var flag2 = reader.MoveToFirstAttribute(); flag2; flag2 = reader.MoveToNextAttribute()) if (reader.LocalName == "__encryption") @@ -511,7 +509,7 @@ protected virtual string EncryptPassword(string document) if (reader.LocalName != "__encrypted") xmlWriter.WriteAttributeString(reader.LocalName, reader.Value); xmlWriter.WriteAttributeString("__encrypted", "no"); - var content = (int) reader.MoveToContent(); + var content = (int)reader.MoveToContent(); var fieldData = reader.ReadElementContentAsString(); // xmlWriter.WriteValue(CryptUtil.Encrypt(fieldData)); xmlWriter.WriteValue(fieldData); @@ -553,10 +551,12 @@ protected virtual void LogDocument(string operation, string document) .Replace("encoding=\"UTF-16LE\"", ""); if (document1[document1.Length - 1] == 0) document1 = document1.Remove(document1.Length - 1, 1); - var streamWriter = new StreamWriter(Path.Combine(Path.GetTempPath(), "Insite.log"), false, - Encoding.Default); - streamWriter.WriteLine(MaskPassword(document1)); - streamWriter.Close(); + + using (var streamWriter = new StreamWriter(Path.Combine(Path.GetTempPath(), "Insite.log"), true, Encoding.Default)) + { + streamWriter.WriteLine(MaskPassword(document1)); + streamWriter.Close(); + } } catch (Exception ex) {