CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2009
    Posts
    596

    Windows Service Won't Stay Up

    Hello guys. At my first attempt at learning this stuff I'm stuck in that this service named MathService stops shortly after it starts. I go to control panel/administrative tools/services I click start on the service. A message pops up saying that the service was started then stopped and that some services are stopped if they aren't being used. I'm trying to to write the client program. But I'm stuck at the point of adding a service reference. I enter http://localhost:8080/MathService press enter when at the "add service reference" and it can't find it. I'm thinking it's because the service isn't running. But I can't get the service to stay running. This is the service code if that's the problem. Can someone please help me wiht this?

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    
    using MathServiceLibrary;
    using System.ServiceModel;
    
    namespace MathWindowsServiceHost
    {
        public partial class MathWinService : ServiceBase
        {
            private ServiceHost myHost;
    
            public MathWinService()
            {
                InitializeComponent();
            }
    
            protected override void OnStart(string[] args)
            {
                if (myHost != null)
                {
                    myHost.Close();
                    myHost = null;
                }
                //create the host.
                myHost = new ServiceHost(typeof(MathService));
    
                // The ABCs in code
                Uri address = new Uri("http://localhost:8080/MathServiceLibrary");
                WSHttpBinding binding = new WSHttpBinding();
                Type contract = typeof(IBasicMath);
    
                //add this endpoint
                myHost.AddServiceEndpoint(contract, binding, address);
    
                //open the host.
                myHost.Open();
            }
    
            protected override void OnStop()
            {
                //shut down the host.
                if (myHost != null)
                    myHost.Close();
            }
        }
    }

  2. #2
    Join Date
    Nov 2004
    Posts
    105

    Re: Windows Service Won't Stay Up

    Hi,

    Check Administrator tools/ Event viewer windows logs/ application.

    Here you will why this service is stopped

    Regards
    Ravi.
    Ravi.Battula

  3. #3
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Windows Service Won't Stay Up

    I've found with the few services I've programmed that they need to be kept alive by a thread. Start a thread in the OnStart handler and place your logic in it - that, at least, has kept mine running

    I'm sure there's an explanation for this - something about message loops and whatnot - but I'm not sure about it so I won't get into it
    It's not a bug, it's a feature!

  4. #4
    Join Date
    Dec 2009
    Posts
    596

    Re: Windows Service Won't Stay Up

    Thanks guys. Batula because of what you shared with me I learned that the service was failing and traced it to a mis-spelling. Super thanks to both of you.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Windows Service Won't Stay Up

    Check out my Tray Notify article series. Parts II and III talk about hosting a WCF services inside a Windows Service application. I've written a WcfServiceHost class that automatically reads serviceModel info out of the app.config file and hosts the services within.

    I've structured the Windows service code to allow you to step through the code in debug mode (which calls the static WcfServiceHost.RunServiceAsConsoleApp method). In non-debug mode, when running as a service, the ServiceBase derived class simply calls the instance version of WcfServiceHost.

    Here's an idea of the simplicity of the Windows service code.

    Code:
    /// 
    /// The main entry point for the application.
    /// 
    static void Main( string [] args )
    {
      TrayNotifyHostService service = new TrayNotifyHostService( );
    
      // To run in debug mode, add "/Debug" to the Start Options,
      // "Command Line Arguments" text box located in the Debug
      // tab of the project properties.
      if ( IsDebugMode( args ) )
      {
        WcfServiceHost.RunServiceAsConsoleApp( "Tray Notify Host", service.EventLog );
        
        // Exit without running in Service mode
        return;
      }
         
      //
      // SERVICE MODE (when service is started by the SCM)
      // 	
    
      try
      {
        ServiceBase [ ] servicesToRun = new ServiceBase [ ] { service };
        ServiceBase.Run( servicesToRun );
      }
      catch ( Exception e )
      {
        service.EventLog.WriteEntry( e.Message
          , System.Diagnostics.EventLogEntryType.Error );
      }
    }
    
    /// 
    /// Checks the command line args for a /debug entry
    /// 
    private static bool IsDebugMode( string [ ] args )
    {
      if ( args == null || args.Length == 0 ) return false;
    
      if ( args [ 0 ].ToLower( ) == "/debug" ) return true;
    
      return false;
    }
    As I mentioned, the WcfServiceHost class does all the work of reading the app.config file, enumeration the serviceModel\services\service entries and launching the ServiceHost instances for each entry.

  6. #6
    Join Date
    Dec 2009
    Posts
    596

    Re: Windows Service Won't Stay Up

    Ok Arjay, thanks for sharing. I'll definitly take a look at that

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured