Is it possible to split the configuration for an enterprise library block into multiple files?
For example: I have three assemblies and one hosting project. I want to store the entlib v6 Exception Handling Application Block (EHAB) configuration for each assembly in a separate config file located in the particular assembly.
The hosting project references the three assemblies:
assembly_X
ehabX.config
assembly_Y
ehabY.config
assembly_Z
ehabZ.config
Hosting project
using ehabX.config
using ehabY.config
using ehabZ.config
I already tried the following:
App.config in the hosting project:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common" requirePermission="true" />
</configSections>
<enterpriseLibrary.ConfigurationSource>
<sources>
<add name="ehabX" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabX.config" />
<add name="ehabY" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabY.config" />
<add name="ehabZ" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabZ.config" />
</sources>
<redirectSections>
<add sourceName="ehabX" name="exceptionHandling" />
<add sourceName="ehabY" name="exceptionHandling" />
<add sourceName="ehabZ" name="exceptionHandling" />
</redirectSections>
</enterpriseLibrary.ConfigurationSource>
</configuration>
ehabX.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"/>
</configSections>
<exceptionHandling>
<exceptionPolicies>
<add name="Swallow NotImplementedException">
<exceptionTypes>
<add type="System.NotImplementedException, mscorlib" postHandlingAction="None" name="NotImplementedException"/>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
</configuration>
ehabY.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"/>
</configSections>
<exceptionHandling>
<exceptionPolicies>
<add name="Swallow ArgumentException">
<exceptionTypes>
<add type="System.ArgumentException, mscorlib" postHandlingAction="None" name="NotImplementedException"/>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
</configuration>
The ehabZ.config is omitted.
Using the EHAB with the policy "Swallow NotImplementedException" works fine. But if I try to use the policy "Swallow ArgumentException" defnied in ehabY.config I get this error message:
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException: The policy with name 'Swallow ArgumentException' cannot be found. Exception handling aborted.
Any suggestions?
Unfortunately, there is nothing out of the box that will let you merge multiple configuration sources into one configuration set.
I think you can probably do what you want but you will have to do some coding. You'll need to read in the appropriate configuration sources and create a custom IConfigurationSource that will merge them all. Additive merge of different config sources? has an example of a MergeConfigurationSource that could help you.
Related
I want to eventually set up Paypal payment. Step one though is to have a play. And I have failed on the quick start!
https://github.com/paypal/PayPal-NET-SDK/wiki/Quick-Start shows a code example. It explains that first, I need to download the PayPal .NET SDK package via NuGet. This is great, I'm on .NET Framework (not core). I install it.
Phase 2 gives an entire example. I add some PayPal config settings to the app.config file. Done
Phase 3 is where it is going wrong. I'm referencing PayPal.API in my C# class.
The relevant part of my code is
using PayPal.Api;
using System.Collections.Generic;
namespace TestProj.Payment
{
public class PaypalGateway
{
public void Sandbox()
{
var config = ConfigManager.Instance.GetProperties();// this is the fault
var accessToken = new OAuthTokenCredential(config).GetAccessToken();
config always has 0 instances
My Project has an App.config file with the correct values
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="paypal" type="PayPal.SDKConfigHandler, PayPal" />
</configSections>
<paypal>
<settings>
<add name="mode" value="sandbox" />
<add name="clientId" value="***" />
<add name="clientSecret" value="***" />
</settings>
</paypal>
If I rename my App.config file to blah.nonsense then the same issue occurs. It's as if the API is looking for a file else where or that it isn't configured correctly.
This question is tagged with asp.net-mvc, so the assumption is that this is for a web project
The quick start clearly states
Add the following to your web.config or app.config
web.config should where you place the configuration details if this is in fact a web project.
Even if the referenced code is for another class library in the project, which can use app.config file, all the settings will eventually have to be in the root web.config file of the web project as that is the process that will be running.
the same problem was with me, the keyword PayPal must be in small caps like paypal.
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="paypal" type="PayPal.SDKConfigHandler, **PayPal**" />
</configSections>
<paypal>
<settings>
<add name="mode" value="sandbox" />
<add name="clientId" value="***" />
<add name="clientSecret" value="***" />
</settings>
</paypal>
should be
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="paypal" type="PayPal.SDKConfigHandler, **paypal**" />
</configSections>
<paypal>
<settings>
<add name="mode" value="sandbox" />
<add name="clientId" value="***" />
<add name="clientSecret" value="***" />
</settings>
</paypal>
I am trying to move connectionStrings to external file using EntLib, but
ConfigurationManager.ConnectionStrings gives me only default ASP.NET database, which is not even in config.
data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
I expected to see there two connection string from shared.config.
What can be the problem, or do I have a wrong understanding this feature?
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<enterpriseLibrary.ConfigurationSource selectedSource="System Configuration Source">
<sources>
<add name="System Configuration Source" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add name="Shared" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
filePath="shared.config" />
</sources>
<redirectSections>
<add sourceName="Shared" name="connectionStrings" />
</redirectSections>
</enterpriseLibrary.ConfigurationSource>
</configuration>
shared.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="connStr1" connectionString="Data Source=local;Initial Catalog=DB1;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="connStr2" connectionString="Data Source=local;Initial Catalog=DB2;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Actually, ConfigurationManager.ConnectionStrings does not know anything about Enterprise Library infrastructure.
By default, sections cannot be redirected with Enterprise Library's ConfigurationSource.
You should use configSource attribute, which is feature of .NET Framework, not Enterprise Library.
app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings configSource="sharedConnectionStrings.config" />
</configuration>
sharedConnectionStrings.config:
<?xml version="1.0" encoding="utf-8" ?>
<connectionsStrings>
<add name="connStr1"
connectionString="Data Source=local;Initial Catalog=DB1;Integrated Security=True"
providerName="System.Data.SqlClient" />
<add name="connStr2"
connectionString="Data Source=local;Initial Catalog=DB2;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionsStrings>
Update: If you want to use Enterprise Library features, don't use ConfigurationManager.ConnectionStrings directly. Instead, use Enterprise Library features to create database connections like:
Database db = DatabaseFactory.CreateDatabase(“Tom’s Connection”);
Then you can use Enterprise Library's configuration sources.
I found this link with example useful.
I have a simple app.config file that doesn't quite pass the IDEs checker.
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="XmlRoot" type="System.String"/>
</configSections>
<connectionStrings>
<omitted/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<XmlRoot>
<add key="relativepath" value=""/>
</XmlRoot>
</configuration>
I added the <section name="XmlRoot" type="System.String" /> part to the config and then I tried to define the name and key here: <add key="relativepath" value=""/>. But for some reason the IDE gives me the following Messages:
I rarely use the app.config file so it could just be a noob mistake. How do I make it recognize my tags?
If it's just a simple string, you can use AppSettings to accomplish this.
<configuration>
<appSettings>
<add key="relativepath" value="mypath" />
</appSettings>
</configuration>
Access it from C# like this:
string path = ConfigurationManager.AppSettings["relativepath"]);
Try changing your type to System.Configuration.NameValueSectionHandler
this is how I define my sections:
<configSections>
<sectionGroup name="MySettings">
<section name="serverConfiguration" type="System.Configuration.NameValueSectionHandler"></section>
<section name="sqlSettings" type="System.Configuration.NameValueSectionHandler"></section>
</sectionGroup>
</configSections>
...
<MySettings>
<sqlSettings>
<add key="sqlTransactionTimeOut" value="0" />
</sqlSettings>
<serverConfiguration>
<add key="resolveDnsAsync" value="true" />
</serverConfiguration>
</MySettings>
Probably you need to use the section appSettings
<configuration>
<appSettings>
<!-- and Here you define the key and the value like you do-->
<add key="relativepath" value="path"/>
</appSettings>
</configuration>
then in your code write something like this to read it.
String path = ConfigurationManager.AppSettings["relativepath"].ToString();
hope it works for you.
:)
I am trying to implement SQL Azure Transient Fault Configuration in my ASP.NET application. I am trying to following: http://msdn.microsoft.com/en-us/library/hh680899(v=pandp.50).aspx
Though I didn't find the block on how can I configure this in config file. I am getting The type RetryManager does not have an accessible constructor. error in my code. I read in some forum that I should configure it using config file, but how is my question. Thanks.
EDIT: I am using Enterprise Lib 5.0
Here's a sample configuration:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="RetryPolicyConfiguration" type="Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.Configuration.RetryPolicyConfigurationSettings, Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling, Version=5.1.1209.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 useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<RetryPolicyConfiguration defaultRetryStrategy="Incremental Retry Strategy" defaultSqlConnectionRetryStrategy="Backoff Retry Strategy" defaultSqlCommandRetryStrategy="Incremental Retry Strategy" defaultAzureStorageRetryStrategy="Incremental Retry Strategy" defaultAzureServiceBusRetryStrategy="Fixed Interval Retry Strategy">
<incremental name="Incremental Retry Strategy" retryIncrement="00:00:05" maxRetryCount="5" />
<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>
<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>
<appSettings>
</appSettings>
</configuration>
I have a console application with a config file called app.config. The full code behind by app.config file is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ClientSettingsProvider.ServiceUri" value=""/>
<add key="Server" value="0.0.0.0"/>
<add key="DB" value="Test"/>
<add key="UserName" value="testuser"/>
<add key="Password" value="testuser"/>
<add key="AgentEmail" value="test#gmail.com"/>
</appSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
</configuration>
When I try to run my application, I get an error : Configuration system failed to initialize. Any idea what am I doing wrong?
Remove the configSections from the file if you don't require it.
You're defining a section group and a contained section in your config here:
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="LeadDataEmail.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
That means, there must be a <applicationSettings> container, with a contained <LeadDataEmail.Properties.Settings> XML element in your <configuration>:
<configuration>
<configSections>
......
</configSections>
<applicationSettings>
<LeadDataEmail.Properties.Settings
attribute1="value1"
attribute2="value2" />
</applicationSettings>
....
</configuration>
but I don't see any trace of that in your config file - no wonder the config system cannot initialize!