简体   繁体   English

完成后在 Web 服务中分离线程

[英]Separate threads in a web service after it's completed

If this has been asked before my apologies, and this is .NET 2.0 ASMX Web services, again my apologies =D如果在我道歉之前有人问过这个问题,这是 .NET 2.0 ASMX Web 服务,再次道歉 =D

A .NET Application that only exposes web services.仅公开 Web 服务的 .NET 应用程序。 Roughly 10 million messages per day load balanced between multiple IIS Servers.每天大约有 1000 万条消息在多个 IIS 服务器之间进行负载平衡。 Each incoming messages is XML, and an outgoing message is XML.每个传入消息都是 XML,传出消息是 XML。 (XMLElement) (we have beefy servers that run on steroids). (XMLElement)(我们有在类固醇上运行的强大服务器)。

I have a SLA that all messages are processed in under X Seconds.我有一个 SLA,所有消息都在 X 秒内处理。

One function, Linking Methods, in the process is now taking 10-20 seconds, it is required for every transaction, however is not critical that it happens before the web service returns the results.一个函数,链接方法,在这个过程中现在需要 10-20 秒,每个事务都需要它,但是它在 Web 服务返回结果之前发生并不重要。 Because of this I made a suggestion to throw it on another thread, but now realize that my words and the eager developers behind them might have not fully thought this through.正因为如此,我建议把它扔到另一个线程上,但现在意识到我的话和他们背后的热心开发人员可能没有完全考虑到这一点。

The below example shows on the left the current flow.下面的示例在左侧显示了当前的流程。 On the right what is being attempted在右边正在尝试什么

Effectively what I'm looking for is to have a web service spawn a long running (10-20 second) thread that will execute even after the web service is completed.实际上,我正在寻找的是让 Web 服务产生一个长时间运行(10-20 秒)的线程,即使在 Web 服务完成后,该线程也会执行。

我正在寻找的示例

This is what, effectively, is going on:实际上,这就是正在发生的事情:

        Thread linkThread= new Thread(delegate()
        {
            Linkmembers(GetContext(), ID1, ID2, SomeOtherThing, XMLOrSomething);
        });
        linkThread.Start();

Using this we've reduced the time from 19 seconds to 2.1 seconds on our dev boxes, which is quite substantial.使用它,我们将开发箱的时间从 19 秒减少到 2.1 秒,这是相当可观的。

I am worried that with the amount of traffic we get, and if a vendor/outside party decides to throttle us, IIS might decide to recycle/kill those threads before they're done processing.我担心随着我们获得的流量如此之大,如果供应商/外部方决定限制我们,IIS 可能会决定在这些线程完成处理之前回收/终止这些线程。 I agree our solution might not be the "best" however we don't have the time to build in a Queue system or another Windows Service to handle this.我同意我们的解决方案可能不是“最好的”,但是我们没有时间构建队列系统或其他 Windows 服务来处理这个问题。

Is there a better way to do this?有一个更好的方法吗? Any caveats that should be considered?有什么需要考虑的注意事项吗?

Thanks.谢谢。

Apart from the issues you've described, I cannot think of any.除了你描述的问题,我想不出任何问题。 That being said, there are ways to fix the problem that do not involve building your own solution from scratch.话虽如此,有一些方法可以解决问题,而无需从头开始构建自己的解决方案。

Use MSMQ with WCF : Create a WCF service with an MSMQ endpoint that is IIS hosted (no need to use a windows service as long as WAS is enabled) and make calls to the service from within your ASMX service.MSMQ 与 WCF 结合使用:使用由IIS 托管的 MSMQ 端点创建 WCF 服务(只要启用了 WAS,就无需使用 Windows 服务),并从 ASMX 服务内部调用该服务。 You reap all the benefits of reliable queueing without having to build your own.您无需构建自己的队列即可获得可靠队列的所有好处。

Plus, if your MSMQ service fails or throws an exception, it will reprocess automatically.另外,如果您的 MSMQ 服务失败或抛出异常,它将自动重新处理。 If you use DTC and are hitting a database, you can even have the MSMQ transaction flow to the DB.如果您使用 DTC 并且正在访问数据库,您甚至可以将 MSMQ 事务流​​传输到数据库。

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

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