Transient Fault Handling using retry strategy in configuration files

Topics: General discussion
Jan 4, 2012 at 1:45 AM


Hi,

I have the following piece of code in a simple Console app:

            EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(new FileConfigurationSource("ScratchPad.exe.config", false));
            var retryManager = EnterpriseLibraryContainer.Current.GetInstance<RetryManager>();

Line 2 throws this exception:

Microsoft.Practices.ServiceLocation.ActivationException was unhandled
  Message=Activation error occured while trying to get instance of type RetryManager, key ""
  Source=Microsoft.Practices.ServiceLocation
  StackTrace:
       at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 57
       at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance[TService]() in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 90
       at ScratchPad.Program.Main(String[] args) in C:\Laptop_backup\MyStocks_Azure_Emulator\ScratchPad\Program.cs:line 26
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: Microsoft.Practices.Unity.ResolutionFailedException
       Message=Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.RetryManager", name = "(none)".
Exception occurred while: while resolving.

 

My app.config looks like this:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="RetryPolicyConfiguration" type="Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.Configuration.RetryPolicyConfigurationSettings, Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling, Vrsion=5.0.1118.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <RetryPolicyConfiguration defaultRetryStrategy="Fixed Interval Retry Strategy"
    defaultSqlConnectionRetryStrategy="Backoff Retry Strategy"
    defaultSqlCommandRetryStrategy="Incremental Retry Strategy"
    defaultAzureStorageRetryStrategy="Fixed Interval Retry Strategy"
    defaultAzureServiceBusRetryStrategy="Fixed Interval Retry Strategy"
    defaultAzureCachingStrategyName="Fixed Interval Retry Strategy"
    defaultAzureStorageStrategyName="Fixed Interval Retry Strategy"
    >
    <incremental name="Incremental Retry Strategy" retryIncrement="00:00:01"
        retryInterval="00:00:01" maxRetryCount="10" />
    <fixedInterval name="Fixed Interval Retry Strategy" retryInterval="00:00:01"
        maxRetryCount="10" />
    <exponentialBackoff name="Backoff Retry Strategy" minBackoff="00:00:01"
        maxBackoff="00:00:30" deltaBackoff="00:00:10" maxRetryCount="10"
        fastFirstRetry="false"/>
  </RetryPolicyConfiguration>
</configuration>

Please help!

Thanks,

Jagadish<!--l version="1.0-->

Jan 4, 2012 at 2:52 AM
Edited Jan 4, 2012 at 2:56 AM

The configuration contains errors (specifically change defaultAzureCachingStrategyName, remove defaultAzureStorageStrategyName and the incremental and exponentialBackoff attributes).  The configuration should look something like this:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="RetryPolicyConfiguration" type="Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.Configuration.RetryPolicyConfigurationSettings, Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling, Vrsion=5.0.1118.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <RetryPolicyConfiguration defaultRetryStrategy="Fixed Interval Retry Strategy"
    defaultSqlConnectionRetryStrategy="Backoff Retry Strategy"
    defaultSqlCommandRetryStrategy="Incremental Retry Strategy"
    defaultAzureStorageRetryStrategy="Fixed Interval Retry Strategy"
    defaultAzureServiceBusRetryStrategy="Fixed Interval Retry Strategy"
    defaultAzureCachingRetryStrategy="Fixed Interval Retry Strategy"
    >
    <incremental name="Incremental Retry Strategy" maxRetryCount="10" 
        retryIncrement="00:00:01" initialInterval="00:00:01" firstFastRetry="true"/>
    <fixedInterval name="Fixed Interval Retry Strategy" retryInterval="00:00:01"
        maxRetryCount="10" />
    <exponentialBackoff name="Backoff Retry Strategy" minBackoff="00:00:01"
        maxBackoff="00:00:30" deltaBackoff="00:00:10" maxRetryCount="10"/>
  </RetryPolicyConfiguration>
</configuration>

If you install the EnterpriseLibrary.Config Visual Studio extension and use nuget to install the Transient Fault Handling Bock you can use the configuration tool to create (and validate) the configuration information.


--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Jan 4, 2012 at 6:47 PM

Hi Randy,

Thanks for the response. I authored the config file using the VS extension. Now my code works!

I have included the generated config file for completeness.

Thanks,

Jagadish

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="RetryPolicyConfiguration" type="Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.Configuration.RetryPolicyConfigurationSettings, Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling, Version=5.0.1118.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    <section name="typeRegistrationProvidersConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.TypeRegistrationProvidersConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>
  <RetryPolicyConfiguration defaultRetryStrategy="Fixed Interval Retry Strategy">
    <incremental name="Incremental Retry Strategy" />
    <fixedInterval name="Fixed Interval Retry Strategy" />
    <exponentialBackoff name="Exponential Backoff Retry Strategy" />
  </RetryPolicyConfiguration>
  <typeRegistrationProvidersConfiguration>
    <clear />
    <add name="Caching" sectionName="cachingConfiguration" />
    <add name="Cryptography" sectionName="securityCryptographyConfiguration" />
    <add name="Exception Handling" sectionName="exceptionHandling" />
    <add name="Instrumentation" sectionName="instrumentationConfiguration" />
    <add name="Logging" sectionName="loggingConfiguration" />
    <add name="Policy Injection" sectionName="policyInjection" />
    <add name="Security" sectionName="securityConfiguration" />
    <add name="Data Access" providerType="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSyntheticConfigSettings, Microsoft.Practices.EnterpriseLibrary.Data" />
    <add name="Validation" providerType="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationTypeRegistrationProvider, Microsoft.Practices.EnterpriseLibrary.Validation" />
    <add sectionName="RetryPolicyConfiguration" name="RetryPolicyConfiguration" />
  </typeRegistrationProvidersConfiguration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

Jul 23, 2012 at 1:52 PM

I am experiencing this same problem with Azure SDK 1.7 and the latest EntLib NuGet packages.  I have used to configuration tool to edit the configuration, I have also cut and pasted the "working" configuration above.  I still get the same problem.

Is there anything else I can do to remedy this?  This is preventing us from upgrading SDKs.

 

Here's my relevant config sections:

  <configSections>
    <section name="RetryPolicyConfiguration" type="Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.Configuration.RetryPolicyConfigurationSettings, Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling, Version=5.0.1118.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    <section name="typeRegistrationProvidersConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.TypeRegistrationProvidersConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  </configSections>

...

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <RetryPolicyConfiguration 
    defaultRetryStrategy="Fixed" 
    defaultSqlConnectionRetryStrategy="Backoff" 
    defaultSqlCommandRetryStrategy="Incremental" 
    defaultAzureServiceBusRetryStrategy="Fixed" 
    defaultAzureCachingRetryStrategy="Fixed" 
    defaultAzureStorageRetryStrategy="Fixed">
    <incremental name="Incremental" maxRetryCount="10" retryIncrement="00:00:01" initialInterval="00:00:01" />
    <fixedInterval name="Fixed" maxRetryCount="10" retryInterval="00:00:01" />
    <exponentialBackoff name="Backoff" maxRetryCount="10" minBackoff="00:00:01" maxBackoff="00:00:30" deltaBackoff="00:00:10" />
  </RetryPolicyConfiguration>
  <typeRegistrationProvidersConfiguration>
    <clear />
    <add name="Caching" sectionName="cachingConfiguration" />
    <add name="Cryptography" sectionName="securityCryptographyConfiguration" />
    <add name="Exception Handling" sectionName="exceptionHandling" />
    <add name="Instrumentation" sectionName="instrumentationConfiguration" />
    <add name="Logging" sectionName="loggingConfiguration" />
    <add name="Policy Injection" sectionName="policyInjection" />
    <add name="Security" sectionName="securityConfiguration" />
    <add name="Data Access" providerType="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSyntheticConfigSettings, Microsoft.Practices.EnterpriseLibrary.Data" />
    <add name="Validation" providerType="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationTypeRegistrationProvider, Microsoft.Practices.EnterpriseLibrary.Validation" />
    <add sectionName="RetryPolicyConfiguration" name="RetryPolicyConfiguration" />
  </typeRegistrationProvidersConfiguration>

Jul 24, 2012 at 1:29 AM

I'm assuming by the same issue you mean you are getting the following exception:

Microsoft.Practices.ServiceLocation.ActivationException was unhandled
  Message=Activation error occured while trying to get instance of type RetryManager, key ""
  Source=Microsoft.Practices.ServiceLocation

When you try to get a RetryManager:

 var retryManager = EnterpriseLibraryContainer.Current.GetInstance<RetryManager>();

Is this correct?

That exception occurs when the configuration is not loaded. The posted configuration is OK so that leads me to think that the configuration is not being loaded.  Can you verify that the configuration is located in app.config (worker role) or web.config (web role) and that the configuration file is being deployed to Azure.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Jul 24, 2012 at 12:48 PM

I figured out that it's because the configuration is not loaded yet, since this is referenced before the web app starts (indirectly from WebRole.OnStart() )

 

So, I had to do this trick in the WebRole:

            EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(
                new FileConfigurationSource(configFile, false)); 

I really wish this could read the config section _only_ though and not require a whole <configuration> element in the file.