0x00 编译问题
从github下载的c#项目,使用Visual Studio 2022编译时 Fody 使用 FodyIsolated.dll 抛出错误
严重性 代码 说明 项目 文件 行 禁止显示状态 详细信息
错误 Fody: An unhandled exception occurred:
Exception:
Could not load file or assembly 'file:///F:\mybox\IntranetTools\Sqlinfo-main_数据库信息探测\Sqlinfo-main\Sqlinfo-main\packages\Fody.6.5.5\netclassictask\FodyIsolated.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)
Type:
System.IO.FileLoadException
StackTrace:
在 System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
在 System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
在 System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
在 System.Reflection.Assembly.LoadFrom(String assemblyFile, Evidence securityEvidence)
在 System.Activator.CreateInstanceFromInternal(String assemblyFile, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo)
在 System.AppDomain.CreateInstanceFrom(String assemblyFile, String typeName)
在 System.AppDomain.CreateInstanceFromAndUnwrap(String assemblyName, String typeName)
在 System.AppDomain.CreateInstanceFromAndUnwrap(String assemblyName, String typeName)
在 IsolatedAssemblyLoadContext.CreateInstanceFromAndUnwrap() 位置 C:\projects\fody\FodyCommon\IsolatedAssemblyLoadContext.cs:行号 21
在 Processor.ExecuteInOwnAssemblyLoadContext() 位置 C:\projects\fody\Fody\Processor.cs:行号 129
在 Processor.Inner() 位置 C:\projects\fody\Fody\Processor.cs:行号 123
在 Processor.Execute() 位置 C:\projects\fody\Fody\Processor.cs:行号 51
Source:
mscorlib
TargetSite:
System.Reflection.RuntimeAssembly _nLoad(System.Reflection.AssemblyName, System.String, System.Security.Policy.Evidence, System.Reflection.RuntimeAssembly, System.Threading.StackCrawlMark ByRef, IntPtr, Boolean, Boolean, Boolean)
An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more information.
Type:
System.NotSupportedException
StackTrace: Sqlinfo
根据报错,看起来是应用程序(Sqlinfo)在尝试加载位于网络位置的程序集(FodyIsolated.dll),而.NET Framework不允许默认情况下从网络位置加载程序集,以防止潜在的安全风险。
在项目中添加 App.config 文件,如果没有的话,创建一个。然后在 App.config 文件中添加以下内容:
<configuration>
<runtime>
<loadFromRemoteSources enabled="true"/>
</runtime>
</configuration>
在App.config中添加以上内容,重新编译,依旧报错。
0x01 解决方法
在经过一番搜寻之后,找到解决办法,因为项目中fody程序集在本地就有,并不需要从网络中加载,没有加载成功,是因为windows的安全机制,将其锁定了,我们只需要将用到的dll文件解除锁定,即可
再次编译
c# - Costura Fody throwing error with FodyIsolated.dll - Stack Overflow
More Implicit Uses of CAS Policy: loadFromRemoteSources | Microsoft Learn