Jonny Piazzi
Jonny Piazzi

Reputation: 3784

Windows Service With WCF

I need to build an application that runs in the background and start as soon as the windows are started. I need a second application, this must be a windows forms, which provides a presentation of the status and some control to the main application.

First I started doing a Windows Service as the main app and a Windows Forms as the secondary, but I found a serious problem. How they communicate each other. So TCP was my first thought, but the firewall blocked me.

I researched and found the WCF. Apparently using WCF I can communicate between two applications without too many headaches, but I never used WCF and has two things that worry me.

The WCF has problems with firewall? Use a Windows Service <=> WCF <=> Windows Forms is a good approach, I mean, this is not a bad practice?

Please help me I'm very lost.

Upvotes: 1

Views: 256

Answers (2)

user1795804
user1795804

Reputation:

I've used WCF for communicating between a client and server before. I see no reason why you shouldn't be able to do this for two local applications. You should check out these two links though. They'll probably help you a lot.

Upvotes: 1

Matt Davis
Matt Davis

Reputation: 46034

Using WCF to communicate between a Windows service and a front-end application is a perfectly fine approach. I have used this approach in the past with success.

WCF is basically an abstraction layer around the transport mechanism. You define what data needs to be exchanged; WCF takes care of how to exchange the data. The really nice thing about WCF is that it turns your data exchange into formalized method calls. From your point of view, sending a status message from the Windows service to the application is as simple as making a method call. Behind the scenes, WCF serializes the message, sends it to the remote endpoint, and returns control to you when the exchange is complete.

WCF works well on the same machine or across machines. If you're on the same machine, the NetNamedPipeBinding is likely what you'd want to use. For cross-machine communication, there are a wide variety of options to choose from, including the NetTcpBinding. I've found this flow chart helpful when selecting a binding.

WCF Binding Selection Flow Chart.

In the interest of full disclosure, I've become frustrated with WCF for three reasons:

  1. The steep learning curve associated with WCF.
  2. Lack of seamless integration with the Visual Studio IDE.
  3. Performance.

The learning curve is a one-time cost, but it can be substantial. When I last used WCF about 3 years ago, Visual Studio did not make it easy to use, which only exacerbated my learning curve. Visual Studio may make it really easy today; I'm just not sure. My real frustration, though, is performance. In my experience, WCF is slow the very first time you invoke a method and almost instantaneous after that. The users of my software commented on the performance more than once, so I'm using a TCP-based solution today that has alleviated the problem.

As to your particular problem, if your service and application are running on the same system, the firewall ought not be an issue. Just make sure you use the localhost address (127.0.0.1). If they are on different systems, WCF can help mitigate the firewall issue with one of the http bindings.

I hope this helps.

Upvotes: 4

Related Questions