コード例 #1
0
ファイル: MediInfoHis.svc.cs プロジェクト: radtek/WCF
        public int RunService(string TradeType, string TradeMsg, ref string TradeMsgOut)
        {
            int    i    = 0;
            string guid = Guid.NewGuid().ToString("N").ToUpper();

            //log.Info("[" + guid + "]");
            log.InfoFormat("[{2}][{0}].IN  {1}", TradeType, LogUnity.I.ShowXml(TradeMsg), guid);
            //string outxml = "";
            IBaseMessage objtype = null;

            try
            {
                objtype = (IBaseMessage)ToolUnity.LoadAssembly(TradeType);
            }
            catch (Exception ex)
            {
                //outxml = ToolUnity.ServiceERR(TradeType, ex);
                TradeMsgOut = ToolUnity.ServiceERR(TradeType, ex);
                i           = -1;
                goto last;
            }
            #region 验证数字签名
            //为适应老版本,这个参数设置成0,以后新上线的 都必须设置成1
            string Signature = ConfigurationManager.AppSettings["Signature"];
            if (Signature == "1")
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(TradeMsg);
                string key  = "";
                string mac  = "";
                string sign = "";
                if (doc.DocumentElement != null)
                {
                    string head = doc.DocumentElement.Name;

                    XmlNode nodemac = doc.SelectSingleNode(head + "/BASEINFO/MACDZ");
                    if (nodemac == null)
                    {
                        TradeMsgOut = ToolUnity.ServiceERR(TradeType, "MACDZ未分配!");
                        return(-1);
                    }
                    mac = nodemac.InnerText;
                    XmlNode nodekey = doc.SelectSingleNode(head + "/BASEINFO/KEY");
                    if (nodekey == null)
                    {
                        TradeMsgOut = ToolUnity.ServiceERR(TradeType, "校验码未分配!");
                        return(-1);
                    }
                    key = nodekey.InnerText;
                    XmlNode nodesign = doc.SelectSingleNode(head + "/BASEINFO/SIGN");
                    if (nodesign == null)
                    {
                        TradeMsgOut = ToolUnity.ServiceERR(TradeType, "数字验证未分配!");
                        return(-1);
                    }
                    sign = nodesign.InnerText;
                    XmlNode BASEINFO = doc.SelectSingleNode(head + "/BASEINFO");
                    BASEINFO.RemoveChild(nodesign);
                }
                string inxml = doc.InnerXml;

                DataTable dt = DBVisitor.ExecuteTable($"select * from GY_REGISTERED where mac='{mac}'");
                if (dt.Rows.Count <= 0)
                {
                    TradeMsgOut = ToolUnity.ServiceERR(TradeType, "未注册!");
                    i           = -1;
                    return(i);
                }
                if (dt.Rows[0]["KEY"].ToString() != key)
                {
                    TradeMsgOut = ToolUnity.ServiceERR(TradeType, "校验码验证不通过!");
                    i           = -1;
                    return(i);
                }
                string mysign = AlipayMD5.Sign(inxml, dt.Rows[0]["KEY"].ToString(), "utf-8");
                if (mysign != sign)
                {
                    TradeMsgOut = ToolUnity.ServiceERR(TradeType, "签名验证错误!");
                    i           = -1;
                    return(i);
                }
            }
            #endregion
            try
            {
                objtype.MessageID = guid;
                objtype.ParseInXml(TradeMsg);
                objtype.ProcessMessage();
                //outxml = objtype.ParseOutXml();
                TradeMsgOut = objtype.ParseOutXml();
                //LogUnity.I.Info(0, TradeType, TradeMsg, outxml, "", guid);
            }
            catch (Exception ex)
            {
                //outxml = objtype.ParseOutXml(ex, ConfigurationManager.AppSettings["enabledymc"] == "0");
                TradeMsgOut = objtype.ParseOutXml(ex, ConfigurationManager.AppSettings["enabledymc"] == "0");
                //LogUnity.I.Insert(-1, TradeType, TradeMsg, outxml, ex.StackTrace.Trim(), guid);
                //LogUnity.I.Insert(-1, TradeType, TradeMsg, TradeMsgOut, ex.StackTrace.Trim(), guid);
                i = -1;
            }
last:
            {
                //log.InfoFormat("[{0}].OUT {1}", TradeType, outxml + "\r\n");
                log.InfoFormat("[{2}][{0}].OUT {1}", TradeType, TradeMsgOut, guid);
                return(i);
            }
        }