2
votes

I have some problems with my MVC c# project.

First I receive this error: Level 2 transparency causes all methods in AllowPartiallyTrustedCallers assemblies to become security transparent by default, which may be the cause of this exception. complete:

Attempt by security transparent method 'System.Runtime.Diagnostics.DiagnosticTrace..ctor(System.String, System.Guid)' to access security critical method 'System.Runtime.Diagnostics.EtwDiagnosticTrace..ctor(System.String, System.Guid)' failed.

Assembly 'System.ServiceModel.Internals, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is marked with the AllowPartiallyTrustedCallersAttribute, and uses the level 2 security transparency model. Level 2 transparency causes all methods in AllowPartiallyTrustedCallers assemblies to become security transparent by default, which may be the cause of this exception."

Consequently I searched on the internet and two possibilities to solve it are found. The first works by including this snippet in assemblyinfo.cs:

[assembly: System.Security.AllowPartiallyTrustedCallers]

but nothing changed..

Googling I found a second information saying that If I place in web.config it probably work.

<trust legacyCasModel="true" level="Full" />

But when I placed that info in web.config I receive and error like

Dynamic operations can only be performed in homogenous AppDomain

I don't know what else do I have to do...

I believe that using microsoft.practices.unity with ASP.NET 4.0 and mvc 4, is the main cause of the problem. But using unity for me is necessary based on the way my app works.

brgds! sebastian.

1
@DarinDimitrov do you know how to solve this issue?, thank you so much in advance.. - s_h
@marc-s thank you for the ediition... do you have any idea of why this could be possible?. brgds - s_h

1 Answers

5
votes

In .NET 4.0 framework, new CAS model prevents any security transparent code to call into security critical code

From the exception that you've described above, it seems that both the methods that you call belong to a an external library (namespace System.Runtime.Diagnostics), and not to your code or even your assembly.

The fact that one external library method throws when trying to access another method of that library, indicates the possibility of a bug in that library or some sort of a binary or configuration mismatch issue (according to this thread http://social.msdn.microsoft.com/Forums/en-US/netfxsetup/thread/a8ceb77b-f625-4259-94de-4ae5157ba98c)

"legacyCasModel" switches to non-homogeneous (or simply CAS 1) app domains. This means that dynamic and the older CAS setting is incompatible. Moreover, MVC 3 used dynamic in its own code (please see ViewBag dynamic in ASP.NET MVC 3), so most likely, MVC 4 does the same.

Instead of switching to legacy cas 1, you could try adding the assembly with System.Runtime.Diagnostics namespace to FullTrustAssembliesSection of your web.config file

http://msdn.microsoft.com/en-us/library/system.web.configuration.fulltrustassembliessection.aspx