ASP.NET Core 3.1跨服务器运行报错:找不到System.Drawing.Common程序集
我们有一套ASP.NET Core 3.1代码文件,在一台服务器上运行完全正常,但部署到另一台服务器的某个服务中时,出现以下错误:
Error Source: KellermanSoftware.Compare-Net-Objects
Error Target Site: Boolean IsFont(System.Type)
Error Message: Could not load file or assembly 'System.Drawing.Common, Version=4.0.2.3, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
Error StackTrace: at KellermanSoftware.CompareNetObjects.TypeHelper.IsFont(Type type)
at KellermanSoftware.CompareNetObjects.TypeComparers.FontComparer.IsTypeMatch(Type type1, Type type2)
at KellermanSoftware.CompareNetObjects.RootComparer.<>c_DisplayClass3.<Compare>b_1(BaseTypeComparer o)
at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable1 source, Func2 predicate, Boolean& found)
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source, Func2 predicate)
at KellermanSoftware.CompareNetObjects.RootComparer.Compare(CompareParms parms)
at KellermanSoftware.CompareNetObjects.CompareLogic.Compare(Object object1, Object object2)
at Services.Security.Auditing.AuditLogService.CreateAuditLogBatch(Guid userId, AuditActionType action, List1 keyFieldIds, List1 oldObjects, List`1 newObjects) in D:\FON2019\Version\Security\Services.Security\Auditing\AuditLogService.cs:line 65
- 依赖缺失:出错服务器未安装
System.Drawing.Common程序集,或其版本与KellermanSoftware.Compare-Net-Objects依赖的4.0.2.3版本不匹配。 - 运行时环境差异:两台服务器的.NET Core运行时配置不同,正常运行的服务器可能安装了包含该程序集的Windows Desktop Runtime,而出错服务器仅安装了ASP.NET Core Runtime(默认不包含
System.Drawing.Common)。 - 部署包不完整:发布到出错服务器的文件中未包含
System.Drawing.Common的dll,可能是发布过程中未正确打包依赖项。
- 安装对应运行时:在出错服务器上安装.NET Core 3.1 Desktop Runtime,该运行时默认包含
System.Drawing.Common组件。 - 添加NuGet依赖并重新发布:在项目中通过NuGet安装
System.Drawing.Common包(优先选择4.0.2.3版本,或兼容版本),重新发布项目后部署,确保发布包中包含该dll文件。 - 配置程序集绑定重定向:若服务器已存在该程序集但版本不符,可在
web.config中添加绑定重定向,将旧版本映射到所需的4.0.2.3版本:<configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.Drawing.Common" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.0.2.3" newVersion="4.0.2.3" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration> - 禁用不必要的类型比较:如果业务逻辑不需要比较
Font类型对象,可修改Compare-Net-Objects的配置,忽略该类型的比较,避免触发对System.Drawing.Common的加载:var compareLogic = new CompareLogic(); compareLogic.Config.IgnoreTypes.Add(typeof(System.Drawing.Font));
内容的提问来源于stack exchange,提问作者hamid

