简体   繁体   English

Windows Service with WCF

[英]Windows Service With WCF

I need to build an application that runs in the background and start as soon as the windows are started. 我需要构建一个在后台运行的应用程序,并在Windows启动后立即启动。 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. 我需要第二个应用程序,它必须是Windows窗体,它向主应用程序提供状态的表示和一些控件。

First I started doing a Windows Service as the main app and a Windows Forms as the secondary, but I found a serious problem. 首先,我开始将Windows Service作为主要应用程序,将Windows Forms作为辅助应用程序,但是发现了一个严重的问题。 How they communicate each other. 他们如何互相交流。 So TCP was my first thought, but the firewall blocked me. 因此,TCP是我的第一个想法,但是防火墙阻止了我。

I researched and found the WCF. 我研究并找到了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. 显然,使用WCF可以在两个应用程序之间进行通信,而不会造成太多麻烦,但是我从未使用过WCF,并且有两点让我感到烦恼。

The WCF has problems with firewall? WCF的防火墙有问题吗? Use a Windows Service <=> WCF <=> Windows Forms is a good approach, I mean, this is not a bad practice? 使用Windows服务<=> WCF <=> Windows窗体是一种好方法,我的意思是,这不是一个坏习惯吗?

Please help me I'm very lost. 请帮助我,我很失落。

Using WCF to communicate between a Windows service and a front-end application is a perfectly fine approach. 使用WCF在Windows服务和前端应用程序之间进行通信是一种完美的方法。 I have used this approach in the past with success. 我过去曾成功使用过这种方法。

WCF is basically an abstraction layer around the transport mechanism. WCF基本上是围绕传输机制的抽象层。 You define what data needs to be exchanged; 您定义哪些数据需要交换; WCF takes care of how to exchange the data. WCF负责如何交换数据。 The really nice thing about WCF is that it turns your data exchange into formalized method calls. 关于WCF的真正好处是,它将您的数据交换变成了形式化的方法调用。 From your point of view, sending a status message from the Windows service to the application is as simple as making a method call. 从您的角度来看,从Windows服务向应用程序发送状态消息就像进行方法调用一样简单。 Behind the scenes, WCF serializes the message, sends it to the remote endpoint, and returns control to you when the exchange is complete. 在幕后,WCF对消息进行序列化,将其发送到远程端点,并在交换完成后将控制权还给您。

WCF works well on the same machine or across machines. WCF在同一台计算机上或在多台计算机上都能很好地工作。 If you're on the same machine, the NetNamedPipeBinding is likely what you'd want to use. 如果您在同一台计算机上,则可能要使用NetNamedPipeBinding For cross-machine communication, there are a wide variety of options to choose from, including the NetTcpBinding . 对于跨计算机通信,可以选择多种选项,包括NetTcpBinding I've found this flow chart helpful when selecting a binding. 我发现此流程图在选择绑定时很有用。

WCF绑定选择流程图 .

In the interest of full disclosure, I've become frustrated with WCF for three reasons: 为了完全公开,出于以下三个原因,我对WCF感到沮丧:

  1. The steep learning curve associated with WCF. 与WCF相关的陡峭学习曲线。
  2. Lack of seamless integration with the Visual Studio IDE. 缺少与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. 大约3年前,当我上一次使用WCF时,Visual Studio并没有使其易于使用,这只会加剧我的学习难度。 Visual Studio may make it really easy today; 今天,Visual Studio可能使它变得非常简单。 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. 以我的经验,第一次调用方法时,WCF速度很慢,此后几乎是瞬时的。 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. 我的软件的用户多次对性能发表了评论,因此我今天使用的是基于TCP的解决方案,它可以缓解该问题。

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). 只要确保使用本地主机地址(127.0.0.1)。 If they are on different systems, WCF can help mitigate the firewall issue with one of the http bindings. 如果它们位于不同的系统上,则WCF可以通过http绑定之一帮助缓解防火墙问题。

I hope this helps. 我希望这有帮助。

I've used WCF for communicating between a client and server before. 我以前使用WCF在客户端和服务器之间进行通信。 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. 他们可能会对您有很大帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM