Multiple Configuration Sources for Enterprise Library 4.1? - c#

We use the caching and logging application blocks from entlib 4.1. We want to keep the configuration of those two in seperate files. How can we achieve this?
It looks like entlib is always using the selectedSource as it configuration.
I tried the following:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=9057346a2b2dcfc8" />
</configSections>
<enterpriseLibrary.ConfigurationSource selectedSource="messagesCache">
<sources>
<add name="messagesCache" filePath="Configuration\\messagesCache.config" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=9057346a2b2dcfc8" />
<add name="logging" filePath="Configuration\\logging.config" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common, Version=4.1.0.0, Culture=neutral, PublicKeyToken=9057346a2b2dcfc8" />
</sources>
</enterpriseLibrary.ConfigurationSource>
</configuration>
But this doesn't work because the application blocks always use the selectedSource attribute value.

As noted in External configuration files in Enterprise Library for .NET Framework 2.0:
[...] while you can configure as many
Configuration Sources as you want
using the tool, only one is ‘selected’
to be the one which Enterprise Library
will automatically use [...]
What I have done is use the configSource attribute:
<configuration>
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<section name="validationConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections>
<loggingConfiguration configSource="logging.config"/>
<exceptionHandlingConfiguration configSource="exceptionHandling.config"/>
<dataConfiguration configSource="dataAccess.config"/>
<validationConfiguration configSource="validation.config"/>
</configuration>
It works great, but the downside is that if you are editing your application/web config file using the configuration tool and save your configuration it will be saved inside the application/web config file.

Related

Could not load file or assembly 'Oracle.ManagedDataAccess'

we are developing a windows service which connect to oracle database. We use
Oracle ManagedDataAccess from Nuget package.
When running the windows service we are receving below error. Tried to get details from stackoverflows and nothing is able to resolve the problem.
Our config file is as below
Could not load file or assembly 'Oracle.ManagedDataAccess,
Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342'
or one of its dependencies
Config
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="eClaimsService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
<section name="oracle.manageddataaccess.client"
type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
</sectionGroup>
</configSections>
<system.data>
<DbProviderFactories>
<remove invariant="Oracle.ManagedDataAccess.Client"/>
<add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver"
type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
</DbProviderFactories>
</system.data>
<system.web>
<httpRuntime executionTimeout="3600" requestValidationMode="2.0" maxRequestLength="10240"/>
<sessionState mode="InProc" timeout="60"/>
<pages validateRequest="false" />
</system.web>
</configuration>
In my case just go to Visual Studio 20xx Developer Command Prompt and open as Administrator.
Then run the command
C:\Windows\System32>gacutil /u Oracle.ManagedDataAccess
And you are able to uninstall the data successfully.
Now I am able to access application without any error.
I got this error when my App.Config contained an old version of the DLL, I just removed this entry from the App.Config and it worked

VS 2017 Intellisense Not Working without Views Web.Config

I work on a large Visual Studio Solution (Sitecore Helix), with separate "feature" Projects. Some Projects have a Views > Web.config file and others do not.
During our build process, all Views end up one location/project and in that final destination the Views folder does have it's production Web.config file.
However, without a Views > Web.config file at individual the Project, there is no IntelliSense for the (dynamic) #model. Strongly typed Objects (#Html.Raw) have autocomplete/IntelliSense. As a front-end person, I really rely on the IntelliSense.
Is there a better/more appropriate way of adding these references other than to adding an individual Web.config (example below) to each Project, especially since I just need this for debug/development.
Example Web.config, please note this is not my entire Web.config file, but specifically the lines I have narrowed down to make the IntelliSense work in this case.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="Sitecore.Mvc" />
<add namespace="Sitecore.Mvc.Presentation" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="GCC.Feature.WebAdmin" />
</namespaces>
</pages>
</system.web.webPages.razor>
</configuration>
IntelliSense working on something other than #model
IntelliSense not working on #model no web config
IntelliSense working with Web.config

Can't set the app.config property of a dll using host app.config file

I have two projects. One is WCF service library. Another is Windows Service. Each project has its own app.config files. In windows service I am using wcf service library as refrence(.dll).In wcf service library app.config file there is a property called "DebugMode".I need to a set this property in windows service app.config file.So that it will affect the wcf service library.
but unfortunately just copy and paste the property setting is not working for me..
I am accessing property using Properties.Settings.Default.DebugMode syntax. C#, WPF.
Try this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="WindowsFormsApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
<sectionGroup name="librarySettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ClassLibrary1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<WindowsFormsApplication1.Properties.Settings>
<setting name="HostSetting" serializeAs="String">
<value>Foo</value>
</setting>
</WindowsFormsApplication1.Properties.Settings>
</applicationSettings>
<librarySettings>
<ClassLibrary1.Properties.Settings>
<setting name="LibrarySetting" serializeAs="String">
<value>Bar</value>
</setting>
</ClassLibrary1.Properties.Settings>
</librarySettings>
</configuration>
This creates two section groups in your app config of your host application. One is for the settings of the host itself and the other is for the settings that are defined in your library.

Get razor intellisense in library project?

I have a business project where I will be creating emails based on templates made with razor. How can I get razor intellisense there? Is it as simple as including a few assemblies or do I need to do something more retorted?
This is for using the RazorEngine library.
You have to edit your .csproj file. Add following ProjectTypeGuids node (add just bellow them bellow existing ProjectGuid node).
<ProjectGuid>{28AD1627-3486-48C2-A045-EFFBB441582B}</ProjectGuid>
<ProjectTypeGuids>{E3E379DF-F4C6-4180-9B81-6769533ABE47};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
Save file, then reopen it with Visual Studio. VS2012 then performs some conversion, but at the end everything is OK. Tooling is there.
Depending what you are doing (Razor Generator?) you will need some references (System.Web, System.Web.WebPages, System.Web.Mvc, System.Web.Razor, System.Web.Routing...).
This was tested with VS2012.
In Visual Studio 2013 I had to also replace the following line in the .csproj file because it pointed to a wrong loaction:
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
This is a nice guide to achieve this.
http://thetoeb.de/2014/01/05/enabling-mvc5-intellisense-in-a-classlibrary-project/
The steps are:
Add the MVC (5.0) nuget package (right click project in solution explorer -> Manage NuGet Packages -> search for MVC and install “Microsoft ASP.NET MVC”)
Close any and all open .cshtml files
Right click project -> Properties -> Build -> change Output path to “bin/”
Add the following minimal Web.config to the root of your class library project (the web config file is solely needed for intellisense. Configuration (via Web.config) should be done in the WebApplication hosting your ClassLibrary assembly)
Clean and Build the solution.
Web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
</system.web>
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<!-- add other namespaces for views here -->
<!-- e.g. your own project's, Lib.Views.Etc -->
</namespaces>
</pages>
</system.web.webPages.razor>
</configuration>

Assembly specific settings not loading at runtime

I am developing a .NET 3.5 Windows Forms app. I have two projects, the UI and a Library.
UI uses strongly typed settings that are stored, as usually, in the app.config file. I read them using UI.Properties.Settings class (generated by Visual Studio).
Library uses its own strongly typed Settings (the Settings.settings file that is dumped into Library.config file).
At runtime, Library settings will not reload from the Library.config file. Only UI.config file is read by the runtime. So I am stuck with default settings in Library assembly, and cannot provide values after deployment.
To summarize: for an application assembly that is not the main executable, the settings are not read at assembly load time.
I know I can use ConfigurationManager.AppSettings["value"] and this will read from the default app config file (UI.config) but what can I do if I want strongly typed settings (read with Properties.Settings class)?
Calling Library.Properties.Settings.Default.Reload() won't do it.
Thanks.
What you need to do is merge your library config sections to app.connfig. Merging config files is done by first adding elements inside the <configSections> config element, to identity the config section and then by adding the config elements inside the configuration element.
Example of merging config files:
App config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="CA3.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<CA3.Settings>
<setting name="Setting" serializeAs="String">
<value>2</value>
</setting>
</CA3.Settings>
</userSettings>
</configuration>
Library config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="CA3.Library" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<CA3.Library>
<setting name="Setting" serializeAs="String">
<value>1</value>
</setting>
</CA3.Library>
</userSettings>
</configuration>
Merged app.config containing both library and app configuration.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="CA3.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<section name="CA3.Library" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<CA3.Settings>
<setting name="Setting" serializeAs="String">
<value>2</value>
</setting>
</CA3.Settings>
<CA3.Library>
<setting name="Setting" serializeAs="String">
<value>1</value>
</setting>
</CA3.Library>
</userSettings>
</configuration>
You can only have one .config file per AppDomain. For a Windows Forms application, that .config file is the file that is named after the executable - that is: UI.exe.config in your case.
This means that while you may have the Library.dll.config file present, the configuration system is never going to pick it up.
You should merge the library's configuration settings into the application configuration file. Configuration files for libraries are not supported by the .NET framework.
Even better, redesign your library so that it does not rely on configuration files, but rather uses Imperative Configuration.

Categories