Link to home
Start Free TrialLog in
Avatar of teknovation
teknovation

asked on

NULLS for APPCONFIG in Visual Studio 2012 - C#

Can  someone tell me why I keep getting nulls when I reference the app.config file in Visual Studio 2012?

.cs file:
 
string ServiceUrl = ConfigurationManager.AppSettings["webUrl"];
 string userName = ConfigurationManager.AppSettings["UserName"];

Open in new window


App Config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="webUrl" value="https://development.api.crm.dynamics.com/XrmServices/2011/Organization.svc" /> 
    <add key="UserName" value="test"/>
    <add key="Password" value="test123"/> -->
    <add key="UserNameDevice" value="160hmrajfdsg" />
    <add key="PasswordDevice" value="TJ*aahg1z9;W" />
    <add key="Domain" value="Test" />
 
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
  </startup>
  <system.web>
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers> 
    </roleManager> 
  </system.web>
</configuration>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of teknovation
teknovation

ASKER

sort of? The purpose of the app config file is so that any changes made can be done in one place. Is there a reason why it's not working?
Well that partially true. The config file is so that you can alter the running of your application without having to change and recompile code. *I believe* the reason your code is not working is because you have a class library which relies on some app setting, and you have defined that setting within the library's app.config. However, when an application runs, it does not load the library for the library; it only loads the app.config for the main executable. There are ways to load the library's app.config, but I find it simpler to just add the settings to the main executable's app.config.

This assumes, though, that what you are trying to do is what I described above.
Hmmm so how do I make sure that the class library acts the same way when it's loaded in the system (CRM) ? Or in other words, how do I make sure that the class library ALWAYS reads from the app.config file?
Which CRM?
CRM Dynamic.

I read somewhere that the reason why you cannot use the app.config when you have a class library is because it's looking to use whatever the application executing the .dll which in this case is CRM Dynamic. If the application that executes the .dll has an app config, then it'll use that and not the one you define in Visual Studio (which works for app consoles). So hard code is the way to go in this scenario.