本文介绍了大会装载远程计算机上使用Assembly.LoadFrom()会导致SecurityException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我加载使用 Assembly.LoadFrom(文件名)组装。当文件名是本地机器上,一切工作正常。然而,当相同的文件(依赖)是在远程网络共享,我收到了 System.Security.SecurityException 那一刻我尝试创建一个新的的SqlConnection 从远程组件: What's the cure? 解决方案 You could load the assembly as bytes and load it with Assembly.Load(bytes), maybe this works.Or you give the application the requested permission.Edit:I made a little test and it worked for me. Here is some code:static Dictionary<Assembly, String> _Paths = new Dictionary<Assembly, String>();static void Main(string[] args){ AppDomain current = AppDomain.CurrentDomain; current.AssemblyResolve += new ResolveEventHandler(HandleAssemblyResolve); // This line loads a assembly and retrieves all types of it. Only when // calling "GetTypes" the 'AssemblyResolve'-event occurs and loads the dependency Type[] types = LoadAssembly("Assemblies\\MyDLL.dll").GetTypes(); // The next line is used to test permissions, i tested the IO-Permissions // and the Reflection permissions ( which should be denied when using remote assemblies ) // Also this test includes the creation of a Form Object instance = Activator.CreateInstance(types[0]);}private static Assembly LoadAssembly(string file){ // Load the assembly Assembly result = Assembly.Load(File.ReadAllBytes(file)); // Add the path of the assembly to the dictionary _Paths.Add(result, Path.GetDirectoryName(file)); return result;}static Assembly HandleAssemblyResolve(object sender, ResolveEventArgs args){ // Extract file name from the full-quallified name String name = args.Name; name = name.Substring(0, name.IndexOf(',')); // Load the assembly return LoadAssembly(Path.Combine(_Paths[args.RequestingAssembly], name + ".dll"));}Something important:There may be files which do not have a matching name and file name, but you can resolve this by checking all files in folder with AssemblyName.GetAssemblyName(file). 这篇关于大会装载远程计算机上使用Assembly.LoadFrom()会导致SecurityException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-19 16:41