Convert 'Windows Application' to 'Windows Service' .... imposible?  
Author Message
vtortola





PostPosted: Common Language Runtime, Convert 'Windows Application' to 'Windows Service' .... imposible? Top

Hi all!

I want run my server application as system service, but i starts the project as 'Windows Application' ... how to convert it now to 'Windows Service' :S

I try to add the component "Windows Service", and change the "Application type" to 'Windows Service', but i can't select 'Service1' as my "Startup Object", then when i register de service and launch it from the "Services" magnagement mmc ... die at 30 seconds because the services mmc can't comunicate with it :(

Nevertheless, if i create a new project like 'Windows Service' runs ok.

Regards.




.NET Development26  
 
 
IanG





PostPosted: Common Language Runtime, Convert 'Windows Application' to 'Windows Service' .... imposible? Top

Your program needs a suitable entry point. Are you writing in C# or VB.NET

If you look at what Visual Studio 2005 gives you when you create a new C# Windows Service application, you'll see some code like this:

static void Main()

{

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 ServiceBase[] {new Service1(), new MySecondUserService()};

//

ServicesToRun = new ServiceBase[] { new Service1() };

ServiceBase.Run(ServicesToRun);

}

This is the critical code that enables your application to run correctly as a service. If you look at the equivalent entry point for a Windows Application you'll see it looks quite different.

If you're using VB.NET it's the same problem, only the VB.NET IDE hides it from you, making it harder to diagnose...

Create a new Windows Service project. In Solution Explorer, click the 'Show All Files' button at the top. (It has a picture of some files and folders.) Expand Service1.vb to reveal Service1.Designer.vb. Open that file. Notice that it contains this:

' The main entry point for the process

<MTAThread()> _

<System.Diagnostics.De****NonUserCode()> _

Shared Sub Main()

Dim ServicesToRun() As System.ServiceProcess.ServiceBase

' More than one NT 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 Service1}

System.ServiceProcess.ServiceBase.Run(ServicesToRun)

End Sub

This is the VB.NET equivalent of the C# code shown above.

Now do the same thing in the app you converted from a Windows Application into a Windows Service. Notice that the Service1.Designer.vb file does not contain this code.

That's why your service is not appearing in the Startup Object list - its missing that Sub Main entry point. If you copy that code over into either Service1.vb or Service1.Designer.vb, you should find that your service appears in your Startup Object list.


 
 
overpeer





PostPosted: Common Language Runtime, Convert 'Windows Application' to 'Windows Service' .... imposible? Top

Nice !! A lot of thanks !

Now i can launch the app like service but ... i do:

Protected Overrides Sub OnStart(ByVal args() As String)

dim i as new Form1

' Add code here to start your service. This method should set things

' in motion so your service can do its work.

i.Show()

End Sub

And then apperars Form1 but empty! xD. What's my error What's could be the correct form of init the Application from the service unit

Thanks again !

Regards.-