locked
Error reading web.config from another assembly/project on the same solution RRS feed

  • Question


  • I'm trying to use the values from web.config on my startup project MyApp.Host on a web api c# 2.0 application, this is my web.config with the Dell values that I want to get:

    <configSections>
      <section name="ProductSettings" type="MyApp.Utils.Configurations.ProductSettings, MyApp.Utils.Configurations"/>
    </configSections>
    <ProductSettings>
      <DellSettings ProductNumber="20001" ProductName="Dell Inspiron" Color="Black" Warranty="2 Years" ></DellSettings>
    </ProductSettings>

    Now on other project, called Utils I have this:

    namespace MyApp.Utils.Configurations
    {
        public class ProductSettings : ConfigurationSection
        {
            [ConfigurationProperty("DellSettings")]
            public DellFeatures DellFeatures
            {
                get
                {
                    return (DellFeatures)this["DellSettings"];
                }
                set
                {
                    value = (DellFeatures)this["DellSettings"];
                }
            }
        }
    }
    
    namespace MyApp.Utils.Configurations
    {
        public class DellFeatures : ConfigurationElement
        {
            [ConfigurationProperty("ProductNumber", DefaultValue = 1000, IsRequired = true)]
            public int ProductNumber
            {
                get
                {
                    return (int)this["ProductNumber"];
                }
                set
                {
                    value = (int)this["ProductNumber"];
                }
            }
        }
    }
    
    namespace MyApp.Utils.RestApiCalls
    {
      public class RestApiDell
      {
    
          public GetProductNumber()
          {
              var productSettings = ConfigurationManager.GetSection("ProductSettings") as MyApp.Utils.Configurations.ProductSettings;
    
              return productSettings.DellFeatures.ProductNumber;
          }
    }

    My class RestApiDell gets an error on the ConfigurationManager.GetSection, it says that can't find the web config file? Do I have to add something else? I'm not specifying the file's path anywhere because I think that by default it will use the .config from the start up application. O how is the appropiate way to read/share the web.config from another project.

    For more references I'm using the fourth approach from this site the code example works fine, but I my case from another assembly not.


    La fuerza es una casualidad, fruto de la debilidad de los demás...

    • Moved by CoolDadTx Wednesday, November 22, 2017 4:36 PM ASP.NET related
    Tuesday, November 21, 2017 3:28 PM

All replies

  • O how is the appropiate way to read/share the web.config from another project.

    You don't is the bottom line. Each Web solution should have its own web.config dedicated to it.

    Anyway, you can ask the questions at the ASP.NET forums.

    http://forums.asp.net/

    Tuesday, November 21, 2017 3:59 PM
  • there's only one web project, and I want to use the same config parameters from a .dll project. . .

    La fuerza es una casualidad, fruto de la debilidad de los demás...

    Tuesday, November 21, 2017 5:49 PM
  • there's only one web project, and I want to use the same config parameters from a .dll project. . .

    La fuerza es una casualidad, fruto de la debilidad de los demás...


    Web application is the root project and the Web.config is the root.config. The DLL project is a subordinate to the root project with its config file info. All config files and their info  in any subordinate projects to the root project must have their config info copied to the root.config so .Net can find it. 
    Tuesday, November 21, 2017 11:21 PM
  • Please post your question in the ASP.NET forums. They can provide suggestions on how to access the config entries from a non-web app project.

    Hint: ConfigurationManager is the type but using an interface with IoC is better.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, November 22, 2017 4:36 PM