19

Hello and thanks in advance for the help, I know that this question or similar questions have been posted, frequently with specific regards to MVC 3 applications. However, I am getting this error message whenever I try and launch any application from within visual web developer express 2010 using a .net 4.0 target framework.

The exact error message is:

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.

Stack trace:

[MethodAccessException: 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.]
   System.Runtime.Diagnostics.DiagnosticTrace..ctor(String traceSourceName, Guid etwProviderId) +24
   System.ServiceModel.Activation.FxTrace.InitializeTracing() +90
   System.ServiceModel.Activation.FxTrace.get_Trace() +84
   System.ServiceModel.ServiceHostingEnvironment.EnsureInitialized() +59
   System.ServiceModel.ServiceHostingEnvironment.OnEnsureInitialized(Object state) +4
   System.ServiceModel.AspNetPartialTrustHelpers.PartialTrustInvoke(ContextCallback callback, Object state) +52
   System.ServiceModel.ServiceHostingEnvironment.SafeEnsureInitialized() +65
   System.ServiceModel.Activation.HttpModule.ProcessRequest(Object sender, EventArgs e) +58
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +143
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69

I am completely at a loss here. If I create just any basic application from scratch and try and launch it, even with nothing added, I get this error screen. I have tried removing the target framework="4.0" attribute from the

<compilation debug="true" targetFramework="4.0" />

tag and running it with no results. I have tried removing that attribute and adding

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v2.0.50727"/>
  <supportedRuntime version="v4.0.30319"/>
 </startup>

as suggested in this thread: BadImageFormatException - Is it possible to specify which .NET version is used to run an executable? I tried repairing the .Net 4.0 framework installation because I heard that may be an issue. I just installed visual web developer last night in order to work on a project that I moved over from Microsoft Visual Studio 2010 because my trial version expired, any help or potential workarounds would be appreciated.

Community
  • 1
  • 1
Christian
  • 1,685
  • 9
  • 28
  • 48

4 Answers4

18

I experienced this issue today. As far as I understand it, the problem is that .NET 4.0 assemblies now default to security critical. It's not possible for security transparent code to call into security critical code.

The solution in my case was related to having AllowPartiallyTrustedCallersAttribute defined on only a few of the assemblies in my application. I had to either remove this attribute from all assemblies, or add it to all of them.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • I've read something similar to this in other places, although I was not able to successfully implement your fix, I believe it is the correct response. – Christian Nov 27 '12 at 16:24
  • 1
    Removing the AllowPartiallyTrustedCallersAttribute from AssemblyInfo.cs worked for my solution. I was getting the error from an older assembly that wasn't using the latest .Net framework versions. – Apogee Jul 13 '15 at 10:01
  • I ran into this after updating a large project to .Net 4. It was an ORM library (Entity Spaces) that was decorated with the AllowPartiallyTrustedCallersAttribute. Commented it out and everything was fixed. Thanks! – scubasteve Feb 25 '16 at 03:47
  • This feature is useful when you want to write a Plugin framework (for example), and you wish to prevent the plugin from making direct calls to methods. Unfortunately, the default is too restrictive making it a real PITA to implement (adding lots of extra security level stuff to your classes / methods). Would be nice if the object was compiled in, that the parent assembly itself would have access to all methods, and to selectively choose methods that are not allowed to directly interact with those inside your main binary. (accessibly to the binary itself) – Kraang Prime Aug 14 '16 at 19:15
  • I have a similar issue today. After fixing the problem as described, and clean / rebuild the solution the problem remained. The reason was "trivial" (of course, after I found it): the assembly was GAC-ed, and Visual Studio (when debugging) and the application without debugging loaded the GAC-es version, which had the wrong attributes. So if you have the same sympthoms, check the assembly in GAC, and re-deploy the new version, if you found a former version. – pholpar Nov 07 '17 at 13:13
3

Appreciate I'm extremely late to the party here but in VS2013 and suffering the same issue with a .NET 4.5.1 ClickOnce project, I fixed this issue by:

  1. Opening the Project Properties of the ClickOnce project in question
  2. Opening the Security tab
  3. Changing the trust settings to "This is a partial trust application"
  4. Changing the trust settings back to "This is a full trust application"

Hope this helps someone else!

Rob
  • 981
  • 12
  • 27
2

You may need to add the following to your AssemblyInfo.cs file so that the involved assemblies are the same.

[assembly: SecurityTransparent()]
Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
  • This just worked for me. I have a C# class library project (.Net 4.0) and a Windows Forms VB project. I added "[assembly: SecurityTransparent()]" to the AssemblyInfo.cs in the C# library project, and the issue vanished. – Crispy Ninja Jan 05 '15 at 22:38
  • Worked for me too. Tnks. – Luiz Jun 25 '15 at 14:54
0

Apparently, one potential workaround which at least made it possible for me to launch applications in Visual Web Developer was this:

  • Remove the targetFramework="4.0" attribute from the element which is located within the element.

  • If it is a Silverlight application Right click the ApplicationName.Web header select properties, then change the target framework to 3.5. If it is an asp.net application right click the top project header and select properties and then set the target framework to 3.5.

All this suggests to me their is a problem with my .Net 4.0 framework installation. But as I have installed and uninstalled and then installed the .Net 4.0 framework at least twice, this is the only option I can think of at this point. If someone provides me a better response/More permanent solution that works for me I'll mark that as the answer. In the meantime I will try launching the "install other frameworks" and reinstalling both the .Net 4.0 targeting pack and .Net 4.0 Runtime once more to see if it works.

Christian
  • 1,685
  • 9
  • 28
  • 48