The current configuration system does not support user-scoped settings - asp.net

I'm trying to use a settings file to store the user preferences when he/she logins on the application.
I defined them as user (scope) but I am getting
System.Configuration.ConfigurationErrorsException: The current configuration system does not support user-scoped settings.
What may be a good solution?

When I had this problem, it turned out that I had a reference to a dll which had a Settings.settings (or Settings.Designer.cs) file.
What happens is that when editing the Setting.settings file, upon clicking the blank line at the bottom, a new line is added with template information and a default user setting instead of application setting. This is a nice feature but you could see how after changing the template and adding your new setting, then clicking below to lose focus a new template line is added and if you are not paying attention, you accidently add a user setting. Check if you have this file in a referenced dll and remove any user settings.

User-scoped settings are indeed not supported for a Web application. And they wouldn't work, User settings would have to be saved under the Users\<username>\... folder on the server.
You have a wide choice of web techniques:
persistent cookies
ASP.NET Membership profiles
your own Db

You can make Application scope settings writable by simply adding a setter to the property definition in Settings.Designer.cs. For instance:
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("AdminContext")]
public string DbContext
{
get { return ((string)(this["DbContext"])); }
set { this["DbContext"] = value; } }
The caveat is that the Settings.Designer.cs is auto-generated, and therefore if you use the designer UI, your setters will be overwritten.
This works in console and web applications.

Related

Reset application settings to a clear/clean state

I use QML Settings in my application. Therefore, while developing and testing, every time I open the app, it remembers the settings.
How can I clear the settings status so that I can simulate the very first launch of the app by a user? Thanks.
You can specify the file that is used to store the Settings by setting the filename property.
Settings {
filename: "/path/to/my/settings/file"
}
Then when you want to clear the Settings, simply delete the file. Everything should start over with default values after that.

How to generate machineKey in web.config using wix?

I need to generate machineKey in my web application installer and put it into the application's web.config. How can I do this?
We need to split the answer in two:
Generate the Machine Key: You will need to implement a custom action to generate the Machine Key (There are many tutorials about creating custom actions so I won't cover that here, review the links below). The important part is the code to generate the Key, review these links: C#, Powershell. You can store the result on an installer property, you may need to make it a secure property to avoid it to apear on the installer logs.
Add the value to the Web.config: Now that you have the key, you can use some of the wix custom actions to modify the web.config, you can use XmlConfig or XmlFile. With this you will be able to modify the Xml file to add the machineKey node using the property created on the previous step. Review the links below for reference on how to use these to update the configuration file.
IMPORTANT: The machineKey element is only valid in the Web.config file at the root of your application and is not valid at the subfolder level.
Additional links:
Adding a Custom Action
Editing Web.Config Connection string settings with Wix
Custom actions with C#
How to pass parameters to the custom action?

ASP.NET Configurable Web Service

I have a web service reference in a ASP.NET 2.0 web site project. I'd like to be able to switch between staging and production versions of the service without having to change my code.
However, I'm not seeing how to do that in a clean way. I know that I can change web.config to point to some other service URL, but then isn't my code still hard-coded to one reference or the other?
I saw this post, but how do you edit the proxy class? If I "Go to Definition" on the class, it doesn't take me to any class that I can edit, but it's the object browser... Do I need to run the wsdl.exe utility so that it will generate a proxy class for me that I can then edit?
Edit #1: Here's the code I'm using to instantiate and call the service:
Dim service As New Swan.MagellanLeadSheetService()
Dim response As Swan.MagellanLeadSheetResponse = service.Foo(stuffToSendToService)
Edit #2: Since the web.config already has the URL endpoint address in the appSettings area, can I just simply edit that setting when we deploy to Production to point to the production URL? Is it that simple? I was worried about the potential for breaking changes between the proxy classes of Staging vs. Production, but those should be resolved prior to deploying any changes to Production I think.
The following article explains how you can make the Web Service Reference dynamic by changing the reference properties, adding a key to the web.config file and referencing this key on the application code:
Article Link
Basically you will have 2 versions of web.config file, production and staging with different URLs defined. While the code will point to a unique location.
UPDATE:
Now, before the following line, you have to alter the service.URL according to what stands in the web.config.
Dim response As Swan.MagellanLeadSheetResponse = service.Foo(stuffToSendToService)
First of all here is some info how to add a web reference to your project:
How to: Add and Remove Web References
Then instance of your webservice class (the proxy class that derives from SoapHttpClientProtocol) has to have an property called url, with that property you can swith at runtime to your asmx from staging or production.
CountryService service = new CountryService();
service.Url = "http://foo/bar.asmx";
More info: WebClientProtocol.Url Property

custom config file in asp.net

In my project I need store complex application settings and i dont want store it in db.
Application settings are available through administration ui to edit/change etc.
So, if i store settings in config, every time when configugration is changed, application is restart.
So second idea is loading external file from file (for example "AppSettings.conf") stored in project.
Question is pretty simple : Is possible load and save setting from external file without restarting application?
Thanks
yes, store the settings in an XML file, and you can read/write to/from an XML file just fine. But, you can't point any of the existing components stored in the web.config (such as the <authentication> or <authorization> elements) to that XML file... that won't work. Only your custom settings would.
HTH.
Finally decided to save/load application settings in db.
Rather than creating a custom config file, create a custom configuration section for the web.config:
Here's just an example:
public class SomeConfigurationSection: ConfigurationSection
{
[ConfigurationProperty("configurationData")]
public string ConfigurationData
{
get
{
return this["configurationData"] as string;
}
set
{
this["configurationData"] = value;
}
}
[ConfigurationProperty("otherConfigurationData")]
public int OtherConfigurationData
{
get
{
return Convert.ToInt32(this["otherConfigurationData"]);
}
set
{
this["otherConfigurationData"] = value;
}
}
}
EDIT
Another possible solution would be to use the Settings.setttings file under the Properties folder. I believe you can add, edit, and delete settings from here without an application restart:
//add a setting
Properties.Settings.Default.Context.Add("foo", "bar");
//edit a setting
Properties.Settings.Default.Context["foo"] = "bar";
//remove a setting
Properties.Settings.Default.Context.Remove("foo");
You can access this file under the Properties folder, or in the properties window of the web application (Properties > Settings).

storing settings in asp.net application

there are few variables in my application which i need to set and are used at various places.
Which is the best place other than web.config file to store them.
Write a class with static members that are set on Application_Start() inside global.asax
If these variables can change their values after application has been built then web.config is the best place to store them. Other options can be database, registry or in short, any persistent medium. Of course, to use these values through-out your application, you should have a wrapper static class that will expose these settings as properties. Settings will be read once at application start-up (or on demand whenever first requested.)

Resources