Microsoft KB Archive/839990

= EIF events that are raised from an ASP.NET page do not succeed when the impersonation feature is turned on =

Article ID: 839990

Article Last Modified on 3/20/2007

-

APPLIES TO


 * Microsoft Enterprise Instrumentation Framework

-



SYMPTOMS
When you raise Microsoft Enterprise Instrumentation Framework (EIF) events to a trace session from a Microsoft ASP.NET page, the following exception may be logged in the application event log: Error loading an Event Sink of type Microsoft.EnterpriseInstrumentation.EventSinks.TraceEventSink'. The Event Source of name 'Application' will not write events out to this Event Sink. The following exception was returned during the load: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.

--->System.ComponentModel.Win32Exception: Unable to register with the Windows Trace Event Session TraceSession. Tracing is disabled to this session. When calling the RegisterTraceGuids API an error code of 5 and message &quot;Access is denied&quot; was returned.

at Microsoft.EnterpriseInstrumentation.EventSinks.TraceEventSink..ctor(IDictionary parameters, EventSource eventSource)

--- End of inner exception stack trace ---

at System.Reflection.RuntimeConstructorInfo.InternalInvoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean isBinderDefault)

at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)

at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)

at System.Reflection.Assembly.CreateInstance(String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)

at Microsoft.EnterpriseInstrumentation.EventSinks.EventSink.CreateNewEventSinks(DataRow[] eventSinkRows, EventSource eventSource)



CAUSE
The RegisterTraceGuids API is used to register the event trace provider and the trace classes that are required to raise the events to the trace sessions. When the TraceEventSink event sink is created, a call to the RegisterTraceGuids API does not succeed, and you receive an &quot;Access Denied&quot; error message. The call does not succeed because the ASPX page is impersonating a thread identity that does not have sufficient permissions to access a process handle for registration.



RESOLUTION
To resolve this problem, use one of the following methods:
 * Raise the first EIF event in the context of the process identity. You must raise the first EIF event on the .aspx page that raises the first event. To do this:
 * Revert to the process identity.
 * Raise the EIF event.
 * Restore the thread identity.
 * Implement the revert and restore process in the Application_Start method in the Global.Asax page.

You only have to perform this step one time during the application lifetime.

Implement the revert and restore process
The following sample code for Microsoft Visual C# .NET and Microsoft Visual Basic .NET demonstrates how to perform the revert and restore process. This process raises the first EIF event in the ASP.NET application.

Visual C# .NET
using System.Security.Principal;

WindowsImpersonationContext context = null; try {    // Revert to process identity. context = WindowsIdentity.Impersonate(IntPtr.Zero); // Raise the first EIF event of the application. TraceMessageEvent.Raise(&quot;Trace Message Event from Webform1.aspx&quot;); }

catch (Exception ex) {     Response.Write(&quot;Exception Thrown. !Error Message: {0}&quot;, ex.Message.ToString); } finally {   // Restore the thread's identity. if (context != null) context.Undo; }

Visual Basic .NET
Imports System.Security.Principal

Dim context as WindowsImpersonationContext

Try 'Revert to process identity. context = WindowsIdentity.Impersonate(IntPtr.Zero) 'Raise the first EIF event of the application. TraceMessageEvent.Raise(&quot;Trace Message Event From Webform1.aspx&quot;)

Catch ex as Exception Response.Write(&quot;ExceptionThrown!. Error Message: &quot; & ex.Message.ToString) Finally 'Restore thread identity. if (Not ( IsNothing(context))) Then context.undo End If

End Try



STATUS
This behavior is by design.

