简体   繁体   English

WCF服务依赖项

[英]WCF Service dependencies

I have three wcf services A,B and C respectively ,since i wanted it to be SOA(Service Oriented Architecture) the way my setup works is when i send a request from client to server. 我分别有三个wcf服务A,B和C,因为我希望它是SOA(面向服务的体系结构),我的设置工作方式是当我从客户端向服务器发送请求时。

  1. All the services are self hosted windows services. 所有服务都是自托管的Windows服务。
  2. Client sends request to service A (client has no clue about the other services B and C); 客户端向服务A发送请求(客户端对其他服务B和C没有任何线索);
  3. Service A eventually sends that request to Service B and Service C. 服务A最终将该请求发送到服务B和服务C.
  4. Service B and C sends response back to Service A which would be sent back to the client by service A. 服务B和C将响应发送回服务A,服务A将由服务A发送回客户端。

Issue im facing :If i make any changes in the code of Service B and rebuild and restart the service ,i am having issue getting the response back but when i restart all the remaining services then it works fine. 问题即将面临 :如果我对服务B的代码进行任何更改并重建并重新启动服务,我有问题得到回复,但当我重新启动所有剩余的服务,那么它工作正常。

In other words my client doesn't get the response back unless i restart all the services(A,B and C) even though i just changed the code in only one service and rebuilt it.I know the thing works if i restart all the three services but i want to know is this the problem in my way of designing or it is something i have to deal with self hosted windows services.And all the services(A,B,C) are independent as none depends on each other. 换句话说,我的客户端没有得到回复,除非我重新启动所有服务(A,B和C),即使我只更改了一个服务中的代码并重建它。我知道如果我重新启动所有的东西三个服务,但我想知道这是我的设计方式的问题,或者我必须处理自托管的Windows服务。所有服务(A,B,C)是独立的,因为没有相互依赖。

Did some one ever see such things happened in SOA.I would be glad if some one can guide me to appropriate solution ? 有人曾经在SOA中看到过这样的事情。如果有人能引导我找到合适的解决方案,我会很高兴吗?

  1. Replace WCF between services with any sort of queue (one service publishes something, other can read when they are ready). 用任何类型的队列替换服务之间的WCF(一个服务发布一些东西,其他服务在准备就绪时可以读取)。 Can be anything. 可以是任何东西。 Can be a simple table where you read from if there is something new. 可以是一个简单的表格,您可以从中读取是否有新的东西。 Can be RabbitMQ, NServiceBus, etc, whatever works for you. 可以是RabbitMQ,NServiceBus等,无论什么适合你。

  2. Define messages you put into the queue: commands and events. 定义放入队列的消息:命令和事件。 Both are simple classes with properties, no logic there. 两者都是具有属性的简单类,没有逻辑。 Commands represent what the system is asked to do (RegisterUser, PlaceOrder, ect), events represent what the system has done (UserRegistered, OrderApproved, PaymentReceived, etc). 命令表示要求系统执行的操作(RegisterUser,PlaceOrder等),事件表示系统已完成的操作(UserRegistered,OrderApproved,PaymentReceived等)。 Be explicit about actions , Don't do something like "I have changed all the properties of a user on the client, now I call SaveUser(user)". 明确操作 ,不要做“我已经更改了客户端上用户的所有属性,现在我调用SaveUser(用户)”。 Your service supposes to know how to change objects, clients should only command what to do. 您的服务设知道如何改变的对象,客户端应该只命令该怎么

  3. Never break your contract. 永远不要破坏合同。 It is easy, easier than it sounds: you can add things to your message contracts, but cannot remove. 它比听起来容易,容易:您可以在邮件合同中添加内容,但无法删除。 In other word you just keep your contract backwards compatible. 换句话说,你只是让你的合同向后兼容。

Now you have a much better design: services communicate only through messages in queues, messages are backward compatible. 现在你有了一个更好的设计:服务只通过队列中的消息进行通信,消息是向后兼容的。 This means that you can stop any of the services at any time without impacting others: they will continue sending messages into queues, and when the stopped service comes back again it will catch up processing all the stuff from the queue. 这意味着您可以随时停止任何服务而不会影响其他服务:它们将继续将消息发送到队列中,当停止的服务再次返回时,它将赶上处理队列中的所有内容。

Then, if you want, you can use the same approach with client interactions: if instead of calling WCF clients would only put their commands in some sort of a queue then service upgrades or other downtime would not impact user experience. 然后,如果需要,您可以使用与客户端交互相同的方法:如果不是调用WCF客户端只将其命令放在某种队列中,那么服务升级或其他停机时间不会影响用户体验。

Example: if I use WCF to place an order or to put an item into a shopping card then if there is a problem or a service is down for maintenance I will not be able to do it. 示例:如果我使用WCF下订单或将商品放入购物卡中,那么如果出现问题或服务因维护而关闭,我将无法执行此操作。 I would click a button and have a nasty error. 我会点击一个按钮,有一个令人讨厌的错误。 More importantly my order will not make into the system. 更重要的是,我的订单不会进入系统。 In contrast, if there is a queue in the middle, I only put my command into the queue. 相反,如果中间有一个队列,我只将我的命令放入队列。 Now even if my service is down at the moment, or experience a high load (and therefore slow) then my user experience is still the same and does not degrade. 现在,即使我的服务暂时停止,或者遇到高负载(因此很慢),我的用户体验仍然是相同的,并且不会降级。 It is just my command will be processed a bit later, but as a client I don't really care. 这只是我的命令将在稍后处理,但作为客户端我并不在乎。 And my order will not be lost in this scenario. 在这种情况下,我的订单不会丢失。 The system became fault-tolerate and self-balanced. 该系统变得容错并且自我平衡。

There are all sorts of fantastic tricks you can do if you simply put a queue in the middle instead of experiencing problems with spatial and temporal coupling that comes with WCF :) And what I described is just the beginning... :) 如果你只是在中间排队而不是遇到WCF带来的空间和时间耦合问题,你可以做各种奇妙的技巧:)而我所描述的只是开始...... :)

You may want to consider using a service bus such as NServiceBus to help you accomplish your functionality. 您可能需要考虑使用NServiceBus等服务总线来帮助您完成功能。

The first issue it will help you address is the decoupling of your services via publish/subscribe messaging pattern. 它将帮助您解决的第一个问题是通过发布/订阅消息传递模式将服务分离。 Rather than invoking web services in one or the other service, publish events that notify the respective services when something has occurred. 不是在一个或另一个服务中调用Web服务,而是发布在发生某些事件时通知相应服务的事件。 In your case this would look something like this: 在你的情况下,这看起来像这样:

  1. Client invokes web service in Service A. 客户端在服务A中调用Web服务。
  2. Service A publishes a message "Client Command Received" which Service B and C subscribe to. 服务A发布消息“客户端命令已接收”,服务B和C订阅该消息。
  3. Service B and C handle this event and then publish events of their own. 服务B和C处理此事件,然后发布自己的事件。
  4. Service A subscribes to both events and replies to the client. 服务A订阅事件和回复客户端。

The first and immediate benefit of using something NServiceBus is reliability. 使用NServiceBus的第一个也是直接的好处是可靠性。 On top of that you are able to easily version your message without affecting your client or your respective services. 最重要的是,您可以轻松地对消息进行版本设置,而不会影响您的客户或您的相应服务。 NServiceBus has full WCF integration so your client can continue to send messages to your service as before. NServiceBus具有完整的WCF集成,因此您的客户端可以像以前一样继续向您的服务发送消息。

One of the things that makes your scenario interesting is that you can't guarantee when Service B and C send their responses back to you. 使您的场景变得有趣的一个原因是,您无法保证服务B和C何时将其响应发送给您。 Do you keep the connection to the client open until Service has received their responses? 在服务收到回复之前,您是否保持与客户端的连接打开? Do you need both responses before you can send a the client its response? 在向客户发送响应之前,您是否需要两个响应? What happens if either or one of the service crash? 如果其中一个或一个服务崩溃会发生什么? What if there is a time limit to how long you can wait before a response is received by Service A? 如果服务A收到响应之前您可以等待多长时间,该怎么办? All of these questions and more can be answered with a feature in NServiceBus called Sagas. 所有这些问题和更多问题都可以通过名为Sagas的NServiceBus中的一个功能来回答。 Check it out. 看看这个。

If using NServiceBus is not possible then things become more difficult. 如果不能使用NServiceBus那么事情变得更加困难。 WCF doesn't support publish/subscribe out of the box so you will have to bake your own. WCF不支持发布/订阅开箱即用,因此您必须自己烘焙。 At a minimum I would recommend using this to decouple your services. 我建议至少使用它来解耦您的服务。 How you manage state and temporal coupling in your services is another matter. 如何管理服务中的状态和时间耦合是另一回事。 Save yourself the trouble. 省去麻烦。

There are other frameworks out there but if you want a developer centric, cost effective way to create a .NET based solution then recommend using NServiceBus. 还有其他框架,但如果您想要以开发人员为中心,经济高效的方式来创建基于.NET的解决方案,那么建议使用NServiceBus。

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

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