AppSettings Section Being Returned Empty

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nick

    AppSettings Section Being Returned Empty

    Hi there,

    I have a Windows Service application that has a load of settings
    defined, I can access these using My.Settings.

    I would like to pass the NameValueCollec tion of the settings to another
    class but unfortunately no matter what I try I get an empty colleciton back,
    i.e.

    System.Configur ation.Configura tionSettings.Ap pSettings
    <Obsolete
    System.Configur ation.Configura tionManager.App Settings

    Now I know the settings are there, this code was working perfect as a
    standard executable, but since I changed it to a Windows Service, it just
    stopped working, any idea where my settings have gone and how I can get the
    NameValueCollec tion?

    Thanks in advance.

    Nick.


  • Linda Liu[MSFT]

    #2
    RE: AppSettings Section Being Returned Empty

    Hi Nick,

    Based on my understanding, you read AppSettings in a Windows Service
    application but the NameValueCollec tion of the settings returns empty. The
    same code works perfect in a WinForm desktop application. If I'm off base,
    please feel free to let me know.

    I performed a test based on your description but didn't reproduce the
    problem on my side.

    I follow the instructions in the following MSDN document to create a
    Windows Service application:


    I add an Application Configuration File in the project and add an
    appSettings in the app.config file:
    <configuratio n>
    <appSettings>
    <add key="appsetting 1" value="value1"/>
    <add key="appsetting 2" value="value2"/>
    </appSettings>
    ...
    </configuration >

    In the override OnStart method, I add the following lines of code:

    Protected Overrides Sub OnStart(ByVal args() As String)
    ' Add code here to start your service. This method should set things
    ' in motion so your service can do its work.

    Dim config As Configuration =
    ConfigurationMa nager.OpenExeCo nfiguration(Con figurationUserL evel.None)
    Dim col As KeyValueConfigu rationCollectio n =
    config.AppSetti ngs.Settings
    Dim valuestr As String
    valuestr = col.Item("appse tting1").Value & " " &
    col.Item("appse tting2").Value
    EventLog1.Write Entry("In OnStart:" & valuestr)

    End Sub

    Build and install the Windows Service application. When the service is
    started, an entry is written to the MyNewLog "In OnStart:value1 value2".

    Is there any difference between your code and mine?

    Sincerely,
    Linda Liu
    Microsoft Online Community Support

    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    msdnmg@microsof t.com.

    =============== =============== =============== =====
    Get notification to my posts through email? Please refer to
    Gain technical skills through documentation and training, earn certifications and connect with the community

    ications.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscripti...t/default.aspx.
    =============== =============== =============== =====
    This posting is provided "AS IS" with no warranties, and confers no rights.


    Comment

    • Nick

      #3
      Re: AppSettings Section Being Returned Empty

      Hi Linda,

      Indeed there are differences between your code and mine. I have added
      my settings through the project menu and let the IDE which has not produced
      a settings element that looks anything like yours... e.g.

      <applicationSet tings>
      <SomeApp.My.MyS ettings>
      <setting name="Port" serializeAs="St ring">
      <value>1001,100 2</value>
      </setting>
      <setting name="LogFilePa th" serializeAs="St ring">
      <value>c:\logs\ </value>
      </setting>
      <setting name="EnableSou nds" serializeAs="St ring">
      <value>True</value>
      </setting>
      </SomeApp.My.MySe ttings>
      </applicationSett ings>

      As I mentioned in my previous post, I can access the settings through
      the My namespace. So are you suggesting that I need 2 lots of settings? I
      would rather just stick with the settings the way I have them, but if that
      is the only way then I shall create another settings section, any ideas?

      Thanks for your time.

      Nick.

      "Linda Liu[MSFT]" <v-lliu@online.mic rosoft.comwrote in message
      news:T42VmNsyIH A.5440@TK2MSFTN GHUB02.phx.gbl. ..
      Hi Nick,
      >
      Based on my understanding, you read AppSettings in a Windows Service
      application but the NameValueCollec tion of the settings returns empty. The
      same code works perfect in a WinForm desktop application. If I'm off base,
      please feel free to let me know.
      >
      I performed a test based on your description but didn't reproduce the
      problem on my side.
      >
      I follow the instructions in the following MSDN document to create a
      Windows Service application:

      >
      I add an Application Configuration File in the project and add an
      appSettings in the app.config file:
      <configuratio n>
      <appSettings>
      <add key="appsetting 1" value="value1"/>
      <add key="appsetting 2" value="value2"/>
      </appSettings>
      ...
      </configuration >
      >
      In the override OnStart method, I add the following lines of code:
      >
      Protected Overrides Sub OnStart(ByVal args() As String)
      ' Add code here to start your service. This method should set
      things
      ' in motion so your service can do its work.
      >
      Dim config As Configuration =
      ConfigurationMa nager.OpenExeCo nfiguration(Con figurationUserL evel.None)
      Dim col As KeyValueConfigu rationCollectio n =
      config.AppSetti ngs.Settings
      Dim valuestr As String
      valuestr = col.Item("appse tting1").Value & " " &
      col.Item("appse tting2").Value
      EventLog1.Write Entry("In OnStart:" & valuestr)
      >
      End Sub
      >
      Build and install the Windows Service application. When the service is
      started, an entry is written to the MyNewLog "In OnStart:value1 value2".
      >
      Is there any difference between your code and mine?
      >
      Sincerely,
      Linda Liu
      Microsoft Online Community Support
      >
      Delighting our customers is our #1 priority. We welcome your comments and
      suggestions about how we can improve the support we provide to you. Please
      feel free to let my manager know what you think of the level of service
      provided. You can send feedback directly to my manager at:
      msdnmg@microsof t.com.
      >
      =============== =============== =============== =====
      Get notification to my posts through email? Please refer to
      Gain technical skills through documentation and training, earn certifications and connect with the community

      ications.
      >
      Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
      where an initial response from the community or a Microsoft Support
      Engineer within 1 business day is acceptable. Please note that each follow
      up response may take approximately 2 business days as the support
      professional working with you may need further investigation to reach the
      most efficient resolution. The offering is not appropriate for situations
      that require urgent, real-time or phone-based interactions or complex
      project analysis and dump analysis issues. Issues of this nature are best
      handled working with a dedicated Microsoft Support Engineer by contacting
      Microsoft Customer Support Services (CSS) at
      http://msdn.microsoft.com/subscripti...t/default.aspx.
      =============== =============== =============== =====
      This posting is provided "AS IS" with no warranties, and confers no
      rights.
      >
      >

      Comment

      • Linda Liu[MSFT]

        #4
        Re: AppSettings Section Being Returned Empty

        Hi Nick,

        Thank you for your quick response!

        Since you add application-scoped settings in the Settings.settin gs under
        the My Project folder, you cannot access these settings through
        ConfigurationMa nager.AppSettin gs property.

        In your scenario, the application-scoped settings are in a section named
        "SomeApp.My.MyS ettings" under a section group named "applicationSet tings".
        To get these settings using ConfigurationMa nager, we need to get the
        section group first and then get the section. The following is a sample:

        Dim config As Configuration =
        ConfigurationMa nager.OpenExeCo nfiguration(Con figurationUserL evel.None)

        ' get the section group named "applicationSet tings"
        Dim secGroup As ConfigurationSe ctionGroup =
        config.SectionG roups("applicat ionSettings")

        ' get the section named "SomeApp.My.MyS ettings"
        Dim applicationSec As ClientSettingsS ection =
        CType(secGroup. Sections("SomeA pp.My.MySetting s"), ClientSettingsS ection)

        ' get the settings of the section
        Dim applicationSett ingCol As SettingElementC ollection =
        applicationSec. Settings

        ' enumerate the element in the settings
        For Each element As SettingElement In applicationSett ingCol
        Console.WriteLi ne(element.Name & " " & element.Value.V alueXml.Value)
        Next

        Hope this helps.
        If you have any question, please feel free to let me know.

        Sincerely,
        Linda Liu
        Microsoft Online Community Support

        Delighting our customers is our #1 priority. We welcome your comments and
        suggestions about how we can improve the support we provide to you. Please
        feel free to let my manager know what you think of the level of service
        provided. You can send feedback directly to my manager at:
        msdnmg@microsof t.com.

        This posting is provided "AS IS" with no warranties, and confers no rights.


        Comment

        • Linda Liu[MSFT]

          #5
          Re: AppSettings Section Being Returned Empty

          Hi Nick,

          How about the problem now? Have you had a chance to try my suggestion?

          If you have any question, please feel free to let me know.

          Thank you for using our MSDN Managed Newsgroup Support Service!

          Sincerely,
          Linda Liu
          Microsoft Online Community Support

          Delighting our customers is our #1 priority. We welcome your comments and
          suggestions about how we can improve the support we provide to you. Please
          feel free to let my manager know what you think of the level of service
          provided. You can send feedback directly to my manager at:
          msdnmg@microsof t.com.

          This posting is provided "AS IS" with no warranties, and confers no rights.

          Comment

          • Nick

            #6
            Re: AppSettings Section Being Returned Empty

            Hi Linda,

            That's excellent, thank you for your help, that has solved the problem.

            Nick.

            "Linda Liu[MSFT]" <v-lliu@online.mic rosoft.comwrote in message
            news:gyXfjY5yIH A.5796@TK2MSFTN GHUB02.phx.gbl. ..
            Hi Nick,
            >
            Thank you for your quick response!
            >
            Since you add application-scoped settings in the Settings.settin gs under
            the My Project folder, you cannot access these settings through
            ConfigurationMa nager.AppSettin gs property.
            >
            In your scenario, the application-scoped settings are in a section named
            "SomeApp.My.MyS ettings" under a section group named "applicationSet tings".
            To get these settings using ConfigurationMa nager, we need to get the
            section group first and then get the section. The following is a sample:
            >
            Dim config As Configuration =
            ConfigurationMa nager.OpenExeCo nfiguration(Con figurationUserL evel.None)
            >
            ' get the section group named "applicationSet tings"
            Dim secGroup As ConfigurationSe ctionGroup =
            config.SectionG roups("applicat ionSettings")
            >
            ' get the section named "SomeApp.My.MyS ettings"
            Dim applicationSec As ClientSettingsS ection =
            CType(secGroup. Sections("SomeA pp.My.MySetting s"), ClientSettingsS ection)
            >
            ' get the settings of the section
            Dim applicationSett ingCol As SettingElementC ollection =
            applicationSec. Settings
            >
            ' enumerate the element in the settings
            For Each element As SettingElement In applicationSett ingCol
            Console.WriteLi ne(element.Name & " " & element.Value.V alueXml.Value)
            Next
            >
            Hope this helps.
            If you have any question, please feel free to let me know.
            >
            Sincerely,
            Linda Liu
            Microsoft Online Community Support
            >
            Delighting our customers is our #1 priority. We welcome your comments and
            suggestions about how we can improve the support we provide to you. Please
            feel free to let my manager know what you think of the level of service
            provided. You can send feedback directly to my manager at:
            msdnmg@microsof t.com.
            >
            This posting is provided "AS IS" with no warranties, and confers no
            rights.
            >
            >

            Comment

            • Linda Liu[MSFT]

              #7
              Re: AppSettings Section Being Returned Empty

              Hi Nick,

              Thank you for your feedback! I'm glad to hear that the problem is solved
              now.

              If you have any questions in the future, please don't hesitate to contact
              us. It's always our pleasure to be of assistance!

              Have a nice day!

              Sincerely,
              Linda Liu
              Microsoft Online Community Support

              Delighting our customers is our #1 priority. We welcome your comments and
              suggestions about how we can improve the support we provide to you. Please
              feel free to let my manager know what you think of the level of service
              provided. You can send feedback directly to my manager at:
              msdnmg@microsof t.com.

              This posting is provided "AS IS" with no warranties, and confers no rights.

              Comment

              Working...