Automatically update Winows .NET Service

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter

    #1

    Automatically update Winows .NET Service

    Is there a way to automatically update a Windows .NET service, kind of like
    Click-Once or put a file in the directory and the service would restart it's
    self and replace the executables or at least replace its DLLs without
    restarting? I know it's posible, (anything is posible) but is there
    anything already in place?

    Thank You


    Peter





  • \Ji Zhou [MSFT]\

    #2
    RE: Automatically update Winows .NET Service

    Hello Peter,

    Actually, from the MSDN document
    http://msdn.microsoft.com/en-us/library/ms973805.aspx, we can see that the
    ClickOnce does not support installing the .Net Windows Services. We have to
    implement the self update logical by ourselves. The following is my simple
    solution for this objective.

    Firstly, I describe the scenario of my solution,

    1.A .net Windows service named service.exe located at c:\services\
    2.A folder named c:\services\upd ates stores the new version of a .net
    Windows service
    3.Now, you have a new version of the above mentioned service named
    newservice.exe
    4.An app named serviceupdate.e xe is located at c:\services\ for updating a
    services automatically
    Now, the solution is described as the following steps:

    1.Creating an instance of FileSystemWatch er to listen to the change of file
    system, the code looks like:
    private void IntializeFileSy stemWatcher()
    {
    System.IO.FileS ystemWatcher newexe = new System.IO.FileS ystemWatcher(
    "c:\\services\\ Updates", "newservice.exe ");
    //other codes are described in step 2
    }

    protected override void OnStart(string[] args)
    {
    IntializeFileSy stemWatcher();
    }

    2.Listen to the Created or Changed event of the FileSystemWatch er instance,
    the code looks like:
    //the following code is put in the method IntializeFileSy stemWatcher()
    newexe.Created += new FileSystemEvent Handler(NewExec utable);
    newexe.Changed += new FileSystemEvent Handler(NewExec utable);
    newexe.EnableRa isingEvents = true;

    private void NewExecutable(o bject source, FileSystemEvent Args e)
    {
    Process.Start(" c:\\services\\s erviceupdate.ex e","service" ,
    "newservice ");
    }

    The above code listen to the changed and created event. When the event
    occurs, serviceupdate.e xe will be called to automatically update services.

    3.Creating the serviceupdate.e xe app. The app is a simply console
    application that is used to stop the old service, replace the service file
    and then restart new service. The code looks like:
    static void Main(string[] args)
    {
    if (args.Length == 0)
    return;

    string oldname = args[0];
    string newname = args[1];

    System.ServiceP rocess.ServiceC ontroller target = new
    System.ServiceP rocess.ServiceC ontroller(oldna me);
    target.Stop();

    while(target.St atus
    !=System.Servic eProcess.Servic eControllerStat us.Stopped)
    {
    target.Refresh( );
    }
    Thread.Sleep(20 00);

    File.Delete("c: \\services\\" + oldname + ".exe");
    File.Move("c:\\ services\\Updat es\\" + newname + ".exe", "c:\\services\\ "
    + newname + ".exe");

    target.Start();
    }

    Serviceupdate.e xe needs two parameters. The first one is the name of old
    services that is located at c:\seriveces in the solution. The second one is
    the name of new services that is located at c:\services\upd ates in the
    solution. Of course, you can set the same name for the old and new service
    because they are in different folder.

    Another solution is to create a separate AppDomain for each unique managed
    service assembly and use Remoting to create and control the managed
    service's IService implementation. There are two nice articles related to
    this topic here.




    Please let me know if these help for your scenario. If you have any future
    questions or concerns, please let me know.


    Best regards,
    Ji Zhou (v-jzho@online.mic rosoft.com, remove 'online.')
    Microsoft Online Community Support

    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    msdnmg@microsof t.com.

    =============== =============== =============== =====
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subs...#notifications.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://support.microsoft.com/select/...tance&ln=en-us.
    =============== =============== =============== =====
    This posting is provided "AS IS" with no warranties, and confers no rights.

    Comment

    • Peter

      #3
      Re: Automatically update Winows .NET Service


      """Ji Zhou [MSFT]""" <v-jzho@online.mic rosoft.comwrote in message
      news:%23FELX9ZP JHA.1644@TK2MSF TNGHUB02.phx.gb l...
      Hello Peter,
      >
      Actually, from the MSDN document
      http://msdn.microsoft.com/en-us/library/ms973805.aspx, we can see that the
      ClickOnce does not support installing the .Net Windows Services. We have
      to
      implement the self update logical by ourselves. The following is my simple
      solution for this objective.
      >
      Firstly, I describe the scenario of my solution,
      >
      1.A .net Windows service named service.exe located at c:\services\
      2.A folder named c:\services\upd ates stores the new version of a .net
      Windows service
      3.Now, you have a new version of the above mentioned service named
      newservice.exe
      4.An app named serviceupdate.e xe is located at c:\services\ for updating a
      services automatically
      Now, the solution is described as the following steps:
      >
      1.Creating an instance of FileSystemWatch er to listen to the change of
      file
      system, the code looks like:
      private void IntializeFileSy stemWatcher()
      {
      System.IO.FileS ystemWatcher newexe = new System.IO.FileS ystemWatcher(
      "c:\\services\\ Updates", "newservice.exe ");
      //other codes are described in step 2
      }
      >
      protected override void OnStart(string[] args)
      {
      IntializeFileSy stemWatcher();
      }
      >
      2.Listen to the Created or Changed event of the FileSystemWatch er
      instance,
      the code looks like:
      //the following code is put in the method IntializeFileSy stemWatcher()
      newexe.Created += new FileSystemEvent Handler(NewExec utable);
      newexe.Changed += new FileSystemEvent Handler(NewExec utable);
      newexe.EnableRa isingEvents = true;
      >
      private void NewExecutable(o bject source, FileSystemEvent Args e)
      {
      Process.Start(" c:\\services\\s erviceupdate.ex e","service" ,
      "newservice ");
      }
      >
      The above code listen to the changed and created event. When the event
      occurs, serviceupdate.e xe will be called to automatically update services.
      >
      3.Creating the serviceupdate.e xe app. The app is a simply console
      application that is used to stop the old service, replace the service file
      and then restart new service. The code looks like:
      static void Main(string[] args)
      {
      if (args.Length == 0)
      return;
      >
      string oldname = args[0];
      string newname = args[1];
      >
      System.ServiceP rocess.ServiceC ontroller target = new
      System.ServiceP rocess.ServiceC ontroller(oldna me);
      target.Stop();
      >
      while(target.St atus
      !=System.Servic eProcess.Servic eControllerStat us.Stopped)
      {
      target.Refresh( );
      }
      Thread.Sleep(20 00);
      >
      File.Delete("c: \\services\\" + oldname + ".exe");
      File.Move("c:\\ services\\Updat es\\" + newname + ".exe", "c:\\services\\ "
      + newname + ".exe");
      >
      target.Start();
      }
      >
      Serviceupdate.e xe needs two parameters. The first one is the name of old
      services that is located at c:\seriveces in the solution. The second one
      is
      the name of new services that is located at c:\services\upd ates in the
      solution. Of course, you can set the same name for the old and new service
      because they are in different folder.
      >
      Another solution is to create a separate AppDomain for each unique
      managed
      service assembly and use Remoting to create and control the managed
      service's IService implementation. There are two nice articles related to
      this topic here.
      >


      >
      Please let me know if these help for your scenario. If you have any future
      questions or concerns, please let me know.
      >
      >
      Best regards,
      Ji Zhou (v-jzho@online.mic rosoft.com, remove 'online.')
      Microsoft Online Community Support
      >
      Delighting our customers is our #1 priority. We welcome your comments and
      suggestions about how we can improve the support we provide to you. Please
      feel free to let my manager know what you think of the level of service
      provided. You can send feedback directly to my manager at:
      msdnmg@microsof t.com.
      >
      =============== =============== =============== =====
      Get notification to my posts through email? Please refer to
      http://msdn.microsoft.com/en-us/subs...#notifications.
      >
      Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
      where an initial response from the community or a Microsoft Support
      Engineer within 1 business day is acceptable. Please note that each follow
      up response may take approximately 2 business days as the support
      professional working with you may need further investigation to reach the
      most efficient resolution. The offering is not appropriate for situations
      that require urgent, real-time or phone-based interactions or complex
      project analysis and dump analysis issues. Issues of this nature are best
      handled working with a dedicated Microsoft Support Engineer by contacting
      Microsoft Customer Support Services (CSS) at
      http://support.microsoft.com/select/...tance&ln=en-us.
      =============== =============== =============== =====
      This posting is provided "AS IS" with no warranties, and confers no
      rights.
      >
      Thank you very much, just what I was looking for


      Comment

      Working...