简体   繁体   中英

In .NET, What is the Runtime Equivalent of my app.config Setting?

If I have the following setting in my app.config file. It is a setting I need to make sure my WCF client can negotiate the default proxy server.

<system.net>
    <defaultProxy enabled="true" useDefaultCredentials="true"></defaultProxy>
</system.net>

Unfortunately, I can't add to the app.config file in my environment. How do I ensure these settings by setting them at runtime?

I think what you do is create a System.Net.WebProxy object, then set the appropriate variables, then set the System.Net.WebRequest.DefaultWebProxy :

System.Net.WebProxy proxy = new WebProxy();
proxy.UseDefaultCredentials = true;
WebRequest.DefaultWebProxy = proxy;

This post sort of talks about the whole thing: Link

Hope that helps!

On the properties page for your project there should be a settings tab. Anything you put there actually lives in a *.settings file in the project, but will also be included in the app.config file automatically at deployment. Can you make changes there?

I imagine that you are using a binding that inherits from WSHttpBindingBase. If so, you can also try setting the 'UseDefaultWebProxy' property in code. Something like this:

myWSHttpBinding.UseDefaultWebProxy = True;

Edit: BasicHttpBinding also has the same property.

Whatever the defined name of your executable (not a library dll) assembly is, add a ".config" at the end...

so if your executable is to be

AcmeWidgets.EastCoast.MyApplicationName.exe

Then the app.config will get renamed to

AcmeWidgets.EastCoast.MyApplicationName.exe.config

However, I wouldn't recommend you attempt to dynamically change these settings (in the config file on disk) at runtime...

instead, can you code your app so that it instead populates and uses static variables from these config settings... and then implement the dynamic "change the value" functionality so that it changes these static variables...

This way you can still "affect" the runtime behavior dynamically, but avoid the hassle of writing to the config file, and Ops management can manage the "default" values in the config file by editing it...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM