Pass XML data as parameter in a function call - c#

I'm copying the MyApp.exe.config file to a separate location as an xml.
I'm using this custom config file to access and changes some settings when the application is running, rather than using the ConfigurationManager.<section> to modify the app.config.
The problem is, the default app.config file might be updated at later stage on the user machine and I want to propagate these changes back to my custom config file.
What is the best way to do this?
<configSections>
<section name="" type="" />
</configSections>
<appSettings>
<add key="" value="" />
</appSettings>
<ConnectionManangerProviders>
<provider name="" enabled="" type="">
<pollingInterval></pollingInterval>
</provider>
</ConnectionManagerProviders>
<QueueManagerProviders>
<provider name="" enabled="" type="unique" encrypted="" signed="" cryptoProvider="" />
<provider name="" enabled="" type="unique" encrypted="" signed="" cryptoProvider="">
<queueName></queueName>
</provider>
<provider name="" enabled="" type="unique" encrypted="" signed="" cryptoProvider="">
<applicationName></applicationName>
<connectionString></connectionString>
</provider>
</QueueManagerProviders>
For example, this is how the configuration file looks. Let's say one of the values of the element under <QueueManagerProviders> is modified in the app.config, how can I pass the data in this file to a method so that the method can effectively parse through it and update the appropriate section in the custom config file that I created?

This screams something about bad design, there are probably better ways to implement the whole functionality. But, if you are not in control of the "Services" part then you are stuck...
You probably need FileSystemWatcher which "Listens to the file system change notifications and raises events when a directory, or file in a directory, changes."
Add an event handler and, if your file changes, read the whole file again and parse it.

Implement a custom ConfigSection Handler (see http://msdn.microsoft.com/en-us/library/2tw134k3(v=vs.100).aspx).

Related

How to set a parameter value based on the app settings key value inside a web.config

I have the following code inside a web.config and I'd like some way of setting the Parameter "NoHeader" to the value of the app settings key "ShowHeader" such that the former would be set to true. The best guess I have is that I use code to read the value from the ConfigurationManager and somehow write it back to NoHeader but I don't know how to do that if that's the case.
<Telerik.Reporting>
<Extensions>
<Render>
<Extension name="CSV">
<Parameters>
<Parameter name="NoHeader" value="false" />
</Parameters>
</Extension>
</Render>
</Extensions>
</Telerik.Reporting>
<appSettings>
<add key="ShowHeader" value="true" />
</appSettings>
We use a method that changes the showHeader key on deployment, but I'm not able to modify the NoHeader portion directly. I'm using C# for my code, any clues would be appreciated.

Umbraco Examine indexes disappearing on Azure

I've been having an issue on an Umbraco 7.5.6 site hosted in an App Service on Azure where the indexes seem to be dropped after an unspecific amount of time.
We're storing information, including some custom fields, on published news articles in the External Examine index to query stories from the index. This is consumed by our client-facing search API.
Initially, we thought that this might be caused by Azure swapping servers so removed the {computerName} parameter from the path under ExamineSettings.config. However, that didn't appear to have any effect.
Our current index path is ~/App_Data/TEMP/ExamineIndexes/External/
The ExamineSettings.config file is as follows:
<Examine>
<ExamineIndexProviders>
<providers>
<add name="InternalIndexer" type="UmbracoExamine.UmbracoContentIndexer, UmbracoExamine"
supportUnpublished="true"
supportProtected="true"
analyzer="Lucene.Net.Analysis.WhitespaceAnalyzer, Lucene.Net"/>
<add name="InternalMemberIndexer" type="UmbracoExamine.UmbracoMemberIndexer, UmbracoExamine"
supportUnpublished="true"
supportProtected="true"
analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net"/>
<!-- default external indexer, which excludes protected and unpublished pages-->
<add name="ExternalIndexer" type="UmbracoExamine.UmbracoContentIndexer, UmbracoExamine"/>
</providers>
</ExamineIndexProviders>
<ExamineSearchProviders defaultProvider="ExternalSearcher">
<providers>
<add name="InternalSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine"
analyzer="Lucene.Net.Analysis.WhitespaceAnalyzer, Lucene.Net"/>
<add name="ExternalSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine" />
<add name="InternalMemberSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine"
analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net" enableLeadingWildcard="true"/>
</providers>
</ExamineSearchProviders>
</Examine>
Due to the unpredictable nature of this issue, short of writing a WebJob to republish the articles on a regular basis, I'm unsure of what to try next.
First thing to do is update your examine config
The filesystem attached to web apps is actually a UNC share which can suffer from IO latency issues which in turn can cause Umbraco to flip out a little bit.
Try updating your ExamineSettings.config as per the following and add this to the indexer(s):
directoryFactory="Examine.LuceneEngine.Directories.SyncTempEnvDirectoryFactory,Examine"
The SyncTempEnvDirectoryFactory enables Examine to sync indexes
between the remote file system and the local environment temporary
storage directory, the indexes will be accessed from the temporary
storage directory. This setting is required due to the nature of
Lucene files and IO latency on Azure Web Apps.
This should take performance issues out of the equation.
Then, debugging
Indexing issues should be picked up in Umbraco's logs (some at Info level, some at Debug). If you're not already capturing Umbraco's logs then use something like Papertrail or Application Insights to collect the logs and see if you can identify what's causing the deletion (you may need to drop logging level to Debug to catch it).
N.B if you do push logs to an external service then wrap it in the Async/Parallel provider from Umbraco Core: here's an example config.
<log4net>
<root>
<priority value="Info"/>
<appender-ref ref="AsynchronousLog4NetAppender" />
</root>
<appender name="AsynchronousLog4NetAppender" type="Umbraco.Core.Logging.ParallelForwardingAppender,Umbraco.Core">
<appender-ref ref="PapertrailRemoteSyslogAppender"/>
</appender>
<appender name="PapertrailRemoteSyslogAppender" type="log4net.Appender.RemoteSyslogAppender">
<facility value="Local6" />
<identity value="%date{yyyy-MM-ddTHH:mm:ss.ffffffzzz} your-site-name %P{log4net:HostName}" />
<layout type="log4net.Layout.PatternLayout" value="%level - %message%newline" />
<remoteAddress value="logsN.papertrailapp.com" />
<remotePort value="XXXXX" />
</appender>
<!--Here you can change the way logging works for certain namespaces -->
<logger name="NHibernate">
<level value="WARN" />
</logger>
</log4net>

Update/Refresh config file

I have a small programm in which i copy paste stuff from A to B on the PC. The directory paths are written in the config, and when i (in the application) change the directory in the textbox it is updating the config file. I checked it, the value is immediatlely rewritten at the appropriate key. When i close the app and reopen it, it is updatet to the previously changed directory path, but i dont want to have to close the application and reopen is. I have a combobox and i want it to update as soon as the combobox reselect event triggers. But during the runtime (altough it is already changed in the config) it will not update the directory path shown in the app.
I read through and tried everything i found online and sadly nothing helped. Not every every kind of
ConfigurationManager.RefreshSection("appSettings");
THis is my config:
<appSettings file="">
<clear />
<add key="SourcepathClient" value="D:\xxx" />
<add key="SourcepathWin32" value="D:\xxx" />
<add key="DestinationpathUpdatePackages" value="D:\xxx" />
<add key="DestinationpathClient" value="D:\xxx" />
<add key="5_9_0-DestinationpathClient" value="D:\xxxt" />
<add key="5_9_0-DestinationpathUpdatePackages" value="D:\xxx" />
<add key="5_9_1-DestinationpathClient" value="D:\xxx" />
<add key="5_9_1-DestinationpathUpdatePackages" value="D:xxx" />
<add key="5_9_2-DestinationpathClient" value="D:\xxx" />
<add key="5_9_2-DestinationpathUpdatePackages" value="D:\xxx" />
</appSettings>
and this the code:
Configuration config = ConfigurationManager.OpenExeConfiguration(System.IO.Path.Combine(Directory.GetCurrentDirectory(), "UpdatePackager.exe"));
config.AppSettings.Settings[ComboBoxVersion.Text + "-DestinationpathClient"].Value = TextBoxDestinationpathClient.Text;
config.AppSettings.Settings[ComboBoxVersion.Text + "-DestinationpathUpdatePackages"].Value = TextBoxDestinationpathUpdatePackage.Text;
config.AppSettings.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
ConfigurationManager.RefreshSection("appSettings");
I hope someone can help me.
Regards
i think there is no issue with code it issue related your access, it makes a difference if you run your application in IIS and run your test sample from Visual Studio. The ASP.NET process identity is the IIS account, ASPNET or NETWORK SERVICES (depending on IIS version).
Might need to grant ASPNET or NETWORK SERVICES Modify access on the folder where web.config resides.

Extend an element in App.config

We have an app.config we are using with Carbonator:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="carbonator" type="Crypton.Carbonator.Config.CarbonatorSection, Crypton.Carbonator"/>
</configSections>
<carbonator defaultCulture="en-US" logLevel="1" collectionInterval="1000" reportingInterval="1000" >
<statsd server="127.0.0.1" port="8125" />
<counters>
<add path="processor_information.pct_processor_time.total" category="Processor" counter="% Processor Time" instance="_Total" />
<add path="memory.available_MBytes" category="Memory" counter="Available MBytes" instance="" />
<add path="memory.pct_commited_bytes_in_use" category="Memory" counter="% Committed Bytes In Use" instance="" />
</counters>
</carbonator>
</configuration>
We want to allow users to configure their own custom counters in an external config file that we reference from the <counters> element. For example, we would like to allow the user config file to look like:
<add path="logical_disk.pct_free_space.C" category="LogicalDisk" counter="% Free Space" instance="C:" />
<add path="logical_disk.disk_read_bytes_per_sec.C" category="LogicalDisk" counter="Disk Read Bytes/sec" instance="C:" />
<add path="logical_disk.disk_write_bytes_per_sec.C" category="LogicalDisk" counter="Disk Write Bytes/sec" instance="C:" />
I don't even know if this is possible outside of an appConfig element, but any help is appreciated.
According to this answer it should be possible. Same way is also described in this article.
But I don't think it's a good idea for one reason - if a user makes a mistake in his configuration extension, it will prevent the application from executing since the application configuration became invalid.
I would rather use the configuration in the app.config file to provide default values and implement some user configuration myself. Is such case, you can use whatever configuration format you like, for example JSON, which would be also better (easier to create and edit) for users. In your application, you simply merge both configurations (app.config values are default values which will be overwritten by the user's configuration).

Merge .net configuration files

My application has 2 config files at the moment.
App.config
Custom.config
The App.config references the Custom.config via a custom section handler like below:
<configuration>
<configSections>
<section name="Custom" type="MyApp.CustomConfigSection, MyApp" />
</configSections>
<Custom configSource="Custom.config" />
</configuration>
The problem is the Custom.config file contains a single section with many elements. I'd like to split this out so some of the elements are in one file and some are in another.
e.g. Right now I have
'<Custom>
<Group1 ../>
<Group2 ../>
<Group3 ../>
</Custom>
I'd like to make 2 or more files like below.
'<Custom>
<Group1 ../>
</Custom>'
'<Custom>
<Group2 ../>
<Group3 ../>
</Custom>'
At the moment I call ConfigurationManager.GetSection("Custom") as MyCustomConfigSection. I then have a number of classes with various configuration attributes that are populated automatically by the .net configuration.
Is there a way to do this split or can you not split 1 section into multiple files?
Thanks in advance

Categories

Resources