config file for ASMX web service - c#

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.

Related

Why do my IIS site-specific application settings disappear?

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

Getting ConnectionString of a BizTalk config file from a C# Application

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.

AppSettings in App or Web Config Using a Linked File

I'm trying to reference some common config settings between a Windows Service and an ASP.NET MVC website. I am doing this by using the file attribute on appSettings in either the App.config or Web.config (respectively). The file (named common.config) that is being referenced is a linked file in a separate project in the same solution. That common.config is set to Content with Copy Always in both projects.
This stack answer to a similiar question seems to suggest at least for configSource this solution would work. I don't want configSource though as I only want a handful of the properties to be common amongst the two projects. Update: I just tried this, and the configSource also doesn't work. It can't find the config file. This leads me to believe the common.config is not treated as content with copy always.
Example App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="common.config">
<add key="NotCommonKey" value="1"/>
</appSettings>
</configuration>
Example Web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings file="common.config">
<add key="NotCommonKey2" value="2" />
</appSettings>
</configuration>
Example common.config (Content -> Copy Always)
<appSettings>
<add key="CommonKey" value="1" />
</appSettings>
I am using ConfigurationManager / WebConfigurationManager reading from the AppSettings property.
Any ideas why when the common.config is a linked file, it's AppSettings values are not used and when it is not linked it works as normal?
Thanks!
In the Web.Config you must add "bin/" (se example below).
By default the web.config is NOT copied into the bin folder but the file common.config is, therefore you must add the path from web.config. In a non-web project the default behavior is that the App.config is copied to the bin folder with name MyProgram.exe.config and is in the same directory as common.config.
<appSettings file="bin/common.config">
The idea of using "bin/..." is good but leads to an error saying that "/" is an invalid character in the resulting virtual path.
The proper solution is tu use "bin...".
Cheers
I use this to access another .exe's config file, not sure whether it will work with a MVC project, but this might get you closer:
string proj2Exe = #"C:\projects\proj2\bin\Debug\proj2.exe";
Configuration proj2Config = ConfigurationManager.OpenExeConfiguration(proj2Exe);
string mysetting = proj2Config .AppSettings.Settings["ThatSetting"].Value;

Hiding private details from open source projects

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.

App.config AppSettings return null

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

Categories