【问题标题】:Get setting from C# App.config file从 C# App.config 文件获取设置
【发布时间】:2014-10-11 20:19:22
【问题描述】:

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

如何在此 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>

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

编辑:

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

【问题讨论】:

  • @dashsa 我可以这样做,如果我只是想将设置放在一个文件中......仍然会添加两个具有相同设置的位置。目标是能够从一个位置提取我想要的信息,这样我就不必维护两个位置(即使在同一个文件中)。重复设置 = 忘记更新第二个设置。
  • @Gandarez 在玩,我有一个行为元素var config = ...; var group = ...; var behaviors = group.Behaviors.EndpointBehaviors;,这似乎是一个很好的起点。仍然试图找到clientCredentials 的属性,但我觉得答案让我更接近了。
  • 这让我很接近,我仍在寻找......但这个答案并没有让我完全到达那里。我有 SectionGroups,但我似乎无法确定各个属性/属性。我感觉它就在我的鼻子底下。

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    一旦您可以访问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;
        }
    

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 2011-05-12
      • 2012-02-24
      • 1970-01-01
      • 2018-04-07
      • 2010-10-15
      • 2011-10-09
      • 2016-06-21
      • 2019-07-06
      相关资源
      最近更新 更多