locked
Forms Application Interacts with Windows Service RRS feed

  • Question

  • (Assume the following is my question to write and please give me an idea where this is best to go)

    I have a Windows Service (C#) I am working on and I would like the user to have some control over it while monitoring its status. I have already understood that having forms in the service is a bad idea (especially considering the service will run as Network Service). So my alternative is to have a separate application that runs in Userspace (Just call it Service Controller App or SCA for now) that communicates with the service. I have been researching the best way to do this and have settled on two major ideas.

    One is to just write a file out and have the programs communicate that way. I’m worried that too many disk interactions will result from this and my desired effects will not be achieved in a timely manner.

    The second is to use WCF. Unfortunately I am not familiar with the technology, but from what I am reading it is designed for more network based operations. I would like something that is either shared memory or just avoids the use of a network system entirely (and it must avoid using the Messaging Service at all costs).

    I am wadding into a swamp without as much as a compass.

    • Moved by Just Karl Tuesday, December 11, 2012 8:29 PM OP requested move (From:Where is the Forum For…?)
    Tuesday, December 11, 2012 4:21 PM

Answers

All replies

  • I'd ask in the Visual C# General forum

    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer



    My Blog: http://unlockpowershell.wordpress.com
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})

    Tuesday, December 11, 2012 5:14 PM
  • Okay then.

    Mod Request: Move this topic to the Visual C# General Forum at http://social.msdn.microsoft.com/forums/en-us/csharpgeneral/threads

    Tuesday, December 11, 2012 5:57 PM
  • WCF isn't just for network communication and is not very difficult for simple IPC.  But if you're not interested in that, you can also use named pipes.

    http://www.switchonthecode.com/tutorials/dotnet-35-adds-named-pipes-support

    Tuesday, December 11, 2012 11:24 PM
  • WCF can also use named pipes for inter process communication.

    Myself, I prefer when services write to their own etw log in the Application and Services folder in Event Viewer. That generally helps the most.

    Of course there are existing solutions to monitoring the health of services (System Center, etc), and being monitored/managed by those solutions would be the best. But if this service is for a small scale deployment have a UI Console that communicates across sessions with WCF is a great solution.

    Tuesday, December 11, 2012 11:37 PM
  • You can control a Windows Service using ServiceController class. This class allows to retrieve the list of all services, pause, start and stop the service.

    http://www.daveoncsharp.com/2009/10/enumerating-and-controlling-windows-services-with-csharp

    http://blog.billsdon.com/2011/05/c-startstoprestart-windows-service-remotely-locally


    Thanks, Murugesan M - Please Mark as the Answer, if this answers your question. Please vote as helpful, if this post is helpful.

    • Proposed as answer by Jason Dot Wang Thursday, December 13, 2012 6:09 AM
    • Marked as answer by Jason Dot Wang Wednesday, December 19, 2012 6:30 AM
    • Unmarked as answer by Jason Dot Wang Wednesday, December 19, 2012 6:32 AM
    • Unproposed as answer by Zero Serenity Thursday, December 20, 2012 5:53 PM
    Tuesday, December 11, 2012 11:50 PM
  • The answer is definitely WCF. It is very easy to do. Here is some info describing how to do it.

    http://msdn.microsoft.com/en-us/library/ms733069.aspx

    Founder & CEO of Stackify - A DevOps solution for application support

    • Proposed as answer by Jason Dot Wang Thursday, December 13, 2012 6:15 AM
    • Marked as answer by Jason Dot Wang Wednesday, December 19, 2012 6:30 AM
    • Unmarked as answer by Jason Dot Wang Wednesday, December 19, 2012 6:32 AM
    • Marked as answer by Zero Serenity Thursday, December 20, 2012 5:53 PM
    • Unmarked as answer by Zero Serenity Thursday, December 20, 2012 5:53 PM
    Wednesday, December 12, 2012 4:37 AM
  • This seems to still be using network. For the sake of it, I did put it together anyway and when I tried it it gave back the problem of permissions since Network Service (and my own personal account for my dev machine) don't have the rights to bind themselves to the port. I need something that doesn't require network access at all.

    Wednesday, December 12, 2012 4:26 PM
  • What's the url you're using for your endpoint?
    Wednesday, December 12, 2012 5:20 PM
  • IPC can be done a variety of ways and WCF seems the most straightforward.  With regards to your permissions you should use nettcp or named pipes for the binding rather than HTTP (basic or WS).  HTTP requires that you have permissions to bind to the port and whatnot.  For intranet communication nettcp is probably the better route.  If you were exposing the WCF service for others to consume then HTTP is appropriate (because you'll likely be hosting it in IIS) but for true IPC the aforementioned bindings are better.

    Just for completeness I'll point out that you can send simple commands to services using the standard SCM interface (exposed by .NET) but you are limited to sending a single integral command code to the service and you don't get any results back.  For a status-type app this isn't solution.  If WCF is too much then you could also use remoting but WCF is generally easier to work with these days.

    Michael Taylor - 12/12/2012
    http://msmvps.com/blogs/p3net

    Wednesday, December 12, 2012 5:39 PM
  • Jared: localhost:65000/ServiceName/service

    Michael (Dad): Do you have information on how to use nettcp or named pipes or something I can use that doesn't require binding permissions?

    Wednesday, December 12, 2012 5:54 PM
  • Rather than using http://localhost:65000... you'd use nettcp://localhost:65000...

    You'd switch from basicHttpBinding/wsHttpBinding to netTcpBinding.  The <binding> element values would be changed a little as well.  Note that you need to make the change in both the client configuration and the server configuration.  Refer to MSDN on how to expose WCF via netTcpBinding.

    Michael Taylor - 12 cubed
    http://msmvps.com/blogs/p3net

    Wednesday, December 12, 2012 6:22 PM
  • New error.

    Could not find a base address that matches scheme net.tcp for the endpoint with binding NetTcpBinding. Registered base address schemes are [nettcp].

              <baseAddresses>
                <add baseAddress="nettcp://localhost:65000/ServiceName/service"/>
              </baseAddresses>


    <endpoint address="" binding="netTcpBinding" contract="ServiceName.IServiceWCF" />

    and

        [ServiceContract(Namespace = "nettcp://ServiceName")]
        interface IServiceWCF

    Is there something else that needs doing?
    Wednesday, December 12, 2012 8:11 PM
  • The base address should begin with net.pipe:// to used named pipes.

    http://msdn.microsoft.com/en-us/library/ms731291.aspx

    Wednesday, December 12, 2012 9:15 PM
  • I spent time with that article, my error now is:

    Configuration binding extension 'system.serviceModel/bindings/NetNamedPipeBinding' could not be found. Verify that this binding extension is properly registered in system.serviceModel/extensions/bindingExtensions and that it is spelled correctly.

    I found this:

    http://msdn.microsoft.com/en-us/library/aa749847.aspx

    But I'm still rather lost as to what I'm missing here.

    Thursday, December 13, 2012 6:55 PM
  • Actually, nevermind. I ran the sample for WCF Named pipes (this one: http://www.microsoft.com/en-us/download/details.aspx?id=21459) and it failed out because of a lack of permissions. Is there something that doesn't require local port binding (and ignores the presence of a network)?
    Thursday, December 13, 2012 7:43 PM
  • You...aparently missed what I just said. I cannot use anything that binds itself to the network which means WCF is out. It cannot be used.

    I'm giving this a try: http://msdn.microsoft.com/en-us/library/bb546085.aspx

    Thursday, December 13, 2012 10:26 PM
  • You don't need network access to use WCF.  Everything can run locally without any issues.  You don't even need to be an admin to set things up.  The non-HTTP bindings are one of the best ways of communicating on the same machine.  However if you don't want to use WCF then remoting is the other option.  However you're going to find it is not as easy to use nor does it support all the features you're going to likely want.  Good luck.
    Thursday, December 13, 2012 11:24 PM
  • I haven't looked at http://code.msdn.microsoft.com/WCF-Non-Administrator-b5d6a3bd in a few years, but it might be what you want.

    Given that you're communicating with a service, and most services run as Admin, you might need Admin perms anyway to do what you want.

    • Marked as answer by Zero Serenity Thursday, December 20, 2012 5:53 PM
    Friday, December 14, 2012 5:49 AM
  • Actually, this one worked. The demo was simple and straightfoward. I'm a bit worried about security for it though since I can't tell if it has any. Assume for a moment a thousand rigs on the same network run this at once. I want to prevent conflicts going on where one controls another instead of controlling itself. I know it says localhost, but I feel like there's a potential attack vector here.
    Friday, December 14, 2012 5:06 PM
  • Given that it's using named pipes, whatever any process is doing on any other machine (even on the same network) won't affect it at all. It's using just inter-process communication on the same machine. I imagine it even requires the client and server to be the same user.
    Friday, December 14, 2012 6:37 PM
  • Any way I can do impersonation? The service I'm having log in as NT AUTORITY/NETWORK SERVICE and the config...might be in userspace.
    Friday, December 14, 2012 6:45 PM