none
Windows Service architecture RRS feed

  • Question

  • Dear all,

    I am setting up a windows service to listen for a particular tcp port. However before doing so the application needs to check whether configuration has been set. If not set a windows form should popup asking user to set the configuration.

    How is it best to proceed?

    Should i create a windows forms app and then add a service or create the project based on a Windows Service (.NET framework) template. If the latter how can i call a form through the service?

    Thanks

    Saturday, October 24, 2020 1:13 PM

Answers

  • Yes, they would normally be two separate projects.

    If you really want to, you can probably get a single exe file to work as both a Windows Form and a Service by inheriting from the corresponding base classes and tweaking the startup code to invoke one or the other. But even if you manage to build it as a single file, you would have to start it twice with different parameters to run in one mode or the other. I don't think it's worth the bother; just go ahead and build it as two separate projects and you'll save yourself a lot of effort.

    • Marked as answer by tamasuMT Saturday, October 24, 2020 9:16 PM
    Saturday, October 24, 2020 9:09 PM
    Moderator

All replies

  • No, you cannot pop up a Form from a Service. The Service cannot access the desktop to pop up anything.

    If you have seen any services that have a configuration form, they work in one of two ways:

    The first one is that the form is run during installation. It prompts the user for configuration data and writes it to a known location, such as a configuration file or the Windows Registry. Then, when the service starts, it reads the configuration from the specified location. If the service needs to be reconfigured at a later time, the form can be re-run so that it rewrites the configuration. Then, the service can be restarted (the form itself can do that) or the service can monitor the configuration file for changes (for instance, using a FileSystemWatcher) and reload it as necessary.

    The other way is to have a desktop program that starts automatically when the user logs in. It minimizes itself to the desktop tray, and the user can restore it from there, or the service can send a message to the running application (e.g., via a socket or a named pipe) so that the Form restores itself to the desktop. It then communicates the changes back to the service via the same socket or pipe, or rewrites the configuration file, or sends one or more ServiceCommands to the Service.

    Saturday, October 24, 2020 2:21 PM
    Moderator
  • Thanks for clearing this up. In viwe that service and the windows form app have to work independently, this also mean that during coding they have to be two separate projects?

    Thanks once again

    Saturday, October 24, 2020 8:26 PM
  • Yes, they would normally be two separate projects.

    If you really want to, you can probably get a single exe file to work as both a Windows Form and a Service by inheriting from the corresponding base classes and tweaking the startup code to invoke one or the other. But even if you manage to build it as a single file, you would have to start it twice with different parameters to run in one mode or the other. I don't think it's worth the bother; just go ahead and build it as two separate projects and you'll save yourself a lot of effort.

    • Marked as answer by tamasuMT Saturday, October 24, 2020 9:16 PM
    Saturday, October 24, 2020 9:09 PM
    Moderator
  • Very well explained. Your guidance cleared a lot of stuff! thanks
    Saturday, October 24, 2020 9:16 PM
  • https://docs.microsoft.com/en-us/dotnet/architecture/microservices/architect-microservice-container-applications/service-oriented-architecture
    Sunday, November 1, 2020 10:28 AM