简体   繁体   English

如何在WCF操作中创建异步/后台进程?

[英]How to create an async/background process within a WCF operation?

In my WCF operations I will do the logic necessary for the operation: save a record, get a dataset, etc. and in some cases I need to log the activity as well. 在WCF操作中,我将执行操作所需的逻辑:保存记录,获取数据集等,在某些情况下,我还需要记录活动。 However, in these cases I feel that there is no point in having the client application waiting for the WCF operation to log the activity. 但是,在这些情况下,我感到让客户端应用程序等待WCF操作记录活动没有意义。 I would like to fire off the logging process and then immediately return whatever necessary to the client without waiting for the logging process to complete. 我想启动日志记录过程,然后立即将所有必要的内容返回给客户端,而无需等待日志记录过程完成。

I do not care to know when the logging process is complete, just fire and forget. 我不在乎知道日志记录过程何时完成,只是开火而忘记了。

I also prefer to use BasicHttpBinding to maintain maximum interoperability. 我也更喜欢使用BasicHttpBinding来保持最大的互操作性。

Is this possible? 这可能吗? Would anyone care sharing coding samples or links to sites with coding examples? 有人愿意分享编码示例或带有编码示例的网站链接吗?

This can be accomplished pretty easily using any number of threading techniques. 使用任何数量的线程技术都可以很容易地做到这一点。

For a very simple example, try modifying this: 对于一个非常简单的示例,请尝试对此进行修改:

// Log something going on.
System.Threading.ThreadPool.QueueUserWorkItem((args) =>
{
   System.Diagnostics.EventLog.WriteEntry("my source", "my logging message");
});

Inside that lambda method you can use whatever logging class you prefer, and you can include local variables to the logger if you want to log some current state. 在该lambda方法中,您可以使用您喜欢的任何日志记录类,并且如果要记录某些当前状态,可以在记录器中包含局部变量。

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

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