Config file gave "could not load file or assembly" error

Topics: Exception Handling Application Block, Logging Application Block
Oct 3, 2011 at 12:38 PM

Hi,

I want to use Enterprise Library in my class library (vs 2010 c#) project.  Following is the code I use to load my config file that I created using Entlib config .Net 4

I have used the code in another application on a different machine (window 7) successfully. but on my laptop it gave error "Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies." and indicate that it's on line 4 in config file.

exceptionsSource = new FileConfigurationSource(@"C:\sandbox.config");
            exceptionFactory = new ExceptionPolicyFactory(exceptionsSource);  //error here
            exceptionPolicy = exceptionFactory.Create("AllExceoptionPolicy");
This the first serval lines in my config file.
<configuration>
    <configSections>
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
        <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
 
Thanks for any help!
Oct 3, 2011 at 3:02 PM

What is the operating system of your laptop?

Oct 3, 2011 at 3:03 PM

My laptop is XP SP3. Thanks!

Oct 4, 2011 at 2:04 AM

Your laptop met the OS requirements...

Can you try configuring this using the Ent Lib 5 Configuration Tool and see if it works?

Hope this helps! :-)

Oct 4, 2011 at 3:07 AM

The config file was created using the EntLib 5 Configuration Tool.

Thanks!

Oct 4, 2011 at 3:49 AM

I see.

Can you post your entire configuration here? (C:\sandbox.config and app/web.config as well)

 

Oct 4, 2011 at 1:43 PM

Thank you for taking time. Because my app is a class library implemented for a Win32 app (ArcGIS Explorer), so I don't have web.config. I have to put all the config in my config file and load it in using the code I posted. Here is the config.

<configuration>
    <configSections>
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
        <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <loggingConfiguration name="" tracingEnabled="true" defaultCategory="ErrorLog">
        <listeners>
            <add name="FileTraceListener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                fileName="C:\URS_Projects\NYGISRCPT\App\NYRCPTAGXAddin\trace.log"
                formatter="Text Formatter" filter="Error" />
        </listeners>
        <formatters>
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
                name="Text Formatter" />
        </formatters>
        <categorySources>
            <add switchValue="Warning" name="ErrorLog">
                <listeners>
                    <add name="FileTraceListener" />
                </listeners>
            </add>
        </categorySources>
        <specialSources>
            <allEvents switchValue="All" name="All Events" />
            <notProcessed switchValue="All" name="Unprocessed Category" />
            <errors switchValue="All" name="Logging Errors &amp; Warnings">
                <listeners>
                    <add name="FileTraceListener" />
                </listeners>
            </errors>
        </specialSources>
    </loggingConfiguration>
    <exceptionHandling>
        <exceptionPolicies>
            <add name="NYRCPTExceptionPolicy">
                <exceptionTypes>
                    <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                        postHandlingAction="NotifyRethrow">
                        <exceptionHandlers>
                            <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                                logCategory="ErrorLog" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
                                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                                priority="0" />
                        </exceptionHandlers>
                    </add>
                </exceptionTypes>
            </add>
        </exceptionPolicies>
    </exceptionHandling>
</configuration>

Oct 5, 2011 at 3:26 AM

Your configuration looks fine.

I find it strange that it works on Windows 7 and not on your XP SP3...

Have you referenced the Microsoft.Practices.EnterpriseLibrary.ExceptionHandling DLL in your project?

Another thing to try is to install Ent Lib 5 Optional Update 1 and see if it works (you can find it here: http://www.microsoft.com/download/en/details.aspx?id=6836)

Oct 5, 2011 at 12:19 PM

I have the reference to these dlls that related to exception handling and all these dlls are in the same folder as my class library dll.

Microsoft.Practices.EnterpriseLibrary.Common.dll

Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll

Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.dll

This is the complete error message (the NYRCPTEntLib.config is the sandbox.config I sent out in previous message) and looks like there is some permission issue?

Exception when attempting to create an instance of the 'NYRCPTAGXAddin.NYRCPTAddin' System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration section handler for exceptionHandling: Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. (C:\Documents and Settings\bing_zhu\Application Data\ESRI\ArcGIS Explorer\AddIns\Cache\NYRCPTAGXAddin\NYRCPTEntLib.config line 4) ---> System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
at System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost host, String typeString, Boolean throwOnError)
at System.Configuration.MgmtConfigurationRecord.CreateSectionFactory(FactoryRecord factoryRecord)
at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere)
--- End of inner exception stack trace ---
at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
at System.Configuration.BaseConfigurationRecord.GetSection(String configKey, Boolean getLkg, Boolean checkPermission)
at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
at System.Configuration.Configuration.GetSection(String sectionName)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource.DoGetSection(String sectionName)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileBasedConfigurationSource.GetSection(String sectionName)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.ConfigSectionLocator.GetRegistrationsInternal(IConfigurationSource configurationSource, Func`3 registrationsAccessor)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.ConfigSectionLocator.GetRegistrations(IConfigurationSource configurationSource)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.CompositeTypeRegistrationsProviderLocator.b__0(ITypeRegistrationsProvider l, IConfigurationSource cs)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.CompositeTypeRegistrationsProviderLocator.<>c__DisplayClass5.b__4(ITypeRegistrationsProvider l)
at System.Linq.Enumerable.d__14`2.MoveNext()
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.Unity.UnityContainerConfigurator.RegisterAllCore(IConfigurationSource configurationSource, ITypeRegistrationsProvider rootProvider)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.ChangeTrackingContainerConfigurator.RegisterAll(IConfigurationSource configurationSource, ITypeRegistrationsProvider rootProvider)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.ConfigureContainer(ITypeRegistrationsProvider locator, IContainerConfigurator configurator, IConfigurationSource configSource)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.ConfigureContainer(IContainerConfigurator configurator, IConfigurationSource configSource)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.EnterpriseLibraryContainer.CreateDefaultContainer(IConfigurationSource configurationSource)
at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.ContainerBasedInstanceFactory`1..ctor(IConfigurationSource configurationSource)
at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyFactory..ctor(IConfigurationSource configurationSource)
at NYRCPTAGXAddin.Infrastructure.NYRCPTExceptionHandler..ctor()
at NYRCPTAGXAddin.NYRCPTAGXAddinBootstrapperMEF..ctor()
at NYRCPTAGXAddin.NYRCPTAddin..ctor()
--- End of inner exception stack trace ---
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at ExplorerCore.AddIn.CreateInstance()
Oct 6, 2011 at 6:26 AM

Hi,

It seems that you are creating a policy which aren't defined on your config. Try to replace the code:

exceptionFactory.Create("AllExceoptionPolicy")
with
exceptionFactory.Create("NYRCPTExceptionPolicy")

But I'm not sure if that is the only issue here since according to the exception it might be a privilege issue. Can you check if the file exists on the path or the application have an access to it? Also, try adding entlib dll to your application (not on the class library you are referencing to) and see if this works?

 

Noel Angelo Bolasoc
Avanade Software and Cloud Services
Avanade, Inc.
Contact 

Oct 6, 2011 at 4:17 PM

Hi,

The problem was the privilege! Once I copied the needed dlls in the host application bin folder. It works. But the host is a stand along commercial application and it's not good solution for us to throw extra dlls in their bin folder.

Is there any other way to point to these dlls to a location that my class library can control? Is it possible to put the path in config file?

Thanks for your time!