C# .net 本地程序集加载失败,CAS 策略

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15238714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:35:04  来源:igfitidea点击:

.net local assembly load failed with CAS policy

c#.net

提问by Maanu

We are getting the following assembly load error. The assembly is loaded from the local path "C:\Program Files\ASWorx Products\ASWorx\Bin\". The problem is not there with the old version of the binary. The issue appears when we have sent the new binary through e-mail. Build settings are not changed. How can we correct the issue? The issue appears in Win7 32 bit machine

我们收到以下程序集加载错误。程序集从本地路径“C:\Program Files\ASWorx Products\ASWorx\Bin\”加载。旧版本的二进制文件不存在问题。当我们通过电子邮件发送新的二进制文件时,问题就会出现。构建设置不会改变。我们如何纠正这个问题?Win7 32位机出现此问题

File name: 'file:///C:\Program Files\ASWorx Products\ASWorx\Bin\ASConnexDI.dll' ---> System.NotSupportedException: 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.

   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)

   at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)

   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks)

   at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)

   at System.Reflection.Assembly.LoadFrom(String assemblyFile, Evidence securityEvidence)

   at System.Activator.CreateInstanceFromInternal(String assemblyFile, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo)

   at NeST.ICE.IOSystem.DIManager.InitializeDI()

采纳答案by Blorgbeard is out

From the link in the error message:

错误消息中链接

If an application has been copied from the web, it is flagged by Windows as being a web application, even if it resides on the local computer. You can change that designation by changing the file properties, or you can use the element to grant the assembly full trust. As an alternative, you can use the UnsafeLoadFrom method to load a local assembly that the operating system has flagged as having been loaded from the web.

如果应用程序是从 Web 复制的,则 Windows 会将其标记为 Web 应用程序,即使它驻留在本地计算机上。您可以通过更改文件属性来更改该指定,也可以使用该元素授予程序集完全信任。作为替代方法,您可以使用 UnsafeLoadFrom 方法加载操作系统已标记为已从 Web 加载的本地程序集。

Try opening the file properties and clicking 'Unblock':

尝试打开文件属性并单击“取消阻止”:

properties window

属性窗口

回答by Dimi Takis

Additionally to the above given answer here my solution for the problem.

除了上面给出的答案之外,我对这个问题的解决方案。

Trying to do the unblocking resulted always that the file still appeared to be blocked.

尝试解除阻塞总是导致文件似乎仍然被阻塞。

So, I made a new folder, copied over the affected files and performed the unblocking file by file. Surprisingly the flag got removed compared to doing the same action in the original file location. So, the final step was copying the now unblocked files back to the original location. Done!

因此,我创建了一个新文件夹,复制了受影响的文件并逐个文件执行了解锁文件。令人惊讶的是,与在原始文件位置执行相同操作相比,该标志被删除了。因此,最后一步是将现在未阻止的文件复制回原始位置。完毕!

回答by Nitin Singh Thakur

I am able to resolve this problem using below link and adding the settings to my config.

我可以使用以下链接解决此问题并将设置添加到我的配置中。

https://msdn.microsoft.com/en-us/library/dd409252(VS.100).aspx

https://msdn.microsoft.com/en-us/library/dd409252(VS.100).aspx

<configuration>
   <runtime>
      <loadFromRemoteSources enabled="true"/>
   </runtime>
</configuration>

回答by Pellet

I had to use the method

我不得不使用该方法

Assembly.UnsafeLoadFrom()

instead of Assembly.LoadFrom, It fixed my issues... Found it at this link:

而不是 Assembly.LoadFrom,它解决了我的问题......在此链接中找到它:

https://msdn.microsoft.com/en-us/library/dd409252(VS.100).aspx

https://msdn.microsoft.com/en-us/library/dd409252(VS.100).aspx