Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping a C# service in a console app to debug it

I want to debug a service written in C# and the old fashioned way is just too long. I have to stop the service, start my application that uses the service in debug mode (Visual studio 2008), start the service, attach to the service process and then navigate in my Asp.Net application to trigger the service.

I basically have the service running in the background, waiting for a task. The web application will trigger a task to be picked up by the service.

What I would like to do is to have a console application that fires the service in an effort for me to debug. Is there any simple demo that anybody knows about?

like image 750
Jack Smit Avatar asked Mar 25 '10 16:03

Jack Smit


People also ask

What is wrap code?

In a software context, the term “wrapper” refers to programs or codes that literally wrap around other program components. Several different wrapper functions can be distinguished. They are often used for ensuring compatibility or interoperability between different software structures.

What is a wrapper C++?

Wrapper class in C++ Wrapper is generally defined as the packaging, or to bound an object. A "wrapper class" is used to manage the resources so that it will be crystal-clear to every one. This wraps the resources by simply wrapping the pointer into an int. In this another function is called having less code.

What is a Python wrapper?

What are Wrappers in Python? So, wrappers are the functionality available in Python to wrap a function with another function to extend its behavior. Now, the reason to use wrappers in our code lies in the fact that we can modify a wrapped function without actually changing it. They are also known as decorators.


2 Answers

You can do something like this in the main entry point:

static void Main() { #if DEBUG     Service1 s = new Service1();     s.Init(); // Init() is pretty much any code you would have in OnStart(). #else     ServiceBase[] ServicesToRun;     ServicesToRun=new ServiceBase[]      {          new Service1()      };     ServiceBase.Run(ServicesToRun); #endif } 

and in your OnStart Event handler:

protected override void OnStart(string[] args) {     Init(); } 
like image 125
scottm Avatar answered Oct 13 '22 10:10

scottm


The approach I always take is to isolate out all of your application's logic into class libraries. This makes your service project really just a shell that hosts your class libraries as a service.

By doing this, you can easily unit test and debug your code, without having to deal with the headache of debugging a service by attaching to a process. I'd recommend unit testing of course, but if you're not doing that then adding a console application that calls the same entry point as your service is the way to go.

like image 31
Aaron Daniels Avatar answered Oct 13 '22 12:10

Aaron Daniels