本文介绍了WCFclient仅操作Async .Net core 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net core 2.0中将wcf服务端点添加到了连接的服务中,然后尝试使用它,但是对于客户端,只有以..async结尾的功能

I added wcf services end point in asp.net core 2.0 to connected services and then I try to use that but with client there is only functions which ended with ..async

我不想使用... async。但是没有.async就没有功能

I don't want to use ...async.But there is no function without .async

这是什么问题我该怎么办?

What is problem with this?What should I do?

而不是使用它

 var response = SystemClient.SearchCountriesAsync(....

我要使用它

 var response = SystemClient.SearchCountries(...

但出现错误

错误CS1061'SystemClient'不包含'SearchCountries'的定义,也没有扩展方法'SearchCountries'接受类型为'SystemClient'的第一个参数可以找到(您是否缺少using指令或程序集引用?)

Error CS1061 'SystemClient' does not contain a definition for 'SearchCountries' and no extension method 'SearchCountries' accepting a first argument of type 'SystemClient' could be found (are you missing a using directive or an assembly reference?)

推荐答案

您的客户端不会公开同步方法,但这对您来说应该不是问题。

Your client does not expose synchronous method but that shouldn't be a problem for you.

不是异步调用该方法,而是这样做:

Instead of asynchronously calling the method just do this:

response = SystemClient.SearchAirportsAsync(credentials, SystemHelperLanguageTypes.English, SystemHelperSearchTypes.CodeSearch, "ist").Result;

这将同步调用该方法,因为它将阻止该调用。在。

This will call the method synchronously as it will block the call. Check John Skeets answer here.

话虽如此,我建议您使用提供的async方法。为了支持这一点,您必须将Action签名更改为:

That being said I would recomend you use the async method that is provided. To support that you would have to change the Action signature to this:

public async Task<IActionResullt> Index()
{
   SystemClient SystemClient = new SystemClient();
   Credential credential = new Credential();
   credential.UserName = "username";
   credential.UserPassword = "****";

   var response1 = await SystemClient.SearchCountriesAsync(credentials, SystemHelperLanguageTypes.English, SystemHelperSearchTypes.CodeSearch, "TR");
   var response = await SystemClient.SearchAirportsAsync(credentials, SystemHelperLanguageTypes.English, SystemHelperSearchTypes.CodeSearch, "ist");

   //Do whatever you do with those responses

   ViewBag.Language = "ar";
   return View();
}

这篇关于WCFclient仅操作Async .Net core 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 23:34