I have a project that is a WCF client using netTCP endpoints. The project compiles into a DLL that is referenced by another project. I use AppSettings to switch between local and remote ip endpoints like so:
public EmbeddedClient()
{
//Grab ip to use: remote or local (used for simulator)
String location = ConfigurationSettings.AppSettings["ipAddress"];
String ip = ConfigurationSettings.AppSettings[location];
//Default to localhost if no appsetting was found
if (ip == null)
ip = "localhost";
String address = String.Format("net.tcp://{0}:9292/EmbeddedService", ip);
//Setup the channel to the service...
channelFactory = new ChannelFactory<IEmbeddedService>(binding, new EndpointAddress(address));
}
My App.Config is where I have my AppSettings and WCF endpoints:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ipAddress" value="local"/>
<!-- Replace above value to "local" (Simulator) or "remote" (Harware)-->
<add key="local" value="localhost"/>
<add key="remote" value="192.168.100.42"/>
</appSettings>
<system.serviceModel>
<!--WCF Endpoints go here--->
</system.serviceModel>
</configuration>
When I compile the project the appsetting always returns a null. I also noticed that app.config is renamed to something like Embedded_DCC_Client.dll.config after compiling. Why is it not able to find my appsettings? Why is it returning null? Thanks.
It sounds like you're trying to use a config file with the DLL - that won't work. You need to set your app settings and WCF-specific settings in the app file of the application that references the WCF DLL. Th DLL will use the config file of the calling application.
In other words:
MyWCF.dll - this is your WCF DLL.
MyApplication.exe - this is an application that references WCF.DLL.
You would put your app settings and system.serviceModel settings in the app.config file of MyApplication.exe. MyWCF.DLL should then read the values from that config.
The app settings file is loaded from the context of the application that is started, so it needs to either be in that project or referenced from the startup project.
The folder which is used to Install utility should contain the Exe file, supporting dll and exe.config file
Related
I'm hosting an ASP.NET C# website using IIS on Windows Server. I publish to the IIS server using Web Deploy. I have a few application settings that I configure via IIS' site-specific application settings - mostly passwords that I don't want in plain-text in my app.config within the project itself. Every time that I publish, the settings that I've created via IIS disappear. I think the publish is over writing them. I can't figure out how to get them to persist through publishes. Does anybody know how to get the IIS application settings to remain even through Web Deploy publishes?
Not sure if that solves your problem, but you could try something like this:
Every config section has an optional attribute named "configSource" to
point to an external file. Therefore you could break down your web.config
into several files and update them accoringly:
web.config:
...
<configuration>
<appSettings/>
<connectionStrings configSource="connectionStrings.config"/>
<system.web>
...
connectionStrings.config:
<connectionStrings>
<add name="defaultConnectionString" connectionString="..." providerName="System.Data.SqlClient" />
</connectionStrings>
Then you could set the connectionString.config to not be included in the output of the project and just keep it unchanged on the server.
You could also:
-create a new build configuration for deployment
-add a web.config transformation for this build configuration
-deploy using this build configuration
That's how I usually do it
I have a BizTalk config file that I want to use in my C# application. I'd like to get the connection string from the BizTalk config. Is there a way to do this? Simply put, I want to read a connection string from an external config file.
What I'm currently using in my C# app config is:
<configuration>
<appSettings>
<add key="foo" value="blah;"/>
<add key="foo" value="blah;"/>
</appSettings>
</configuration>
I get the keys by using this code:
connectionString = ConfigurationManager.AppSettings[configKey];
Thanks.
From an external .config file, as in not the current .exe's config file, no.
System.Configuration will always refer to the local .config.
To access another .exe's .config file, you have to treat it as just an Xml file with System.Xml.
So I have made a web service in Visual Studio 2010. To deploy it onto the IIS web server, I copy the service.asmx, web.config, and the bin over to the server (wwwroot folder). This all works fine.
My problem is reading even a simple string from web.config. My code is:
In a method, I have:
string from = System.Configuration.ConfigurationManager.AppSettings["folder_new"];
In the web.config file, I have:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="folder_new" value="C:\images\new" />
</appSettings>
<...other stuff etc...>
</configuration>
I read in from the location "from". If I change it to
string from = #"C:\images\new";
it works perfectly.
This is driving me crazy.
You should be using the WebConfigurationManager class instead of the ConfigurationManager. The interface on it is basically the same.
string notFrom =
System.Web.Configuration.WebConfigurationManager.AppSettings["folder_new"];
have you tried double \'s in the config? Like "c:\\images\\new"
ConfigurationManager is for dealing with app config files for assemblies, rather than for Web.config files. Please use WebConfigurationManager instead.
I have a .net github project that is basically a wrapper around a web API. In the test project, I am calling to the API using an API key. I need to keep this key private, how do I accomplish this in a visual studio project?
In some other projects, like python, I can have git ignore the file (config.py) and use something like config.example.py. But in visual studio's case, the project will not compile because of the missing file Config.cs. What is the proper way to solve this? I'm thinking of using this same method of ignoring the file and have them execute a build script that should rename Config.example.cs to Config.cs?
This is the perfect for .config files. Depending on whether its a web or console application, you will have a web.config or app.config file in your project.
You can use the appSettings section to store your API key.
To make things even easier, you can actually have this section read from another file, ie: specialappsettings.config and then just ignore that single file from your repository.
Modify your web.config (or app.config):
<configuration>
<appSettings file="specialappsettings.config">
</appSettings>
<system.web>
<!-- standard web settings go here -->
</system.web>
</configuration>
Create a new specialappsettings.config file:
<appSettings>
<add key="APIKey" value="YourApiKeyValue" />
<add key="AnotherKey" value="AnotherValue" />
</appSettings>
This can be accessed in your code via:
var apiKey = ConfigurationManager.AppSettings["APIKey"];
Notes:
You can keep your settings within the original web.config file as
well but this lets you ignore just the specific settings file from
your git repository without affecting the rest of the project's
necessary configuration details.
The same "key" can be saved in
either file however the external file will override the original
web.config file value.
You are probably looking for the App.config file for a project. It will be copied to <application>.exe.config when you compile it. Users can edit that config file as needed.
In that config file, you can add your API keys:
<configuration>
<appSettings>
<add key="APIKey" value="12345"/>
</appSettings>
</configuration>
Then you can access it from your code using ConfigurationManager.AppSettings:
string apiKey = ConfigurationManager.AppSettings["APIKey"];
One option is to use .config files instead of having secret keys hardcoded in sources.
More info Using Settings in C# and step-by-step guide
<configuration>
<appSettings>
<add key="SecretKey" value="0" />
</appSettings>
</configuration>
var secretKey = ConfigurationManager.AppSettings.Get("SecretKey");
Perhaps you can store the key outside of the Config.cs file and load it at run time.
Bonus, other people using your code won't have to recompile the project to change to their API key.
I have a solution containing 1 console application and 2 libraries.
In the libraries I have two different app.configs for an example my data.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="OutputFileFolder" value="c:\\log" />
<add key="OutputIndexFile" value="c:\\log\index.xml" />
</appSettings>
</configuration>
And in this library class I have in the constructor
_indexPath = ConfigurationManager.AppSettings["OutputIndexFile"];
But how should I load the Data.config file from my main console application (this should be the main config file)?
Config files in your dll projects are not relevant at runtime. The config file (if any) in your console application project is the one that will get used.
If you want to use a configuration file in two separate projects, you can add it as a link to your second project, or you could use a post-build event to copy it over. However both of these seem a little hacky.
Libraries don't really have associated configuration files as such - they operate under an executable (a console application, in your case).
You should put all the configuration in the app.config file of the application for the code in the libraries to have access to it.
You can load multiple config files by have multiple Configuration instances from multiple calls to ConfigurationManager.OpenMappedExeConfiguration (add your config file to the file map, the global file will be added automatically and specify a ConfigurationUserLevel.None.
Something like:
var fileMap = new ExeConfigurationFileMap {
ExeConfigFilename = Path of dll's config file
};
var cfg = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
var result = cfg.AppSettings["OutputIndexFile"];