Pregunta

Tengo un servicio de Windows que me gustaría recopilar algunos datos sobre el uso de la depuración de Intellitrace - el problema es que no se puede depurar un servicio de Windows al iniciar directamente desde el interior VS. Tengo instalado el servicio, y la primera declaración en Service.Start es "Debug.Break", lo que me permite adjuntar VS. Sin embargo, no se puede utilizar si Intellitrace un proceso ya se ha iniciado cuando se adjunta.

¿Alguien sabe de una solución para esto?

¿Fue útil?

Solución

Es posible con un poco de trabajo. La idea general es burlarse de una aplicación de consola que va a llamar a los métodos OnStart y OnStop del servicio. No es exactamente la ruta de inicio y parada de un servicio pasará a través, pero es de esperar que se llega a tal punto que se puede diagnosticar su problema. He incluido un código de ejemplo para darle una idea general.

ConsoleMock.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindowsService1;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Service1 s1 = new Service1();
            while (true)
            {
                Console.WriteLine(">1 Start\n>2 Stop");
                string result = Console.ReadLine();
                if (result == "1")
                {
                    var method = s1.GetType().GetMethod("OnStart", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                    method.Invoke(s1, new object[] { args });
                }
                else if (result == "2")
                {
                    var method = s1.GetType().GetMethod("OnStop", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                    method.Invoke(s1, new object[] { });
                }
                else
                {
                    Console.WriteLine("wrong command");
                }
            }
        }
    }
}


Service.cs:
    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 System.Threading;

    namespace WindowsService1
    {
        public partial class Service1 : ServiceBase
        {
            private long serviceCounter;
            private Thread workerThread;

            public Service1()
            {
                InitializeComponent();
                serviceCounter = 0;

            }

            public void Worker()
            {
                while (true)
                {
                    serviceCounter += 1;
                    System.Threading.Thread.Sleep(500);

                    try
                    {
                        throw new Exception(serviceCounter.ToString());
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            protected override void OnStart(string[] args)
            {
                workerThread = new Thread(new ThreadStart(Worker));
                workerThread.Start();
            }

            protected override void OnStop()
            {
                workerThread.Abort();
            }
        }
    }

Otros consejos

La técnica de Evan no responde a la pregunta original.

Para una solución ver http://blogs.msdn.com/b/msaffer/archive/2011/02/23/using-intellitrace-with-services.aspx . Poco crear un registro de cadena múltiple nombre del valor "medio ambiente" en HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services * * myservice con tres líneas

COR_ENABLE_PROFILING=1
COR_PROFILER={301EC75B-AD5A-459C-A4C4-911C878FA196}
VSLOGGERCPLAN=Put_here_the_path_to_your_CollectionPlan.xml
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top