I have an application residing in a virtual directory. It has the following setting in its Web.config:
<appSettings>
<add key="SomePath" value="C:\Somepath\whatever"/>
........other settings.........
</appSettings>
I have an executable that runs externally to the app that needs to read this value:
System.Configuration.Configuration config =
WebConfigurationManager.OpenWebConfiguration("/MyApplicationVirtualDirectory")
as System.Configuration.Configuration;
string path = config.AppSettings.Settings["SomePath"].Value;
This throws a compilation error:
'System.Configuration.ConfigurationElement.this[System.Configuration.ConfigurationProperty]' is inaccessible due to its protection level
Which I guess makes sense because web.configs in virtual directories contain some sensitive info like connection strings, etc but I wanted to check and make sure that I'm not doing anything wrong and that my understanding is correct. If not, how can I access this setting from my external executable?
This is sort of a round-abound way of solving the problem, but its the way I've done it in the past. You can simply make a page in your Web Application that gives the setting you want back, and then use an HttpWebRequest to access that page in your executable.
Otherwise its a permission issue. You can mess with the permissions on the Web.config file or try running the executable as Administrator.
Related
I have an asp.net project that is using Entity Framework that is used on several different client servers. This means a different web.config for each for connectionstrings and app settings.
Hasn't been an issue but I changed something that altered the web.config file recently and I manually had to adjust this for each client, I also have to exclude the web.config file in updates to ensure their own one is not overwritten.
What I would like to achieve is store these settings in maybe another config file that the project can pick up and use. Maybe that on Globals Application_Start gets these and imports them/overwrites the current web.config file perhaps.
Essentially I don't want to affect the current code that uses the connection string and ConfigurationManager.AppSettings used throughout the project but I do want to be able to let the web.config file update for each client and use a seperate file for some settings.
NOTE: I do not have access to publish directly to each server so I can't simply write a different deploy web.config for each one. I have to publish the files locally, store as zip and automated routine on servers downloads and extracts accordingly.
EDIT:
Please do say if this is considered a bad idea but an idea I had was to put something similar in Global.asax Application_Start method:
Does config file exist?
If no, create file and append all current settings in web.config
If yes, open and import those settings to the current web.config overwriting the original values
Hopefully then in a few weeks time, after I have asked all clients to perform a manual update they will have this code and I can begin to include the web.config in updates.
In VS, inside the Build menu, the last item is Configuration Manager.
In here you can specify various different release environments which can each have their own web.config transforms.
This is normally used for production/staging/test environments. However, I can see no reason why you could not use this and have a configuration file for each of your servers/environments.
You will then need to create the transformations for each environment, by rightclicking the web.config, then selecting Add Config Transform.
each of the environments you had setup can then override the settings in the main web.config. (That now acts as a template/default settings)
e.g.
In Web.EnvironmentA.Config
<add key="ConnectionString" value="ConStringA" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
In Web.EnvironmentB.Config
<add key="ConnectionString" value="ConStringB" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
Then when you do a publish, you simply setup which config file to use. If you use Azure or the VS publish method, it will save each of these settings, so then you can simply push to the relevant environment easily.
Just make sure you test this works as you intent first, before you start publishing everywhere ;)
I have a few XML files in my WCF service that are mapped like
\\servermain\D$\apps\dataservice\bin\Type\FileName.xml
\\servermain\D$\apps\dataservice\bin\Type\Config\ConfigFile.xml
I refer to these files in my program by using
.\\Type\\FileName.xml
.\\Type\\Config\\ConfigFile.xml
When i run my application through the console as a service it works completely fine... when i deploy it to the IIS server i get an error saying the file can't be found...
Here is a bit of text from my result (My WCF service spits out JSON)
"Message":"IO Exception C:\\Windows\\SysWOW64\\inetsrv","Detail":"File does not exist: .\\Type\\Config\\ConfigFile.xml"
I used System.Environment.CurrentDirectory to get the current directory for the error above.
How do i configure it so that my application path is actually
\\servermain\D$\apps\dataservice\bin\
I would prefer if I wouldn't have to hardcode the actual path of the file, because developers would be running this service through their local machines as well.
I would prefer if I wouldn't have to hardcode the actual path of the file, because developers would be running this service through their local machines as well.
That's what you are doing right now. A possible way to resolve this hardcoded paths is pretty easy:
Every WCF service comes with a config file. You can easily add the appSettings element to your config and specify absolute or relative paths to any additional file.
E.g. for executing the console:
<appSettings>
<add key="ConfigFile" value=".\\Type\\Config\\ConfigFile.xml" />
</appSettings>
And instead for the IIS hosted config:
<appSettings>
<add key="ConfigFile" value="\\servermain\D$\apps\dataservice\bin\Type\Config\ConfigFile.xml" />
</appSettings>
You can then access those values via the System.Configuration.ConfigurationSettings class.
UPDATE:
The System.Configuration.ConfigurationSettings is marked as obsolete. Instead, the System.Configuration.ConfigurationManager or System.Configuration.WebConfigurationManager should be used.
I have a C# executable reading and updating it's configuration file (app.exe.config) at runtime, specifically, the "appSettings" section. After I make two changes to two key values and save them using the Configuration.Save() method, an "access to the path" error is thrown, but only on some users' machines (so far 2 reports out of 10,000). We have never seen this issue in-house, only in production.
The Configuration.Save() method seems to be the issue based on our program's log file. When a problem with this call happens a "ConfigurationErrorsException" occurs because the configuration file could not be written to -or- the configuration file has changed. The configuration file is in the same directory as the application. This is a Dot Net 4 application running on a Windows 7 PC.
The code is:
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configuration.AppSettings.Settings["Last Updated Package"].Value = packageVersion;
configuration.AppSettings.Settings["Copy Updated Files"].Value = bool.TrueString;
configuration.Save(ConfigurationSaveMode.Minimal);
ConfigurationManager.RefreshSection("appSettings");
The error is:
An error occurred loading a configuration file: Acces to the path 'C:\Program Files (x86)\My Program\bin\guaixcax.tmp' is denied. (C:\Program Files (x86)\My Program\bin\MyProgram.exe.Config)
Is this a permissions issue, or can I add something to either the code or config file to fix this issue?
I have had similar issues with .NET program installations. It has always been a permissions issue. To test if this is your problem, you should try running your application as an unprivileged user - developers tend to have administrator rights...whenever they can manage it.
If it fails, edit the security of the config file (as an administrator) to allow the "Users" group write access to your "bin" folder, and try it again. If it works, you've identified the problem. Then you can figure out what the best solution is. Some options would be:
Set access rights to necessary files/folders from your installer to work for the users that will run the application
Run the application as a user with admin rights (NOT a good idea from a security best-practices point of view)
Instead of modifying the main app settings file, create a separate settings section referencing a file that has the data that can be changed:
<configuration>
<configSections>
...
<section name="MyChangableSettings" type="My.Namespace, ChangableSettingsClass"/>
</configSection>
...
<MyChangableSettings configSource="Path\To\Writable\StorageDir\mySettings.config"/>
</configuration>
I'm creating a web application, which calls a DLL to run unit tests, I also have another DLL(DataAccessLayer) which performs connections and performs queries to SQL which references the main DLL. Both the DLLs use the same config file to read settings.
When running application from VS, the application is working fine. However when the web app is deployed to IIS, it seems the DLLs are unable to read the settings from the config file.
After some research I found that I might have to explicitly define the configuration elements in the web.config file, however I don't know how to implement this. Can someone please point me in the right direction?
I'm actually retrieving the settings using the ConfigurationManager with the following code:-
public string GetValue(string key)
{
var appConfig = ConfigurationManager.OpenExeConfiguration("path to dll");
strKeyValue = appConfig.AppSettings.Settings[key].Value;
return strKeyValue;
}
Thanks.
Use WebConfigurationManager.AppSettings["HelloWorldKey"]; to read AppSettings from the web.config.
Just set all the appSettings values used by the DLL you mention, directly in the web.config PRIOR to deploying the app. You don't need to modify this at run-time (and you shouldn't anyway, since any modification to the web.config will cause the application to restart)
Add the connectionstring or AppSetting or ApplicationSettings used in you app.config into your web.config, I understand this is a manual task but is the only way that the config will read the settings.
Use following code to access connection string
string filePath= WebConfigurationManager.AppSettings["Pathfile"].ToString();
Web config Fie
<configuration>
....
<appSettings>
<add key="Pathfile" value="Path to dll"/>
</appSettings>
....
</configuration>
I have a web site deployed for several customers.
For security reasons, the deployment is initiated from the target server. The deployment consist in copying the file to dev->prod.
For each target site, I have a specific resource file and at the deployment time I rename the file from xxx.resx to Resource.resx. This works fine but I wonder, if it is possible to define the resource file to use from the web.config?
It is possible.
You can make a variable in web.config in which you can enter the name of the resx to use.
It should require you to access the resource object with GetGlobalResourceObject(string, string) though, assuming the resx file(s) are located in App_GlobalResources.
If you want intellisense, then you'd have to make sure all the .resx files have the exact same keys (cause the keys will be compiled) and then keep renaming. For example in Global.asax's Application_Start event, which is fired whenever the app starts/restarts.
Anyway, have a look at this for info on the web.config variable: http://forums.asp.net/t/1386667.aspx/1