Configuration system failed to initialize at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey) - c#

Created a C# Service Project in Visual Studio 2015, putting it together from various code sources.
It compiles, I can install the service, and I can start the service, but the service won’t fire the logic. It is supposed to look at a particular folder for only XML files, then upload them to an FTP site.
I added a logger and it writes out the following error consistently:
FTP Upload Service Started 05/08/2016 10:44:42 AM FTP Upload Service
Error on: 05/08/2016 10:44:42 AM Configuration system failed to
initialize at
System.Configuration.ClientConfigurationSystem.EnsureInit(String
configKey) at
System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(String
sectionName) at
System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String
sectionName) at
System.Configuration.ConfigurationManager.get_AppSettings() at
FTPUploadService.Service1.ScheduleService() in
C:\user\Desktop\FTPUploadService\FTPUploadService\FTPUploadService\Service1.cs:line
51
I go to this particular line in the code (line 51), which is:
string mode = ConfigurationManager.AppSettings["Mode"].ToUpper();
For reference here is more of the code block:
try
{
Scheduler = new Timer(new TimerCallback(SchedulerCallback));
string mode = ConfigurationManager.AppSettings["Mode"].ToUpper();
this.WriteToFile("FTP Upload Service Mode: " + mode + " {0}");
//Set the Default Time.
DateTime scheduledTime = DateTime.MinValue;
if (mode == "DAILY")
{
//Get the Scheduled Time from AppSettings.
scheduledTime = DateTime.Parse(System.Configuration.ConfigurationManager.AppSettings["ScheduledTime"]);
if (DateTime.Now > scheduledTime)
{
//If Scheduled Time is passed set Schedule for the next day.
scheduledTime = scheduledTime.AddDays(1);
}
}
if (mode.ToUpper() == "INTERVAL")
{
//Get the Interval in Minutes from AppSettings.
int intervalMinutes = Convert.ToInt32(ConfigurationManager.AppSettings["IntervalMinutes"]);
//Set the Scheduled Time by adding the Interval to Current Time.
scheduledTime = DateTime.Now.AddMinutes(intervalMinutes);
if (DateTime.Now > scheduledTime)
{
//If Scheduled Time is passed set Schedule for the next Interval.
scheduledTime = scheduledTime.AddMinutes(intervalMinutes);
}
}
I’ve tried quite a few things, changing the XML format of the app.config, making sure the namespaces that are needed are there, checking the syntax of the XML for the app.config, to make sure it is correct…none have helped.
Here is the app.config:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="Mode" value="Interval"/>
<!-- <add key ="Mode" value ="Interval"/>-->
<add key="IntervalMinutes" value="1"/>
<add key="ScheduledTime" value="18:41"/>
<add key="sourceFiles" value="C:\XML Test\"/>
<add key="sourcePath" value="C:\XML Test\"/>
<add key="files" value="*.xml"/>
<add key="allDirectories" value="True"/>
<add key="addDateTimeBeforeUpload" value="True"/>
<add key="hostName" value="XXXXXXXXX"/>
<add key="Port" value="22"/>
<add key="UserName" value="XXXXXXX"/>
<add key="Password" value="XXXXXXXX"/>
<add key="SshHostKeyFingerprint" value=""/>
<add key="uploadpath" value="/XXXXXXX/"/>
<add key="removeSourceFiles" value="false"/>
<add key="SshPrivateKeyPath" value=""/>
<add key="protocol" value=""/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
Any suggestions?? Issue with Configuration Manager?

I've run into this issue before and it is most likely because the xml in your app.config is not setup properly I believe the "configSections" node must be the first child node after "configuration". Check out my xml below and give that a try.
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
</sectionGroup>
</configSections>
<appSettings>
<add key="Mode" value="Interval"/>
<!-- <add key ="Mode" value ="Interval"/>-->
<add key="IntervalMinutes" value="1"/>
<add key="ScheduledTime" value="18:41"/>
<add key="sourceFiles" value="C:\XML Test\"/>
<add key="sourcePath" value="C:\XML Test\"/>
<add key="files" value="*.xml"/>
<add key="allDirectories" value="True"/>
<add key="addDateTimeBeforeUpload" value="True"/>
<add key="hostName" value="XXXXXXXXX"/>
<add key="Port" value="22"/>
<add key="UserName" value="XXXXXXX"/>
<add key="Password" value="XXXXXXXX"/>
<add key="SshHostKeyFingerprint" value=""/>
<add key="uploadpath" value="/XXXXXXX/"/>
<add key="removeSourceFiles" value="false"/>
<add key="SshPrivateKeyPath" value=""/>
<add key="protocol" value=""/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>

Related

Multiple int values in config key C# XML

I want to add multiple int values to my Settings.
So far I have this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DigiPortServer1"
type="Configuration.Helpers.MultipleValuesSelection, Configuration.Helpers"
requirePermission="false"/>
</configSections>
<DigiPortServer1>
<add key="3" value="3"></add>
<add key="4" value="4"></add>
<add key="5" value="5"></add>
<add key="6" value="6"></add>
<add key="7" value="7"></add>
<add key="8" value="8"></add>
<add key="9" value="9"></add>
<add key="10" value="10"></add>
<add key="11" value="11"></add>
<add key="12" value="12"></add>
<add key="13" value="13"></add>
<add key="14" value="14"></add>
<add key="15" value="15"></add>
<add key="16" value="16"></add>
<add key="17" value="17"></add>
<add key="18" value="18"></add>
</DigiPortServer1>
</configuration>
Is this right? I have found many questions considerung multiple String values. How can I access these values? I would like to save these into a int array or something similar.
I would modify your file to be in this way. So it will be easier to access and to work with. This way you can easily access those features by writing ConfigurationManager.AppSettings["3"].ToString();
Note that your file wasn´t having the keys associated with any values. I added the value atribute.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DigiPortServer1"
type="Configuration.Helpers.MultipleValuesSelection, Configuration.Helpers"
requirePermission="false"/>
</configSections>
<appSettings>
<add key="3" value="3"></add>
<add key="4" value="4"></add>
<add key="5" value="5"></add>
<add key="6" value="6"></add>
<add key="7" value="7"></add>
<add key="8" value="8"></add>
<add key="9" value="9"></add>
<add key="10" value="10"></add>
<add key="11" value="11"></add>
<add key="12" value="12"></add>
<add key="13" value="13"></add>
<add key="14" value="14"></add>
<add key="15" value="15"></add>
<add key="16" value="16"></add>
<add key="17" value="17"></add>
<add key="18" value="18"></add>
</appSettings>
</configuration>
You could access the configuration section value using the ConfigurationManager GetSection method.
var section = System.Configuration.ConfigurationManager.GetSection("DigiPortServer1") as System.Collections.Specialized.NameValueCollection;
var value = section["keyname"];
If section is the name value pair then cast with above type (NameValueCollection) or you can use own type to cast.

XML Custom Configuration Error Handling

Here is some code of my app.config file in my windows form application. I used this configuration file for the printer and scanner. I have used custom configurations in this xml. The problem I'm facing is whenever any attribute value is not set, My application throws a built in message error box like "The port com 4 does not exists"
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Server" value="blah blah" />
<add key="Database" value="blah" />
<add key="User" value="blah" />
<add key="StoreID" value="blah" />
<add key="BranchID" value="blah" />
<add key="LotSize" value="100"/>
<add key="ZipsPrintingPath" value="blah.exe"/>
<add key="BCScannerPath" value="blah.exe"/>
<add key="SecuGenDeviceName" value="DEV_AUTO"/>
<add key="SecuGenPortAddress" value="USB_AUTO_DETECT"/>
<add key="SecuGenLiveTimeout" value="15000"/>
<add key="SecuGenImageQuality" value="50"/>
<add key="BCSPort" value="COM4"/>
<add key="BCSBaudRate" value="9600"/>
<add key="BCSParity" value="None"/>
<add key="BCSByteSize" value="8"/>
<add key="BCSStopBit" value="One"/>
<add key="BTicketBarcodeStartsWith" value="%B"/>
<add key="BTicketBarcodeEndsWith" value="."/>
<add key="TechSupportURL" value="blah"/>
</appSettings>
</configuration>
Here is also attached port error.
Now, I want to suppress this message box or I want to show this message in my custom message box.

Transform config in asp.net mvc isnt updated

I have asp.net mvc 4 project where try to use transform config, where have some default values and when I try to change to release mode some default values change to release values, but it isnt work. When I try to change to release mode and build application I have nothing changes. Here is my default values in Web.config:
<appSettings>
<add key="appId" value="####"/>
<add key="appSecret" value="####"/>
<add key="hostName" value="####"/>
</appSettings>
And here is what I have in my Web.Release.config:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="appId" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(name)" />
<add key="appSecret" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
<add key="hostName" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(name)"/>
</appSettings>
</configuration>
Does anybody help me?
Using xdt:Locator="Match(name)", you're trying to match an attribute name which does not exist. You're looking for key for the appSettings:
<add key="appId" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(key)" />
<add key="appSecret" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
<add key="hostName" value="!!!!" xdt:Transform="Replace" xdt:Locator="Match(key)"/>

How to get contents of a .config file using ConfigurationManager.GetSection(columnsSectionName) without altering the sections' order?

In my config section I have the following:
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
<section name="NFeColumns" type="System.Configuration.DictionarySectionHandler" />
<section name="CTeColumns" type="System.Configuration.DictionarySectionHandler" />
</configSections>
<appSettings>
<add key="csv.separator" value=";" />
<add key="infNFe.columns" value="NFeColumns"/>
<add key="infCte.columns" value="CTeColumns"/>
<add key="infNFe.filename" value=".\Extracted\NFeCollection.csv"/>
<add key="infCte.filename" value=".\Extracted\CTeCollection.csv"/>
</appSettings>
<NFeColumns>
<add key="infNFe.Id" value="Id" />
<add key="ide.cUF" value="cUF" />
<add key="dest.CNPJ" value="CNPJ" />
<add key="dest.xNome" value="xNome" />
<add key="det.nItem" value="nItem" />
<add key="prod.cProd" value="cProd" />
<add key="prod.xProd" value="xProd" />
<add key="IPI.cEnq" value="cEnq" />
</NFeColumns>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
I want to catch the values from and put in a IDictionary in the same order as the configuration file. I`m doing this:
string columnsSectionName = ConfigurationManager.AppSettings[colFileName + ".columns"];
IDictionary columns = (IDictionary)ConfigurationManager.GetSection(columnsSectionName);
However, Im geting the <NFeColumns> in a totally different order, and I cant figure out why. Can you please help me?
ConfigurationManager.GetSection returns an instance of HashTable. Items in a hash table won't be stored in the order they are inserted. You will have to manually order them.

Enterprise Pattern and Practise: configure Sql Azure transient fault Configuration

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>

Categories