.net local assembly load failed with CAS policy

C#.Net

C# Problem Overview


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

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()

C# Solutions


Solution 1 - C#

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.

Try opening the file properties and clicking 'Unblock':

properties window

Solution 2 - C#

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

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

Solution 3 - C#

I had to use the method

Assembly.UnsafeLoadFrom()

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

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

Solution 4 - C#

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaanuView Question on Stackoverflow
Solution 1 - C#BlorgbeardView Answer on Stackoverflow
Solution 2 - C#Nitin Singh ThakurView Answer on Stackoverflow
Solution 3 - C#PelletView Answer on Stackoverflow
Solution 4 - C#Dimi TakisView Answer on Stackoverflow