简体   繁体   English

WCF和执行服务后的更长​​操作

[英]WCF and a longer operation after executing a service

I have a WCF service with many methods. 我有许多方法的WCF服务。 I would like that after executing one of the methods emails will be send to some users. 我希望执行其中一种方法后,会将电子邮件发送给某些用户。 Sending emails may be a long running operation and I don't want a caller of the method to await this time. 发送电子邮件可能需要很长时间才能完成,因此我不希望该方法的调用方等待这次。 The caller should receive a response as soon as it is computed and the emails should be send afterwards. 计算后,呼叫者应立即收到响应,然后应发送电子邮件。 I was thinking about sending the emails in a new thread, but I am not sure if it is correct to start new threads when WCF service is hosted in IIS. 我当时正在考虑在新线程中发送电子邮件,但是我不确定在IIS中托管WCF服务时启动新线程是否正确。 Could somebody tell me what is the best practice in such cases? 有人可以告诉我在这种情况下的最佳做法是什么?

Thanks in advance Lukasz Glaz 在此先感谢Lukasz Glaz

There is nothing inherently problematic about starting new threads when a service is hosted in IIS, but consider this: 在IIS中托管服务时启动新线程并没有本质上的问题,但是请考虑以下因素:

How important is it that the email is sent? 发送电子邮件有多重要?

When you start a background thread, there is no guarantee that the process doesn't crash before the operation completes. 启动后台线程时,无法保证该操作不会在操作完成之前崩溃。 In worst cases, the machine may simply crash due to external reasons (loss of power, Blue Screen of Death, etc.) and there's really nothing you can do to prevent that. 在最坏的情况下,机器可能仅是由于外部原因(电力中断,蓝屏死机等)而崩溃,实际上您无能为力。 In such cases (and perhaps less severe cases as well), the thread may never complete its operation, meaning that the email is never sent. 在这种情况下(可能还有不太严重的情况),线程可能永远不会完成其操作,这意味着永远不会发送电子邮件。

If you can live with that, sending the email from a new thread is fine. 如果可以接受,那么从新线程发送电子邮件就可以了。

If, one the other hand, you must guarantee that the email will always be sent, you will need to hand off that operation to a transactional queue of some sort (either MSMQ, a database table or some other transactional mechanism). 另一方面,如果您必须保证将始终发送电子邮件,则需要将该操作转交给某种事务队列(MSMQ,数据库表或其他事务机制)。

In that case, you then need another robust background process (a Windows Service comes to mind) that pulls messages off the queue and sends the emails. 在这种情况下,您需要另一个健壮的后台进程(想到Windows服务),它将消息从队列中拉出并发送电子邮件。 This architecture guarantees that the emails will eventually be sent, even if the server suddenly reboots. 这种结构保证即使服务器突然重新启动,电子邮件也将最终被发送。 However, it is a much more complex setup, so I think you should only implement it if the requirements state that emails must be sent. 但是,它的设置要复杂得多,因此,我认为只有在要求指出必须发送电子邮件的情况下,才应实施它。

You could also make the operation OneWay , so the client doesn't wait for the result of the operation. 您也可以将操作设为OneWay ,这样客户端就不必等待操作结果了。 This would be the right choice, in my opinion, as you basically don't care about the result of the operation (mail failures, etc...). 我认为这是正确的选择,因为您基本上不关心操作的结果(邮件失败等)。 If you want to ensure delivery, you'll have to configure reliability for you service. 如果要确保交付,则必须配置服务的可靠性。

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

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