locked
ToolWindow: Problem in reading App.config of a dll. RRS feed

  • Question

  • Hi,

    I have a problem in Reading ConncectionString Section of App.Config of my dll.

    I have gone through all the discussions related to "Config files for dlls" one here and gone for it as it is neccessary for my requirement.

    My config file is as shown below

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <connectionStrings>
        <add name="MyConn1" connectionString="ConncectionString1" providerName="System.Data.SqlClient"/>
        <add name="MyConn2" connectionString="ConncectionString2" providerName="System.Data.SqlClient"/>
          </connectionStrings>
    </configuration>

    and i am reading connection strings using following code (This code is part of a class in same dll).

    var appConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location);
    string con=appConfig.ConnectionStrings.ConnectionStrings["MyConn1"].ToString();

    It is working fine when i am testing this dll by adding it to an executable project. But it (appConfig.ConnectionStrings.ConnectionStrings["MyConn1"].ToString()) is giving null reference exception when it is  hosted to a Toolwindow (Menu command initiates this tool window).

    Debugging info:

    Extension location: .....\AppData\Local\Microsoft\VisualStudio\11.0Exp\Extensions\MyCompany\MyCommand\1.0

    appConfig object (in above sample code): .....\AppData\Local\Microsoft\VisualStudio\11.0Exp\Extensions\MyCompany\MyCommand\1.0\Mydll.Config




    Thursday, November 21, 2013 2:13 PM

Answers

  • Hi Jane,

    Since ConnectionStrings returns null only after the app.config is hosted to a Toolwindow, please check if you have dealt with them in the proper way.

    you are right.

    The problem is due to the abscense of Mydll.dll.Config in the location

    ...AppData\Local\Microsoft\VisualStudio\11.0Exp\Extensions\MyCompany\MyCommand\1.0\.

    I have added my dll as a reference to MenuCommand project and forgot to add app.config.

    I might have found (during debugging) config file was missed in the location if 'appConfig' would return null in the below code

    var appConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location);

    but fact is that 'appConfig' object wouldnt return null even if there is no presence of file in the required location. I found 'appConfig.HasFile'  as 'False' during debugging and found that file was missed in the location.

    Thank you for your interest in solving my issue.




    Tuesday, November 26, 2013 11:41 AM

All replies

  • Hi,

    Please check if you have added the System.Configuration dll as reference.

    Here are some related links below:

    How to get Connection String from App.Config in C#

    Get connection string from App.config

    Best Regards,

    Jane.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 22, 2013 8:07 AM
  • Hi Jane,

    Thanks for your reply. My assembly already has this Reference and thats why It is working fine when i am testing this dll by adding it to an executable project. (that reference is there in both dll and executable proj).

    When debugging the menucommand,

    var appConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location);

    'appConfig' has the 'Configuration object' and its location as "\AppData\Local\Microsoft\VisualStudio\11.0Exp\Extensions\MyCompany\MyCommand\1.0\Mydll.dll.Config".

    Problem is that this configuration object is not returning "MyConn1" connectionstring.


    Friday, November 22, 2013 9:16 AM
  • Hi,

    Thank you for your feedback.

    Is there a possibility that this issue has some relevance to the .NET Framework version?

    That is just a guess.

    Please check the discussions in the following topics:

    Get connection string from app.config file

    C# DLL config file

    Since ConnectionStrings returns null only after the app.config is hosted to a Toolwindow, please check if you have dealt with them in the proper way.

    You could share more details about the .config file here so that we could have a better understanding of this issue.

    Best Regards,

    Jane.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 26, 2013 9:18 AM
  • Hi Jane,

    Since ConnectionStrings returns null only after the app.config is hosted to a Toolwindow, please check if you have dealt with them in the proper way.

    you are right.

    The problem is due to the abscense of Mydll.dll.Config in the location

    ...AppData\Local\Microsoft\VisualStudio\11.0Exp\Extensions\MyCompany\MyCommand\1.0\.

    I have added my dll as a reference to MenuCommand project and forgot to add app.config.

    I might have found (during debugging) config file was missed in the location if 'appConfig' would return null in the below code

    var appConfig = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location);

    but fact is that 'appConfig' object wouldnt return null even if there is no presence of file in the required location. I found 'appConfig.HasFile'  as 'False' during debugging and found that file was missed in the location.

    Thank you for your interest in solving my issue.




    Tuesday, November 26, 2013 11:41 AM