getting started with windows services

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

    getting started with windows services

    (1) I tried to use what seems like a standard line (from Walkthrough:
    Creating a Windows Service Application in the Component Designer at
    Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


    if (!System.Diagno stics.EventLog. SourceExists("M ySource")) { ...

    But received this error when attempting to start the service:

    System.Security .SecurityExcept ion was unhandled
    Message: The source was not found, but some or all event logs could not
    be searched. Inaccessible logs: Security.

    (2) Per the walkthrough, I had set up the account on the
    ServiceProcessI nstaller to LocalService, which I assumed had insufficent
    privilege. So I tried to recompile after setting this account to a
    different choice. I then naively tried to install from the setup project
    again, but got a message that the "service was marked for deletion" and I
    needed to re-run the installer. But re-running gave the same message.

    (3) I then tried to use "installuti l /u MyNewService" to remove the
    service outside of VS so I could re-install. But installutil did not
    recognize the "/u" option! It just gave a list of "command/test/option
    names". (This is on WinXP Pro.)

    (4) So I re-launched VS and tried the installer again and this time it
    installed successfully. But unfortunately, when I tried to start the
    service, it produced the original error in (1) above.

    Questions:
    (A) Why am I getting a SecurityExcepti on and how can I get around it?
    (B) What is the appropriate way to re-install a service?
    (C) Why does installutil /u not work as advertised?
  • Robbe Morris [C# MVP]

    #2
    Re: getting started with windows services

    This will probably be helpful. It has self installing capabilities.

    Self-Updating Windows Service Infrastructure with Command Pattern Message
    Queue Invoker Service



    --
    Robbe Morris - 2004-2006 Microsoft MVP C#
    I've mapped the database to .NET class properties and methods to
    implement an multi-layered object oriented environment for your
    data access layer. Thus, you should rarely ever have to type the words
    SqlCommand, SqlDataAdapter, or SqlConnection again.






    "michael sorens" <m_j_sorens@com munity.nospamwr ote in message
    news:op.tgte7uj u62op9a@spo-cont-2-dt.itron.com...
    (1) I tried to use what seems like a standard line (from Walkthrough:
    Creating a Windows Service Application in the Component Designer at
    Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

    >
    if (!System.Diagno stics.EventLog. SourceExists("M ySource")) { ...
    >
    But received this error when attempting to start the service:
    >
    System.Security .SecurityExcept ion was unhandled
    Message: The source was not found, but some or all event logs could not
    be searched. Inaccessible logs: Security.
    >
    (2) Per the walkthrough, I had set up the account on the
    ServiceProcessI nstaller to LocalService, which I assumed had insufficent
    privilege. So I tried to recompile after setting this account to a
    different choice. I then naively tried to install from the setup project
    again, but got a message that the "service was marked for deletion" and I
    needed to re-run the installer. But re-running gave the same message.
    >
    (3) I then tried to use "installuti l /u MyNewService" to remove the
    service outside of VS so I could re-install. But installutil did not
    recognize the "/u" option! It just gave a list of "command/test/option
    names". (This is on WinXP Pro.)
    >
    (4) So I re-launched VS and tried the installer again and this time it
    installed successfully. But unfortunately, when I tried to start the
    service, it produced the original error in (1) above.
    >
    Questions:
    (A) Why am I getting a SecurityExcepti on and how can I get around it?
    (B) What is the appropriate way to re-install a service?
    (C) Why does installutil /u not work as advertised?

    Comment

    • Walter Wang [MSFT]

      #3
      RE: getting started with windows services

      Hi Michael,

      Since creating event log source requires administrative privilege, and it's
      only needed to create once, I recommend you create the event log source in
      the installer rather than in the service.

      We can move the code to create the event log source from MyNewService to
      ProjectInstalle r:

      public partial class MyNewService : ServiceBase
      {
      public MyNewService()
      {
      InitializeCompo nent();

      eventLog1.Sourc e = "MySource";
      eventLog1.Log = "MyNewLog";
      }

      [RunInstaller(tr ue)]
      public partial class ProjectInstalle r : Installer
      {
      public ProjectInstalle r()
      {
      InitializeCompo nent();

      if (!System.Diagno stics.EventLog. SourceExists("M ySource"))
      {
      System.Diagnost ics.EventLog.Cr eateEventSource ("MySource",
      "MyNewLog") ;
      }

      As the walkthrough mentioned, reinstalling the service on Windows 2000 will
      needs rebooting since the service control manager will first disable the
      service for deleting on reboot; for Windows xp and above, the windows
      installer has some feature can reinstall the service without rebooting.
      That's why you need to use the generated setup file to re-install the
      service.

      The installutil will need an assembly and install/uninstall the installers
      inside the assembly. In this case, we don't need to use installutil
      manually, since we've already created a setup project to call the installer
      within the service assembly.

      Sincerely,
      Walter Wang (wawang@online. microsoft.com, remove 'online.')
      Microsoft Online Community Support

      =============== =============== =============== =====
      Get notification to my posts through email? Please refer to
      Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

      ications. If you are using Outlook Express, please make sure you clear the
      check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
      promptly.

      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

      • Willy Denoyette [MVP]

        #4
        Re: getting started with windows services


        "michael sorens" <m_j_sorens@com munity.nospamwr ote in message
        news:op.tgte7uj u62op9a@spo-cont-2-dt.itron.com...
        | (1) I tried to use what seems like a standard line (from Walkthrough:
        | Creating a Windows Service Application in the Component Designer at
        | http://msdn2.microsoft.com/en-us/lib...t39148a.aspx):
        |
        | if (!System.Diagno stics.EventLog. SourceExists("M ySource")) { ...
        |
        | But received this error when attempting to start the service:
        |
        | System.Security .SecurityExcept ion was unhandled
        | Message: The source was not found, but some or all event logs could not
        | be searched. Inaccessible logs: Security.
        |
        | (2) Per the walkthrough, I had set up the account on the
        | ServiceProcessI nstaller to LocalService, which I assumed had insufficent
        | privilege. So I tried to recompile after setting this account to a
        | different choice. I then naively tried to install from the setup project
        | again, but got a message that the "service was marked for deletion" and I
        | needed to re-run the installer. But re-running gave the same message.
        |
        | (3) I then tried to use "installuti l /u MyNewService" to remove the
        | service outside of VS so I could re-install. But installutil did not
        | recognize the "/u" option! It just gave a list of "command/test/option
        | names". (This is on WinXP Pro.)
        |
        | (4) So I re-launched VS and tried the installer again and this time it
        | installed successfully. But unfortunately, when I tried to start the
        | service, it produced the original error in (1) above.
        |
        | Questions:
        | (A) Why am I getting a SecurityExcepti on and how can I get around it?
        | (B) What is the appropriate way to re-install a service?
        | (C) Why does installutil /u not work as advertised?

        In order to help you out with this, we need to see more code.
        if (!System.Diagno stics.EventLog. SourceExists("M ySource")) { ...
        is of little help as this line is not the cause of the exception, probably
        what's inside the {... is.
        Anyway, your problem is security related, notably a lack of registry
        privileges. My guess is that you are trying to create a log, but the
        LocalService account (a restricted account!) has no privileges to do so.
        Creating logs from withing services is not advisable, you need to create the
        log using a separate installer program or a simple script.


        Willy.



        Comment

        • michael sorens

          #5
          Re: getting started with windows services

          As both Walter and Willy suggest that best practice dictates not to create
          the event log within the service, I am fine with that. I was merely using
          the sample code from Microsoft's primer. So I moved that couple lines of
          code from Service1.cs to ProjectInstalle r.cs as Walter showed. I then
          opened the setup project context menu and selected Uninstall first, then
          Install, which I am guessing is the better way to try re-installing. Upon
          completion of the Install, my event log was created. (So I assume the
          setup project automatically uses a higher privilege than what I set for
          the Account property of the ServiceProcessI nstaller, yes?) I then used the
          ServerExplorer to locate the service, started it, and it successfully
          added an entry in the new event log.

          So I succeeded in getting my first service to execute properly, but I like
          to understand my loose ends.
          (4) Why does Microsoft's sample code fail on the security exception--is
          that just a case of old code in the walkthrough? (And BTW, the exception
          was indeed on the SourceExists method, and the text of the error message I
          provided supports that. I had not even gotten to the creation of my new
          log, which would most likely also throw a security exception...)
          (5) And why does installUtil not work as advertised on my system?

          Thanks to Walter and Willy for the good tip here. Thanks also to Robbe for
          the intriguing article on managing services without stopping them--it is a
          useful tool for serious applications.

          Comment

          • Walter Wang [MSFT]

            #6
            Re: getting started with windows services

            Hi Michael,

            (4) Yes the documentation is somewhat out-of-date.

            (5) Previously I said it's no need to use installutil manually since we're
            creating a setup which generates .msi file. That doesn't mean installutil
            doesn't work as advertised. I just tested it again:

            C:\projects\_Al l\MyNewService\ bin\Debug>insta llutil MyNewService.ex e
            Microsoft (R) .NET Framework Installation utility Version 2.0.50727.42
            Copyright (c) Microsoft Corporation. All rights reserved.


            Running a transacted installation.

            Beginning the Install phase of the installation.
            See the contents of the log file for the
            C:\projects\_Al l\MyNewService\ bin\Debug
            \MyNewService.e xe assembly's progress.
            The file is located at
            C:\projects\_Al l\MyNewService\ bin\Debug\MyNew Service.Inst
            allLog.
            Installing assembly
            'C:\projects\_A ll\MyNewService \bin\Debug\MyNe wService.exe'.
            Affected parameters are:
            logtoconsole =
            assemblypath = C:\projects\_Al l\MyNewService\ bin\Debug\MyNew Service.exe
            logfile = C:\projects\_Al l\MyNewService\ bin\Debug\MyNew Service.Install Log
            Installing service MyNewService...
            Service MyNewService has been successfully installed.
            Creating EventLog source MyNewService in log Application...

            The Install phase completed successfully, and the Commit phase is beginning.
            See the contents of the log file for the
            C:\projects\_Al l\MyNewService\ bin\Debug
            \MyNewService.e xe assembly's progress.
            The file is located at
            C:\projects\_Al l\MyNewService\ bin\Debug\MyNew Service.Inst
            allLog.
            Committing assembly
            'C:\projects\_A ll\MyNewService \bin\Debug\MyNe wService.exe'.
            Affected parameters are:
            logtoconsole =
            assemblypath = C:\projects\_Al l\MyNewService\ bin\Debug\MyNew Service.exe
            logfile = C:\projects\_Al l\MyNewService\ bin\Debug\MyNew Service.Install Log

            The Commit phase completed successfully.

            The transacted install has completed.

            C:\projects\_Al l\MyNewService\ bin\Debug>net start mynewservice
            The MyNewService service is starting.
            The MyNewService service was started successfully.


            C:\projects\_Al l\MyNewService\ bin\Debug>insta llutil /u MyNewService.ex e
            Microsoft (R) .NET Framework Installation utility Version 2.0.50727.42
            Copyright (c) Microsoft Corporation. All rights reserved.



            The uninstall is beginning.
            See the contents of the log file for the
            C:\projects\_Al l\MyNewService\ bin\Debug
            \MyNewService.e xe assembly's progress.
            The file is located at
            C:\projects\_Al l\MyNewService\ bin\Debug\MyNew Service.Inst
            allLog.
            Uninstalling assembly
            'C:\projects\_A ll\MyNewService \bin\Debug\MyNe wService.exe'

            Comment

            • michael sorens

              #7
              Re: getting started with windows services

              Whether trying to install or uninstall, I get the same error from
              installutil:
              installutil MyNewService.ex e
              Error dispatching command/test named 'MyNewService.e xe'

              And I figured out why: my search path was picking up a *different* program
              called 'installutil' (this one from Rational). Once I adjusted it, now I
              get the .Net utility. Mystery solved.

              Comment

              • Walter Wang [MSFT]

                #8
                Re: getting started with windows services

                Hi Michael,

                Thank you for letting us know the cause.

                Have a nice day!

                Regards,
                Walter Wang (wawang@online. microsoft.com, remove 'online.')
                Microsoft Online Community Support

                =============== =============== =============== =====
                When responding to posts, please "Reply to Group" via your newsreader so
                that others may learn and benefit from your issue.
                =============== =============== =============== =====

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

                Comment

                Working...