Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

"System.MethodAccessException: Attempt to access method" in ASP.NET 4.0


Symptoms

When an ASP.NET 4.0 web application that uses the AJAX ScriptManager receives its first request, the following exception may occur:

System.MethodAccessException: Attempt to access method System.Management.Instrumentation.InstrumentedAttribute..ctor(System.String) in violation of security transparency rules failed

This occurs when the ScriptManager calls AssemblyCache.GetAjaxFrameworkAssemblyAttribute and encounters an assembly that is already loaded and which has the InstrumentedAttribute. In the above example the assembly with this attribute was System.Management.dll.

 

↑ Back to the top


Cause

This problem occurs because when the System.Web.UI.ScriptManager class is first instantiated it builds a cache of method information for classes in assemblies already loaded in the application. When it tries to examine the constructor of the InstrumentedAttribute the exception is thrown and not handled.

 

↑ Back to the top


Resolution

To workaround this issue reliably, instantiate an instance of ScriptManager at a point during application start up prior to the first request actually being executed but after the assembly that has the InstrumentedAttribute has been loaded.

For example, in global.asax add some new code to Application_Start:

private void Application_Start(HttpApplication application)
{

if (applicationStartupComplete) return;
try
{
object osm = new System.Web.UI.ScriptManager();
}
catch(Exception)
{}

}

 

NOTE: The above code is provided AS-IS and is for sample purposes only.

 

↑ Back to the top


More Information

Microsoft has confirmed that this is a problem with the products listed in the applies-to section of this article.  The issue is scheduled to be addressed in a future version of ASP.NET.

 

↑ Back to the top


Keywords: vkball, kb

↑ Back to the top

Article Info
Article ID : 2635463
Revision : 1
Created on : 1/8/2017
Published on : 11/4/2011
Exists online : False
Views : 643