简体   繁体   English

C#Web服务在返回响应后执行某些操作

[英]C# web service do something after return response

I have a web service called S 我有一个名为S的网络服务

My client have a web service called C 我的客户有一个名为C的Web服务。

  • My client send a request to my web service ( web service S ) 我的客户向我的Web服务( Web服务S )发送请求
  • Web service S will send a response to client ( C ) Web服务S将向客户端( C )发送响应
  • After that, my service ( S ) will create 1 invoice message and send it to client web service ( C ) 之后,我的服务( S )将创建1条发票消息并将其发送到客户端Web服务( C
  • Client web service return result to my web service ( S ) 客户端Web服务返回到我的Web服务的结果( S

How to implement it? 如何执行呢?

在此处输入图片说明

As I understand, you want to return a response to client app, but still continue with some processing. 据我了解,您想将响应返回给客户端应用,但仍要继续进行一些处理。 There are a few possibilities here: 这里有几种可能性:

  1. Start a new thread in 2., that will create the invoice and send it to client WS. 在2.中启动一个新线程 ,该线程将创建发票并将其发送到客户端WS。 This however can be error-prone - your web service might die or be shut down in the middle of creating the invoice and client WS will never know. 但是,这可能容易出错-在创建发票的过程中,您的Web服务可能会死机或关闭,并且客户端WS永远不会知道。
  2. Use something like hangfire to schedule invoice creation. 使用诸如hangfire之类的方法来计划发票的创建。 Hangfire stores scheduled tasks in DB, so it will be eventually executed, even in case of failure. Hangfire将计划的任务存储在数据库中,因此即使发生故障也将最终执行该任务。 This does not require additional configuration other than setting up the backend db. 除了设置后端数据库外,不需要其他配置。 Processing happens in the same hosting process of your Service. 处理过程与服务的托管过程相同。
  3. Use a ServiceBus or MSMQ - the idea is simmilar as with Hangfire - you send a message (saying like "create invoice with parameters X") to the Bus, the Bus makes sure the message gets delivered to anyone that listens for it. 使用ServiceBusMSMQ -这个想法是simmilar与迟发型-你发送消息(说像“创建参数X发票”)的总线,总线确保消息被分发给任何人监听了。 Then you register a listener that would handle that kind of message and create the invoice. 然后,您将注册一个处理此类消息的侦听器并创建发票。 This would require more work, since you have to choose the Service Bus engine , take a moment to understand it, install, configure, etc. 这将需要更多的工作,因为您必须选择Service Bus引擎 ,花点时间来了解它,安装,配置等。

This is a good case for a domain event. 对于域事件,这是一个很好的案例。 I don't know what the first request is - perhaps placing an order? 我不知道第一个请求是什么-也许下订单了? When the order is placed then you would raise an event indicating that an order was placed. 下订单后,您将引发一个事件,指示已下订单。 The event could contain either some information about the order or a reference (id) that can be used to retrieve it. 该事件可能包含有关订单的某些信息,也可以包含可用来检索它的参考(id)。 Then other listeners would respond accordingly. 然后其他听众将做出相应的响应。

One benefit is that it keeps different parts of your application decoupled. 好处之一是,它可以使应用程序的不同部分分离。 For example, the class that submits an order doesn't need to know that there's going to be an invoice. 例如,提交订单的类不需要知道会有发票。 It just raises an event indicating that an order has been placed and then goes on its way. 它只是引发一个事件,指示已下订单,然后继续进行。

That becomes even more important if you want to have multiple behaviors when an order is placed. 如果您要在下订单时有多种行为,那将变得更加重要。 Perhaps you also want to send an email confirming that your received the order. 也许您还想发送一封电子邮件,确认您已收到订单。 Now you can add that additional behavior as an event listener with no modification to the code that places the order. 现在,您可以将其他行为添加为事件侦听器,而无需修改下订单的代码。

Also, your application could grow so that perhaps there's another service for placing orders. 此外,您的应用程序可能会增长,因此可能还有另一项用于下订单的服务。 (I'm running with "placing orders" although I don't know what the specific event is.) You don't want multiple points in your application that follow all of the post-ordering steps. (尽管我不知道具体事件是什么,但我正在执行“下订单”。)您不希望应用程序中的多个点遵循所有后排序步骤。 If those steps change then you'd have to modify code in all of those places. 如果这些步骤改变了,那么您将不得不在所有这些地方修改代码。 Instead you just raise the event. 相反,您只是引发事件。

Here's a popular article that describes the concept well. 这是一篇很受欢迎的文章 ,很好地描述了这个概念。 There are numerous implementations of an event bus. 事件总线有多种实现。 Here's one . 这是一个

In pseudocode, you could now have a few event handlers, each of which is completely decoupled from your ordering code. 在伪代码中,您现在可以有几个事件处理程序,每个事件处理程序都与您的订购代码完全分离。

The event itself is raised immediately after the order is submitted. 提交订单后立即引发事件本身。

var order = SubmitOrder(info); 
eventBus.Raise(new OrderSubmitEvent(order));

Then you have some event handlers which are registered to respond to that event. 然后,您将注册一些事件处理程序以响应该事件。

public class SendInvoiceOrderEventHandler : IEventHandler<OrderSubmitEVent>
{
    public void HandleEvent(OrderSubmitEvent e)
    {
        //e contains details about the order. Send an invoice request
    }
}

public class SendConfirmationOrderEventHandler : IEventHandler<OrderSubmitEVent>
{
    public void HandleEvent(OrderSubmitEvent e)
    {
        //send an email confirmation
    }
}

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

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