简体   繁体   English

在.NET中,我的app.config设置的运行时等效是什么?

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

If I have the following setting in my app.config file. 如果我的app.config文件中有以下设置。 It is a setting I need to make sure my WCF client can negotiate the default proxy server. 这是我需要确保我的WCF客户端可以协商默认代理服务器的设置。

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

Unfortunately, I can't add to the app.config file in my environment. 不幸的是,我无法在我的环境中添加到app.config文件。 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对象,然后设置适当的变量,然后设置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. 你放在那里的任何内容实际上都存在于项目中的* .settings文件中,但也会在部署时自动包含在app.config文件中。 Can you make changes there? 你能在那里做出改变吗?

I imagine that you are using a binding that inherits from WSHttpBindingBase. 我想你正在使用一个从WSHttpBindingBase继承的绑定。 If so, you can also try setting the 'UseDefaultWebProxy' property in code. 如果是这样,您还可以尝试在代码中设置“UseDefaultWebProxy”属性。 Something like this: 像这样的东西:

myWSHttpBinding.UseDefaultWebProxy = True;

Edit: BasicHttpBinding also has the same property. 编辑: BasicHttpBinding也具有相同的属性。

Whatever the defined name of your executable (not a library dll) assembly is, add a ".config" at the end... 无论您的可执行文件(而不是库DLL)程序集的定义名称是什么,最后添加一个“.config”...

so if your executable is to be 所以如果你的可执行文件是

AcmeWidgets.EastCoast.MyApplicationName.exe AcmeWidgets.EastCoast.MyApplicationName.exe

Then the app.config will get renamed to 然后app.config将重命名为

AcmeWidgets.EastCoast.MyApplicationName.exe.config 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... 这样你仍然可以动态地“影响”运行时行为,但是避免了编写配置文件的麻烦,并且Ops管理可以通过编辑来管理配置文件中的“默认”值...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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