[英]Get setting from C# App.config file
I have an app.config file. 我有一个app.config文件。 It's from a sample given to me for an API I have to use... I want to get a setting from the file so that I can use the settings from there and not have to duplicate efforts. 这是从我给我的一个示例中获取的API我必须使用...我想从文件中获取一个设置,以便我可以使用那里的设置而不必重复工作。
How can I get the words "FindMe", "LocalMachine" and "My" in this app.config file (to drive pulling a certificate from the given information)? 如何在此app.config文件中获取“FindMe”,“LocalMachine”和“My”(以驱动从给定信息中提取证书)?
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>...</startup>
<system.serviceModel>
<bindings>...</bindings>
<client>...</client>
<behaviors>
<endpointBehaviors>
<behavior name="ClientCertificateBehavior">
<clientCredentials>
<clientCertificate findValue="FindMe" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName"/>
<serviceCertificate><authentication certificateValidationMode="None"/></serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
I'm looking to see if I can find it in System.ServiceModel.Configuration or ConfigurationManager, but I'm not seeing how to get those specific values. 我想看看我是否可以在System.ServiceModel.Configuration或ConfigurationManager中找到它,但我没有看到如何获取这些特定的值。
Edit: 编辑:
I think I'm real close, but I can't seem to get the values. 我想我真的很亲密,但我似乎无法获得价值观。
Using Gandarez's comment and Phils answer as a launching board, I was able to poke my way into this solution. 使用Gandarez的评论和Phils作为启动板的答案,我能够通过这种方式进入这个解决方案。 It's far from finished, but it'll allow me to get the values and I can fine tune it as needed: 它还远没有完成,但它会让我获得价值,我可以根据需要对其进行微调:
using System.Configuration;
using System.ServiceModel.Configuration;
using config = System.Configuration.Configuration;
namespace Client
{
public class Program
{
private static void Main(string[] args)
{
config Config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ServiceModelSectionGroup Group = ServiceModelSectionGroup.GetSectionGroup(Config);
BehaviorsSection Behaviors = Group.Behaviors;
EndpointBehaviorElementCollection EndpointBehaviors = Behaviors.EndpointBehaviors;
EndpointBehaviorElement EndpointBehavior = EndpointBehaviors[0];
ClientCredentialsElement ClientCredential = (ClientCredentialsElement) EndpointBehavior[0];
var ClientCertificate = ClientCredential.ClientCertificate;
var findValue = ClientCertificate.FindValue;
var storeName = ClientCertificate.StoreName;
var storeLocation = ClientCertificate.StoreLocation;
var X509FindType = ClientCertificate.X509FindType;
}
}
}
Once you have access to the ServiceModelSectionGroup
, you can access the various parts of the model. 一旦访问ServiceModelSectionGroup
,就可以访问模型的各个部分。 eg Behaviors.EndpointBehaviors
collection 例如Behaviors.EndpointBehaviors
集合
public ServiceModelSectionGroup GetServiceModelSectionGroup() {
var cfg = GetConfig();
ServiceModelSectionGroup serviceModelSection = ServiceModelSectionGroup.GetSectionGroup(cfg);
return serviceModelSection;
}
public Configuration GetConfig() {
if (_cfg == null) {
if (HostingEnvironment.IsHosted) // running inside asp.net ?
{ //yes so read web.config at hosting virtual path level
_cfg = WebConfigurationManager.OpenWebConfiguration(HostingEnvironment.ApplicationVirtualPath);
}
else { //no, se we are testing or running exe version admin tool for example, look for an APP.CONFIG file
//var x = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
_cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
}
}
return _cfg;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.