简体   繁体   English

WPF在异步等待模式中使用MVVM和WCF

[英]WPF using MVVM with WCF in a async await pattern

I'm building a WPF app that uses MVVM as pattern together with a WCF backend. 我正在构建一个使用MVVM作为模式和WCF后端的WPF应用程序。 My UI must always be responsive and for some controls the data can be up to 10 seconds to load. 我的UI必须始终具有响应性,对于某些控件,数据最多可以加载10秒。

Following code works well and is inside my viewmodel where ConfigurationsForInterface is an Observable type that is bound to my control: 以下代码运行良好,并且在我的viewmodel中,其中ConfigurationsForInterface是绑定到我的控件的Observable类型:

ConfigurationsForInterface = new ObservableCollection<string>() { "Loading..." };      
ConfigurationsForInterface = await GetConfigurationInterfaceXAsync();

Method that works fine: 工作正常的方法:

      public async Task<ObservableCollection<string>> GetConfigurationHL7Server5Async()
        {
            ObservableCollection<string> result;

            using (var localClient = new ManagerListenerClient(Globals.ChannelBinding, Globals.EndpointAddress))
            {               
                result = await  localClient.GetConfigurationsAsync("interfacex");
            }

            return result;
        }

Method that freezes my UI as I reuse the service client already opened in other areas of the app as I set a context for the service (user): 当我为服务(用户)设置上下文时重用已在应用程序的其他区域中打开的服务客户端时冻结我的UI的方法

  public async Task<ObservableCollection<string>> GetConfigurationHL7Server5Async() { ObservableCollection<string> result; result = await Globals.Client.GetConfigurationsAsync("interfacex"); return result; } 

It seems like the WCF client is somehow bound to my UI thread and causes my UI to freeze until the data is loaded. 看起来WCF客户端以某种方式绑定到我的UI线程并导致我的UI冻结,直到加载数据。 I can see from my debugging that it does not wait in the method itself. 我可以从调试中看到它不会在方法本身等待。

Can someone help me out why I cant reuse my WCF client object that is used elsewhere and not always in a async context like just sync calling the viewmodel 有人可以帮助我解决为什么我无法重用我在其他地方使用的WCF客户端对象而不是总是在异步上下文中,例如同步调用viewmodel

Thanks to @Rowbear I pinpointed the real issue. 感谢@Rowbear,我确定了真正的问题。

I changed my binding from WsHttpBinding to NetTCPBinding and then everything runs nicely async. 我将绑定从WsHttpBinding更改为NetTCPBinding,然后一切运行良好的异步。 For my app I can easily use NetTCPBinding. 对于我的应用程序,我可以轻松使用NetTCPBinding。

It seems that something is still going sync. 似乎某些东西仍在同步。 in the WsHttpBinding implementation. 在WsHttpBinding实现中。

Thanks! 谢谢! for the advice. 建议。

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

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