简体   繁体   English

WCF服务与WCF服务通信

[英]WCF Service to talk to WCF Service

I have a solution with 3 projects 我有3个项目的解决方案

  1. Winform Client Winform客户端
  2. WCF Service hosted in a windows service WCF服务托管在Windows服务中
  3. WCF Agent hosted in a windows service WCF代理托管在Windows服务中

The client (Item 1) talks to the agent (Item 3) via the service (Item 2) - all using WCF. 客户端(第1项)通过服务(第2项)与代理(第3项)进行通信 - 全部使用WCF。 The client (item 1) gets information from an SQL database via the service (Item 2) - using WCF. 客户端(第1项)通过服务(第2项)从SQL数据库获取信息 - 使用WCF。

Hence here is how I designed it. 因此,这就是我设计它的方式。 Item 1 contain a WCF client to talk to Item 2 over WCF Item 2 contains a WCF service so it can service item 1 requests. 项目1包含通过WCF与项目2通信的WCF客户端项目2包含WCF服务,因此它可以为项目1请求提供服务。 It also contains a WCF client (to talk to 3) Item 3 contains a WCF service so it can service item 2 requests. 它还包含一个WCF客户端(与3对话)第3项包含一个WCF服务,因此它可以为第2项请求提供服务。

I add a service reference to item 2 in item 1's project i add a service reference to item 3 in item 2's project 我在项目1的项目中添加了对项目2的服务引用,我在项目2的项目中添加了对项目3的服务引用

is this how it should be done give that everything is in the one solution ? 这应该怎么做才能让一切都在一个解决方案中?

Because item 2 and item 3 are self hosted in 2 windows services, every-time i recompile my code i have to stop Item 2 and item 3 services manually in Services.MSC, compile , start Item 1 and Item 2 services and finally run my Winform client by pressing play in visual studio. 因为第2项和第3项是自我托管在2个Windows服务中,每次我重新编译我的代码我必须在Services.MSC中手动停止第2项和第3项服务,编译,启动第1项和第2项服务,最后运行我的通过在visual studio中按play来Winform客户端。 Is there a better easier way of doing all this? 有没有更好的方法来做这一切?

鉴于您的WCF服务和您的代理都托管在Windows服务中并在某台计算机上运行(我想)。您只能使用一个Windows服务来执行wcf代理和wcf服务这样您只能启动和停止一个服务

Are item 2 and 3 (the Windows services) installed from their project locations (probably the bin\\debug folder)? 是否从项目位置(可能是bin \\ debug文件夹)安装了第2项和第3项(Windows服务)? If so, I'm guessing that you're running into an issue with the compiler not being able to write the .exe file (because it's in use, since the services are running). 如果是这样,我猜你遇到了编译器无法编写.exe文件的问题(因为它正在使用,因为服务正在运行)。

In this case, I'd recommend installing the actual windows services from a different location (for example, C:\\WindowsServices\\Service1 and C:\\WindowsServices\\Service2). 在这种情况下,我建议从其他位置安装实际的Windows服务(例如,C:\\ WindowsServices \\ Service1和C:\\ WindowsServices \\ Service2)。 That way, as long as you aren't making changes to the code of either of the services, you can compile your solution (which includes item 1) while the Windows services are running, as the compiler will be writing the .exe file to the project bin\\debug folder, not the folder(s) where the services are actually running from. 这样,只要您不对任何一个服务的代码进行更改,就可以在Windows服务运行时编译您的解决方案(包括第1项),因为编译器会将.exe文件写入项目bin \\ debug文件夹,而不是实际运行服务的文件夹。

This will allow you to make changes to your windows application, recompile and test. 这将允许您更改Windows应用程序,重新编译和测试。

Another alternative would be to remove (or exclude) the two windows services from the solution that you have your windows application in. 另一种方法是从您拥有Windows应用程序的解决方案中删除(或排除)两个Windows服务。

EDIT 编辑

If I understand your question, you're asking what is the proper way to reference WCF services when they are in the same solution? 如果我理解你的问题,你问的是当它们在同一个解决方案中时,引用WCF服务的正确方法是什么? In that case, it sounds like you have it set up correctly (though it's a little hard to tell). 在这种情况下,听起来你已经正确设置(虽然这有点难以辨别)。

Basically, for a client to communicate with a WCF service, you need to have a reference to that service so you can generate the proxy. 基本上,对于客户端与WCF服务进行通信,您需要引用该服务,以便生成代理。 The proxy is what you use to actually communicate with the service. 代理是您用来与服务实际通信的内容。

Item 1 will have a service reference to item 2 (the WCF service). 项目1将具有对项目2(WCF服务)的服务引用。

Item 2 will have a service reference to item 3 (another WCF service). 第2项将具有对项目3(另一个WCF服务)的服务引用。

The fact that item 2 is itself a WCF service doesn't prevent it from being a client to another, different WCF service. 项2本身是WCF服务的事实并不妨碍它成为另一个不同WCF服务的客户端。

As long as you're using service references for the WCF services (as opposed to regular assembly references) it sounds to me like you're set up correctly. 只要您使用WCF服务的服务引用(而不是常规程序集引用),就像您设置正确一样。 Move the installation of the services to a different location and your issue with not being able to rebuild the solution should go away. 将服务的安装移动到其他位置,您的无法重建解决方案的问题应该消失。

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

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