C# Service seems to fail on install but is working?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathan Lark
    New Member
    • Sep 2010
    • 1

    C# Service seems to fail on install but is working?

    Hey All,

    So I have a weird one for ya'll. Currently I have C# service that once started creates a thread which will read a stop tomcat if its started. Reads it to see if its been configured, if it hasn't it will write the file and then start it if not it will simply restart the tomcat service.

    When I install my service(using NSIS and a DLL called simple service) I have some issues these two events show up in the event log simultaneously. First this one shows in the system log:

    The (Service Name) service terminated unexpectedly. It has done this 1 time(s). The following corrective action will be taken in 60000 milliseconds: Restart the service.

    Event ID:7031


    and then this event in the Application log:

    EventType clr20r3, P1 (Service name).exe, P2 1.0.0.0, P3 4c8ea600, P4 system.servicep rocess, P5 2.0.0.0, P6 4333aef4, P7 3a, P8 4e, P9 system.invalido perationexcepti on, P10 NIL.

    Event ID:5000

    After a few minutes or rebooting the system and logging in it will give me an message box with the information for the second error.

    However after the install before rebooting. The Service is not only starting but it has rewritten the Tomcat Config File exactly the way I want it to. The error will never be seen again without uninstalling everything and reinstalling it.

    Anyone have any idea on whats going on?

    Thanks,
    J
  • ZiadElmalki
    New Member
    • Sep 2010
    • 43

    #2
    This means there was an unhandled exception in your application that caused it to crash. From the error we can see where the exception occured.

    EventType clr20r3,
    P1 (Service name).exe,
    P2 1.0.0.0, --app version
    P3 4c8ea600, --app time stamp
    P4 system.servicep rocess, -- module
    P5 2.0.0.0, -- Assmple version
    P6 4333aef4, -- Assembly timestamp
    P7 3a, -- method def. metadata token.
    P8 4e, -- Il offset
    P9 system.invalido perationexcepti on, -- exception name
    P10 NIL -- Nothing




    Looks like you are using .Net 2.0 sp3. I wrote a quick snippet of code that will find the method given the metadata token.
    Code:
                int token = 0x3a;
                Type[] types = typeof(System.ServiceProcess.ServiceAccount).Assembly.GetTypes();
                foreach (Type t in types)
                {
                    MethodInfo[] methods = t.GetMethods(BindingFlags.DeclaredOnly|BindingFlags.Public|BindingFlags.Static|BindingFlags.NonPublic|BindingFlags.Instance);
                    foreach (MethodInfo m in methods)
                    {
                        if ((m.MetadataToken & 0xFFFFFF) == token)
                        {
                            Console.WriteLine(m.DeclaringType.Name + "." + m.Name);
                            break;
                        }
                    }
                }

    The methoddef is for ServiceBase.Req uestAdditionalT ime. For the IL offset 0x4e I see the InvalidOperatio nException being thrown.


    Code:
        L_003f: ldstr "NotInPendingState"
        L_0044: call string System.ServiceProcess.Res::GetString(string)
        L_0049: newobj instance void [mscorlib]System.InvalidOperationException::.ctor(string)
        L_004e: throw
    The C# code from reflection looks like this

    Code:
    [ComVisible(false)]
    public unsafe void RequestAdditionalTime(int milliseconds)
    {
        fixed (NativeMethods.SERVICE_STATUS* service_statusRef = &this.status)
        {
            if (((this.status.currentState != 5) && (this.status.currentState != 2)) && ((this.status.currentState != 3) && (this.status.currentState != 6)))
            {
                throw new InvalidOperationException(Res.GetString("NotInPendingState"));
            }
            this.status.waitHint = milliseconds;
            this.status.checkPoint++;
            NativeMethods.SetServiceStatus(this.statusHandle, service_statusRef);
        }
    }

    Not reall sure what is calling this. Is this exception from your code or the installer? If it is from the installer can you tried installing the service instead with installutil.exe that comes .net. If the exception is coming from your code try adding some error logging so we can see a stack trace or something.
    Last edited by ZiadElmalki; Sep 24 '10, 05:05 AM. Reason: added code blocks

    Comment

    Working...