从C#App.config文件获取设置

时间:2014-08-18 20:22:39

标签: c# wcf app-config wcf-binding service-model

我有一个app.config文件。它来自我给我的一个样本,我必须使用...我希望从文件中获取一个设置,以便我可以使用那里的设置,而不必重复工作。

我怎样才能得到" FindMe"," LocalMachine"和"我的"在这个app.config文件中(驱动从给定信息中提取证书)?

<?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>

我正在查看是否可以在System.ServiceModel.Configuration或ConfigurationManager中找到它,但我没有看到如何获取这些特定值。

编辑:

我认为我真的很接近,但我似乎无法获得价值观。

enter image description here

2 个答案:

答案 0 :(得分:2)

使用Gandarez的评论和Phils的答案作为启动板,我能够捅到这个解决方案。它还远未完成,但它允许我获取值,我可以根据需要对其进行微调:

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;
        }
    }
}

enter image description here

答案 1 :(得分:1)

一旦您有权访问ServiceModelSectionGroup, 您可以访问模型的各个部分。 例如Behaviors.EndpointBehaviors集合

WCF section info

 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;
    }