|
-
October 13th, 2011, 03:55 PM
#1
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();
}
}
}
-
October 14th, 2011, 03:02 AM
#2
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
-
October 14th, 2011, 03:08 AM
#3
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!
-
October 14th, 2011, 09:24 AM
#4
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.
-
October 14th, 2011, 03:49 PM
#5
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.
-
October 14th, 2011, 06:32 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|