How to run the C# Console application as services?

  • Thread starter Thread starter Joe
  • Start date Start date
J

Joe

Dear Friends,
How to run the C# Console application as services?
I have a console application.I want run this application as background
services.User don't want see the command propmt.
If anyone knows please let me know.

Thanks,
Joe
 
You have to create a window service project, and copy the code you want to
run and place your code in the OnStart function. Example:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
namespace Remoting
{
public class MainService : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public MainService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(),
new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new
MainService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// MainService
//
this.ServiceName = "Dion Backup Service";
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
TcpChannel chan=new TcpChannel(8085);
ChannelServices.RegisterChannel(chan);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingServer),"D
ionBackupServer",
WellKnownObjectMode.Singleton);
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
}
}
}
 
Dear Dan Cimpoiesu,
Thanks for your reply.
The application is console apllication.How to disable the command prompt
through C# Program? But the application will run the backround
continuously.
If you know please let me know...

Thanks,
Joe

Dan Cimpoiesu said:
You have to create a window service project, and copy the code you want to
run and place your code in the OnStart function. Example:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
namespace Remoting
{
public class MainService : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public MainService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(),
new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new
MainService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// MainService
//
this.ServiceName = "Dion Backup Service";
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
TcpChannel chan=new TcpChannel(8085);
ChannelServices.RegisterChannel(chan);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingServer),"D
ionBackupServer",
WellKnownObjectMode.Singleton);
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your
service.
}
}
}

Joe said:
Dear Friends,
How to run the C# Console application as services?
I have a console application.I want run this application as background
services.User don't want see the command propmt.
If anyone knows please let me know.

Thanks,
Joe
 
You want to run a console app but not have it create a new console
window?

Either make it a service and run it thru' the services layer in
Windows or create a formless windows forms application.

I think the service is the better way to go.
 
Back
Top