首页 > 解决方案 > 如何在等待 WCF 回调时保持控制台应用程序运行?

问题描述

我正在编写一个从 3rd 方服务接收实现回调的程序。我目前正在通过 保持应用程序打开Console.ReadLine,但是,我不确定它是否在等待用户输入时冻结了整个程序?或者我的回调不起作用。

第一次使用.net,请多多包涵;获得了大部分代码,特别是第三方 api 文档中的回调事件。

主要的:

public static void Main(string[] args)
{
   Console.WriteLine("Hello World!");
   var callback = new ThirdPartyCallbackImplementation();
   var context = new InstanceContext(callback);
   ThirdPartyClient client = new ThirdPartyClient(context);

   callback.FoundEvent += CallbackOnFoundEvent;
   client.Subscribe();

   Console.ReadLine();
}

private static void CallbackOnFoundEvent(object info) 
{
   Console.WriteLine("Something was found!");
}

第三方回调实现:

public delegate void CallBackEvent(object info);

[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Multiple,
    UseSynchronizationContext = false, ValidateMustUnderstand = false)]

public class ThirdPartyCallbackImplementation : ThirdPartyCallback
{
   public event CallBackEvent FoundEvent;

   private void OnFound(object info)
   {
      this.FoundEvent?.Invoke(info);
   }

   // function implementation
   public void Found()
   {
      CallBackEvent c = this.OnFound;
      c.BeginInvoke(result, ar => { }, null);
   }
}

根本没有错误。它只是在那里说Hello World!。API 确实可以正常工作,因为它有自己的接口并且可以正确触发。

标签: c#.netwcf

解决方案


我首选的解决方案是利用 Rx 事件扩展

public async static Task Main(string[] args)
{
   var callback = new ThirdPartyCallbackImplementation();
   var context = new InstanceContext(callback);
   ThirdPartyClient client = new ThirdPartyClient(context);


   client.Subscribe();
   object info = await callback.WhenEventFound()
                      //timeout
                      .Take(TimeSpan.FromSeconds(10))
                      .FirstOrDefaultAsync()
                      .ToTask();
   if(info != null)
      Console.WriteLine("Something was found!");

   Console.ReadLine();
}

private static IObservable<object> WhenEventFound(this ThirdPartyCallbackImplementation proxy)
{
     return Observable.FromEvent<CallBackEvent, object>(h => x => h(x),
            handler => proxy.FoundEvent += handler,
            handler => proxy.FoundEvent -= handler)
}

这段代码看起来很像一个从 Java 移植过来的库,可能是 Ignite.net。


推荐阅读