使用WCF进行异步

时间:2014-04-12 11:32:18

标签: oracle wcf signalr .net-4.5 xsockets.net

我有一个Windows表单应用程序,它依赖于单向WCF服务;它只是从(Oracle)数据库返回数据。 一些客户端对wcf的调用是长时间运行的,现在我们需要实现异步捕获并将Oracle的DBMS_output发送到客户端的方法。 如何使用可以挂钩到WCF的SignalR或类似方法来实现? 我们正在使用.NET framework 4.5。

2 个答案:

答案 0 :(得分:2)

@kims答案是正确的,但为了进一步解释,我将添加一个控制器样本。 只是显示工作可以开始,然后发布消息......

/// <summary>
/// A realtime controller
/// </summary>
public class ZooController : XSocketController
{
    /// <summary>
    /// This method will be hit both from JavaScript, WCF and MVC examples
    /// Could have sent a custom object as well, but settled for a string in this case.
    /// </summary>
    /// <param name="message"></param>
    public void Say(string message)
    {

        //Just send it out to all subscribers
        this.SendToAll(new {message = "Longrunning process started"}, "say");

        Thread.Sleep(10000);

        this.SendToAll(new {message}, "say");
        //You can also use
        //this.Send, this.SendTo<T> etc...
    }
}

编辑:我不知道您的情况,但也许您可以使用here所述的长途控制器选项

答案 1 :(得分:1)

由于问题中标记了XSockets ...

我会订阅客户端中的主题,并让实时框架在长时间运行的任务完成时发布结果。

有关如何将WCF提升为实时的简单示例,请访问:XSockets WCF 该示例只是立即发布结果,但如果在XSockets中调用的方法启动长时间运行的任务并在完成时发布结果,则在完成工作时将通知客户端。

从WCF(或另一个半双工进程)调用XSockets控制器只需两行代码

//Get a connection (if it exists already that one will be used)
var conn = ClientPool.GetInstance("ws://127.0.0.1:4502/Zoo", "*");

//We send an anonymous message here, but any object will work.
conn.Send(new { message = "I was sent over WebSockets: " + message },"Say");