multiple applications single config file - service

I'm trying to write a service and configuration application. VB/C++ 2010 I've had a number of hits on google but they largely seem to be obsolete. What I have so far is a project with a single form app and a service app. The single form app has an "app.config" file and I have added a section:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="settings.config">
</appSettings>
</configuration>
In the Solution I have added a "settings.config" file and its contents is:
<?xml version="1.0" encoding="utf-8"?>
<appSettings>
<add key="Setting1" value="This is Setting 1 from settings.config" />
<add key="Setting2" value="This is Setting 2 from settings.config" />
<add key="ConnectionString" value="ConnectString from settings.confg" />
</appSettings>
I have added a reference to then C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\Profile\Client\System.Configuration.dll
library in both the forms app and the service app
In the very simple forms app i have the following code
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim s As String = _
System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
TextBox1.Text = s
End Sub
It doesn't work! Now clearly I am missing something. Its probably very simple. But my limited understanding is that this is automatically configuered by the config files I have? MS in their usual helful fashion seem to only give samples for 2012 and net 4.5 or greater. I need this to work on a 2003 server (as well) so I'm limited to net 4.0

The problem here is that the line System.Configuration.ConfigurationManager.AppSettings("ConnectionString") is looking for the key ConnectionString in your application's app.config file.
The fact that you have included that file key in your app.config file doesn't magically tell the ConfigurationManager to load the settings from a different file. If that's what you want you will have to read the setting for the file key and then manually load the configuration from that file.
This has not changed since the early versions of .Net though so I'm not sure why you were conflicted by the examples.

Add reference on existing assembly in .Net section of your Add Reference Popup
But i suggest you to use connectionStrings section in your config file
<connectionStrings>
<add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

Related

How to override the keys in windows services .exe.config file through VSTS release definition

I am working on VSTS release task for deploying the Windows Services Project. Unfortunately, we are not creating any Build Definition for creating drop folder.
But, my client will provide drop folder for this project, what I need is “I want to override the keys of an existing .exe.config file” at release level.
For creating the Windows Services Deploy task,I followed this Windows Services Extension
For example my drop folder looks like below:
Many thanks for this reference article and It's a very useful for changing values in config file using Power Shell commands. I have doubt in from that reference link :
For Example, If had a Code like this :
<erecruit.tasks>
<tasks>
<task name="AA" taskName="AA">
<parameters>
<param key="connectionString">Server="XXXX"</param>
</parameters>
</task>
How to change this above connectionstring value?
You can use Tokenizer task in Release Management Utility tasks extension.
Install Release Management Utility tasks extension
Add Tokenizer with XPath/Regular expressions task to release definition (Specify Source filename and Configuration Json filename)
Config file sample:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="TestKey1" value="__Token1__" />
<add key="TestKey2" value="__Token2__" />
<add key="TestKey3" value="__Token3__" />
<add key="TestKey4" value="__Token4__" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
Configuration Json file (Default Environment is the environment name in release definitioin):
{
"Default Environment":{
"CustomVariables":{
"Token2":"value_from_custom2",
"Token3":"value_from_custom3"
},
"ConfigChanges":[
{
"KeyName":"/configuration/appSettings/add[#key='TestKey1']",
"Attribute":"value",
"Value":"value_from_xpath"
}
]
}
}
Then the value of TestKey1 (key) will be related to value_from_xpath and the values of TestKey2 and TestKey3 will be related to value_from_custom2 and value_from_custom3.
On the other hand, you can use release variables directly if you don’t specify Configuration Json filename.
For example, there is __TokenVariable1__ in your config file and TokenVariable1 release/environment variable in release definition, then the __TokenVariable1__ will be replaced through Tokenizer task.
A related article: Using Tokenization (Token Replacement) for Builds/Releases in vNext/TFS 2015
Update:
You also can do it through PowerShell directly.
Update configuration files using PowerShell

Using a separate file to maintain the connection string for entity framework

I have my connection string currently in my web.config file.
Is it possible to place it in a separate file and point entity framework to it.
I found the answer here Separate ConnectionStrings and mailSettings from web.config? Possible?:
<configuration>
<connectionStrings configSource="connections.config"/>
</configuration>
With file connections.config containing
<connectionStrings>
<add name="name" connectionString="conn_string" providerName="System.Data.SqlClient" />
<add name="name2" connectionString="conn_string2" providerName="System.Data.SqlClient" />
</connectionStrings>
In case anyone stumbles upon this question. You can put the connection strings in a separate config file using configSource but DONT expect the EF designer to work happily with it.
Every time to go to edit the edmx and 'Update from Database' it will ask for a new connection string and then always want to save it back to the web.config. Not ideal and for me not workable. This is the case in EF6 and previous.

Using XmlLogFormatter with the RollingFlatFileTraceListener in Enterprise Library

I'm trying to use the RollingFlatFileTraceListener to provide rolling logs in my app along side the XmlLogFormatter so that the logs are in an XML format, however the app no longer seems to be logging anything.
<listeners>
<clear />
<add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
fileName="C:\Inetpub\logs\rolling.log" rollFileExistsBehavior="Increment" header="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" footer="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" formatter="Error Formatter"
rollSizeKB="12499" maxArchivedFiles="3200" traceOutputOptions="None" timeStampPattern="yyyy-MM-dd" rollInterval="Midnight" />
</listeners>
<formatters>
<clear />
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.XmlLogFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" template="Timestamp: {timestamp(local)}
Message: {message}
Category: {category}
Severity: {severity}
Title:{title}
Machine: {machine}
Extended Properties: {dictionary({key} - {value}
)}" name="Error Formatter" />
</formatters>
Since the application isn't hard faulting, I can't see any errors to diagnose, yet I know that I should see some trace logs by now.
Update:
Current progress is that I've been able to use the RollingFlatFileTraceListnerData with the TextFormatter, making use of the template to specify XML. The two items that don't work as of yet are
The file has no XML declaration
The file has no root element, instead it has many root elements
Any thoughts on how to tack that on to the start and end of the file?
The out of the box trace listeners do not support a file header or file footer concept. As you've seen, it basically just appends to the file. Even if you used the .NET Framework System.Diagnostics.XmlWriterTraceListener it only writes XML fragments and not well formed XML documents.
One way to achieve what you want would be to create a separate process that modifies the archived files to be well formed after they have been rolled. The downside of that approach is that the active log file is not well formed.
If that is a concern then you will probably have to create a custom trace listener to do what you want. Instead of simply appending to the log file you could overwrite the XML document's closing tag (e.g. ) with the latest LogEntry and the closing tag.
Another interesting approach from the article Efficient Techniques for Modifying Large XML Files is to create a well formed XML document that includes the document with XML fragments. E.g.
<?xml version="1.0"?>
<!DOCTYPE logfile [
<!ENTITY events
SYSTEM "XMLFile1.xml">
]>
<logfile>
&events;
</logfile>

How do I get the EntLib database settings to stay deleted?

I'm using EntLib 5.0 for logging and exception handling, but not for data access.
Everytime I delete the 'database settings' app block using the entlib designer, it re-adds itself after I rebuild the application. It wouldn't be a problem and I'd just leave it except that it creates LocalSqlServer and LocalMySqlServer instances automatically and throws "LocalMy[DB]Server (Connection String): Connection string is a required value" on the next build.
I've just deleted it before every other build, but that's obviously pretty annoying.
So again, how can I delete it such that it'll stay deleted?
You need to edit your machine.config.
This issue started for me, after I installed Wordpress/MySQL via Web Matrix.
It added this line into my .NET 2.0 machine.config file:
<add name="LocalMySqlServer" connectionString="" />
So all you need to do is open it up in a text editor and comment out that line!
In my case, the file was here:
c:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config
And I commented out two providers, e.g.:
<!-- <add name="OraAspNetConString" connectionString=" " />
<add name="LocalMySqlServer" connectionString="" /> -->

Problems attempting to read web.config from a Windows application

I'm writing a WPF application to help non-XML-savvy customers set up configuration files on a web server. web.config is one of these files. I have custom sections defined, but I've commented them out until I get the basics working.
In web.config, I have this:
<appSettings>
<add key="buffer" value="65536"/>
<add key="updateInterval" value=""/>
</appSettings>
Here's a snippet of how I'm reading web.config (I have already confirmed that web.config exists inside AppPath):
var vdm = new VirtualDirectoryMapping(AppPath, true);
var wcfm = new WebConfigurationFileMap();
wcfm.VirtualDirectories.Add("/", vdm);
WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
However, WebConfigurationManager.AppSettings["buffer"] returns null. Any ideas?
Duhhhh. It's Monday morning. :-)
var config = WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
config.AppSettings["buffer"];
works.