经验分享:U9 BP更新免重启IIS实现方法

2022/12/14

这篇文章发布于 725 天前,部分信息可能已经发生变化。

项目背景

OrBit MES对接U9:开发MES->U9接口

痛处

  • BP更新IIS需要重启💀
  • 如果是生产环境还要等夜里重启IIS💀

更新BP免重启思路

  • 使用反射

反射主要代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace UFIDA.U9.Cust.SHBC.OrBitMESToU9.HotLoad
{
    public static class HotLoadDLL
    {
        public static object run(string jsonStr,string action)
        {
            object retrunobj=null;
            string DllName = "UFIDA.U9.SzCusDev.JAR.InterfaceSVSHBCHot.dll";
            string PathAndFile = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"ApplicationServer\Libs\" + DllName;

            if (!File.Exists(PathAndFile))
            {
                new Exception("糟糕!反射文件:" + PathAndFile + "文件没找到.");
            }

            Assembly asm = Assembly.Load(File.ReadAllBytes(PathAndFile));

            foreach (Type type in asm.GetTypes().Where(t => t.IsClass && t.Namespace.StartsWith("UFIDA.U9.SzCusDev.JAR.InterfaceSV.Syncers")).ToList())
            {
                // Do stuff here
                object o = Activator.CreateInstance(type, null);
                PropertyInfo propertyInfo = type.GetProperty("Action");
                if (propertyInfo == null) continue;

                string actionName = (string)propertyInfo.GetValue(o, null);
                PropertyInfo propertyInfo2 = type.GetProperty("ProjectName");
                string projectName = (string)propertyInfo2.GetValue(o, null);
                PropertyInfo propertyInfo3 = type.GetProperty("InModelName");
                string inModelName = (string)propertyInfo3.GetValue(o, null);
                PropertyInfo propertyInfo4 = type.GetProperty("OutModelName");

                if (actionName == action)
                {
                    MethodInfo method = type.GetMethod("Do", new Type[] { typeof(string) });
                   
                    try
                    {
                        object result = method.Invoke(o, new object[] { jsonStr });
                        if (result != null)
                        {
                            retrunobj = JsonConvert.SerializeObject(result);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex.InnerException != null)
                            retrunobj = JsonConvert.SerializeObject(new OutModel
                            {
                                isSuccess = false,
                                message = ex.InnerException.Message
                            });
                    }

                    break;
                }
            }

            if (retrunobj == null)
            {
                throw new Exception(string.Format("Action解析失败,Action类型:{0},请确认传入的Action是否正确", action));
            }

            return retrunobj;
        }

    }
}

调用方法:

public override object Do(object obj)
    {
      SyncService syncService = (SyncService) obj;
      try
      {
        LoggerManager.GetLogger(((object) this).GetType()).Error("OrBitMES->U9 JSON Begin:" + syncService.InStr + " OrBitMES->U9 JSON END;");
        InModel inModel = JsonConvert.DeserializeObject<InModel>(syncService.InStr);
        string action = inModel.action;
        string key = inModel.action;
        if (!string.IsNullOrEmpty(inModel.projectName))
          key = key + "-" + inModel.projectName;

             var t1 = UFIDA.U9.Cust.SHBC.OrBitMESToU9.HotLoad.HotLoadDLL.run(syncService.InStr, action);
            if (t1 == null && !string.IsNullOrEmpty(inModel.projectName))
                {
                    t1 = UFIDA.U9.Cust.SHBC.OrBitMESToU9.HotLoad.HotLoadDLL.run(syncService.InStr, key);
                }

                if (t1 == null)
                {
                    throw new Exception(string.Format("Action解析失败,Action类型:{0},请确认传入的Action是否正确", (object)inModel.action));
                }

                //  return (object) JsonConvert.SerializeObject(((SyncerFactory.GetSyncer(key) ?? SyncerFactory.GetSyncer(action)) ?? throw new Exception(string.Format("Action解析失败,Action类型:{0},请确认传入的Action是否正确", (object) inModel.action))).Do(syncService.InStr));

                return (object)JsonConvert.SerializeObject(t1);


      }
      catch (Exception ex)
      {
        return (object) JsonConvert.SerializeObject((object) new OutModel()
        {
          isSuccess = false,
          message = ex.Message
        });
      }
    }

测试

修改业务逻辑,更新BP DLL到U9,成功覆盖BP文件,调用成功!

image-20221214085350457