System.UnauthorizedAccessException

What is causing this exception?

Today, I was pushing out an old .NET 1.1 application in Visual Studio 2003 that was using the Microsoft Enterprise Data Blocks 1.1 to a new development image in our pre-production environment. It had been a while since I had to install the application in a development environment and was bumping into all sorts of nice surprises.

Every time I ran the application, I would get the following error message when I made a database call:

System.TypeInitializationException: The type initializer for "Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataCommandFailedEvent" threw an exception. ---> System.UnauthorizedAccessException: The source was not found, but some or all event logs could not be searched.  Inaccessible logs: Security.
   at System.Diagnostics.EventLog.FindSourceRegistration(String source, String machineName, Boolean readOnly)
   at System.Diagnostics.EventLog.SourceExists(String source, String machineName)
   at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData)
   at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category)
   at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID)
   at System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType type)
   at Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.PerformanceCounterInstances.ReportCounterFailure(String message)
   at Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.PerformanceCounterInstances..ctor(String categoryName, String counterName, Boolean createNewInstance)
   at Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.InstrumentedEvent.AddPerformanceCounter(String category, String[] counterNames, Boolean createNewInstance)
   at Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.InstrumentedEvent.Initialize(String counterCategory, String[] counterNames, Boolean createNewInstance, String eventLogSource, EventLogIdentifier[] eventIds)
   at Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation.InstrumentedEvent..ctor(String counterCategory, String[] counterNames, Boolean createNewInstance)
   at Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataServiceEvent..ctor(String[] counterNames)
   at Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataCommandFailedEvent..ctor(String[] counterNames)
   at Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataCommandFailedEvent..cctor()
   --- End of inner exception stack trace ---
   at Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataCommandFailedEvent.Fire(String commandText, String connectionString)
   at Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataInstrumentationFacade.CommandFailed(String commandText, String connectionString)
   at Microsoft.Practices.EnterpriseLibrary.Data.Database.DoLoadDataSet(DBCommandWrapper command, DataSet dataSet, String[] tableNames)
   at Microsoft.Practices.EnterpriseLibrary.Data.Database.LoadDataSet(DBCommandWrapper command, DataSet dataSet, String[] tableNames)
   at Microsoft.Practices.EnterpriseLibrary.Data.Database.LoadDataSet(DBCommandWrapper command, DataSet dataSet, String tableName)
   at Microsoft.Practices.EnterpriseLibrary.Data.Database.ExecuteDataSet(DBCommandWrapper command)
   at Microsoft.Practices.EnterpriseLibrary.Data.Database.ExecuteDataSet(CommandType commandType, String commandText)
   at 

I checked and rechecked my references to insure that everything was there and being copied over into the debug environment. I started digging and then I had a faint memory of having to do something special to one of the DLLs when it hit me.

I needed to run InstallUtil on the Microsoft.Practices.EnterpriseLibrary.Common.dll. You can tell if you have done this or not because the installer in the .dll that gets called will create a file named microsoft.practices.enterpriselibrary.common.InstallState.

You’ll want to start the SDK command window so that the paths for the utility will be available. That’s the easiest way. Just change to the folder where the DLL is and execute the following:

InstallUtil Microsoft.Practices.EnterpriseLibrary.Common.dll

What is happening in the above error is that there are instrumentation configuration settings that aren’t on the server in the registry and elsewhere. The InstallUtil installs server resources needed by the application blocks for logging and other things.

Let me know if this helped you out. Hopefully I can remember this next time now that I’ve blogged it.

Update: 
This link has some more discussions on the subject.

http://codebetter.com/blogs/eric.wise/archive/2005/03/21/60163.aspx

3 thoughts on “System.UnauthorizedAccessException

  1. Thanks for the info.
    I run to the same problem when installing an apps that use Enterprise Library. Thanks for your tip, it works well now.

    regards

    Jamal

  2. I was getting an error message Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataCommandFailedEvent threw an exception.

    I followed your steps to InstallUtil Microsoft.Practices.EnterpriseLibrary.Data.dll and it worked.

  3. Hi Kiesler…
    There are very few people who thinks the problem they encountered should not be occured to others. you are one of them. I too converting application from VS 2003 to VS 2005. You'r post is helped me lot. Thanks a lot man….

Comments are closed.