1

I have an app.config file. 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.

How can I get the words "FindMe", "LocalMachine" and "My" in this app.config file (to drive pulling a certificate from the given information)?

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

Edit:

I think I'm real close, but I can't seem to get the values.

enter image description here

WernerCD
  • 2,137
  • 6
  • 31
  • 51
  • 2
    http://stackoverflow.com/questions/14032875/reading-wcf-behavior-elements-from-config-file-programmticlally-when-exposing-se – gandarez Aug 18 '14 at 20:24
  • @dashsa I could do that, if i simply wanted the settings in a single file... that still adds two locations with the same setting. The goal is to be able to pull the information I want from one spot so I don't have to upkeep two spots (even if in the same file). Duplicate settings = forgetting to update the second one. – WernerCD Aug 18 '14 at 20:35
  • 1
    @Gandarez Playing around, I've got a behaviors element `var config = ...; var group = ...; var behaviors = group.Behaviors.EndpointBehaviors;`, which seems to be a good starting point. Trying to find the attributes for the `clientCredentials` still, but I feel that answer has gotten me a little closer. – WernerCD Aug 18 '14 at 21:00
  • This gets me close, and I'm still looking... but that answer doesn't quite get me there. I've got the SectionGroups, but I can't seem to nail down the individual properties/attributes. I have a feeling that it's right under my nose. – WernerCD Aug 19 '14 at 00:26
  • @Gandarez Thanks for the pointer, posted an answer I came too for getting the values from the config. Feel free the poke holes in it. – WernerCD Aug 19 '14 at 01:48

2 Answers2

2

Using Gandarez's comment and Phils answer as a launching board, I was able to poke my way into this solution. 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;
        }
    }
}

enter image description here

WernerCD
  • 2,137
  • 6
  • 31
  • 51
1

Once you have access to the ServiceModelSectionGroup, you can access the various parts of the model. eg Behaviors.EndpointBehaviors collection

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;
    }
phil soady
  • 11,043
  • 5
  • 50
  • 95