简体   繁体   English

在事务中从 WCF 服务向 REST API 发出 Post 请求

[英]Making a Post request to a REST API from a WCF service in a transaction

I am doing a series of work in a wcf service transaction.我正在做一个 wcf 服务事务的一系列工作。

1.Making a POST response to a rest api. 1.对 rest api 进行 POST 响应。 2.Processing POST req response and saving it in a db. 2.处理 POST req 响应并将其保存在数据库中。

Above step 1 and step 2 are happening in a wcf transaction but as a result I have seen an issue where on having an exception step 2 doesn't get committed but step 1 gets completed creating inconsistencies.上面的第 1 步和第 2 步发生在 wcf 事务中,但结果我看到了一个问题,即在出现异常时第 2 步没有被提交,但第 1 步完成创建不一致。

I have seen way out to achieve this through saga pattern or 2PC but couldn't find any easy implementation or workaround.我已经看到了通过 saga 模式或 2PC 实现这一目标的方法,但找不到任何简单的实现或解决方法。

PS: I NEED NOT TO ROLLBACK DATA FOR NOW JUST AVOID COMMITTING POST REQ TO REST API. PS:我现在不需要回滚数据,只是避免向 REST API 提交请求。 Currently I am handling this by making another patch request to rest api to revert the response received in CATCH BLOCK.目前我正在通过向 rest api 发出另一个补丁请求来处理这个问题,以恢复在 CATCH BLOCK 中收到的响应。

The bad news:坏消息:

There is no 2PC over rest apis. rest apis没有2PC。 Rest is by definition stateless, does not include envelopes for metadata to be used in a two phase commit world, so you are out of luck here. Rest 根据定义是无状态的,不包括用于两阶段提交世界的元数据信封,所以你在这里不走运。

SOAP on the other hand supports the WS-Atomic Transaction protocols, though I would suggest that you use some technology like WCF where all this is implemented already.另一方面,SOAP 支持WS-Atomic Transaction协议,但我建议您使用 WCF 之类的技术,所有这些都已经实现。 (net core does not support it, so it .net framework for this). (net core不支持,所以.net框架为此)。

The somewhat good news:有点好消息:

Eventual consistency to the rescue.救援的最终一致性。 The SAGA Pattern is one way to achieve this in the microservices world. SAGA 模式是在微服务领域实现这一目标的一种方式。 In it's simplest form, eventual consistency could be something as trivial as the following:在最简单的形式中,最终的一致性可能是微不足道的,如下所示:

  1. Application 1 creates a DB row that described the action in Pending state应用程序 1创建了一个数据库行,描述了Pending state 中的操作
  2. Application 1 makes the post to a url应用程序 1发布到 url
  3. The target api, creates an event with the result in a persistent way (on an event bus, a queue or even a database table that the Application 1 can access目标 api 以持久方式(在应用程序 1可以访问的事件总线、队列甚至数据库表上)创建具有结果的事件
  4. Some service or background thread of Application 1 constantly reads or listens to that event bus for the api's results Application 1的某些服务或后台线程不断读取或侦听该事件总线以获取 api 的结果
  5. Once it gets the result, it updates the row made on 1 .一旦得到结果,它就会更新在1上创建的行。 It retries until possible.它会重试直到可能。 You can stop it at some point.你可以在某个时候停止它。

The negative of this approach, is that you need to manually intervene if 5 constantly fails.这种方法的缺点是,如果 5 不断失败,您需要手动干预。 There are more complicated aspects involved (like compensating actions) but I'll leave this up to you for further study.涉及更复杂的方面(如补偿动作),但我将把它留给你进一步研究。

The other bad news另一个坏消息

There is no out of the box implementation for eventual consistency:( If you can switch the REST Api to a SOAP with WSAT protocol enabled, then you can have out of box distributed transactions.最终一致性没有开箱即用的实现:(如果您可以将 REST Api 切换到 SOAP 并启用 WSAT 协议,那么您可以拥有分布式事务。

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

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