10

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

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

What is problem with this?What should I do?

instead of using that

 var response = SystemClient.SearchCountriesAsync(....

I want to use that

 var response = SystemClient.SearchCountries(...

but it give that error

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?)

enter image description here

user1688401
  • 1,851
  • 8
  • 47
  • 83
  • 1
    There's nothing wrong with the generated code. Why don't you want to use async when *everything* is async? HttpClient is async. The methods you *though* were sync were never actually async - the framework always blocked on the real asynchronous operations to give the illusion of `sync` – Panagiotis Kanavos Nov 13 '17 at 08:45
  • 1
    To put it another way, this isn't a programming issue. You should get familiar with `async`. You'll also reduce your running costs - blocking uses spinwaits which means the CPU isn't idle. By blocking you have to buy a bigger machine or more VMs for the same amount of traffic. Hard to justify `I don't want to use async` when it means paying at least one new VM – Panagiotis Kanavos Nov 13 '17 at 08:49
  • BTW WCF generates async methods since 2012. – Panagiotis Kanavos Nov 13 '17 at 08:51
  • I also face this and manage to resolve, you can refer my answer i wrote this for different problem but that will resolve this problem as well link is given here https://stackoverflow.com/questions/64446120/net-core-3-1-importing-wsdl-errors-how-to-work-around/68560971#68560971 – Rajesh Kumar Bhawsar Jul 28 '21 at 13:10

2 Answers2

9

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.

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();
}
Robert
  • 2,407
  • 1
  • 24
  • 35
  • 2
    `.GetAwaiter().GetResult()` is preferable as it doesn't wrap the exception in AggregateException. But still, this is a very expensive indulgence - nothing gets simplified by blocking, but it *does* cost more in wasted CPU, threads and by extension, servers – Panagiotis Kanavos Nov 13 '17 at 08:51
  • 2
    I agree, hence the other part of the answer. – Robert Nov 13 '17 at 08:53
  • Thank you @Rob ,adding .Result to end of line is works.Is this works is cause any performance problems?I want my code work with performance so async or without async is not problem.I just want my code works with good performance – user1688401 Nov 13 '17 at 10:19
  • 2
    @user1688401 Synchronous IO is prone to thread pool starvation issues - since the IIS maximum parallel request limit does may not apply to ASP.NET Core, lots of threads blocking on `.Result` or similar (or non-async variants of other APIs) can cause needless performance bottlenecks. In extreme cases, it can lead to deadlock situations because of thread pool starvation ([real-world exampe](https://github.com/aspnet/IISIntegration/issues/245#issuecomment-253668589)) – Martin Ullrich Nov 13 '17 at 18:00
  • I agree Async does improves performance significantly, but my app is doing synch operation can't continue without response. – Jeb50 Sep 07 '20 at 16:59
8

There is a way to generate synchronous methods in your .NET core project in Visual Studio 2019.

Wizard that adds WCF web service reference to your .NET core project has an option Generate Synchronous Operations in the third step, Client Options:

enter image description here

Make sure you check it as it is unchecked by default.

Nikita Barsukov
  • 2,957
  • 3
  • 32
  • 39
  • 1
    It seems it's not a true sync method AFAIK. The details are linked in another question [here](https://stackoverflow.com/questions/46713881/net-core-wcf-connected-services-synchronous-methods) including a discussion in .net core repository [here](https://github.com/Azure/azure-storage-net/issues/367). – Beytan Kurt Apr 03 '20 at 20:55
  • 1
    The only useful answer here. I was porting .Net Framework code to .Net Core, and missed this operation which would have resulted in a TON of refactoring since, for some reason, the synchronous and async methods have different return types each containing different members. Thanks. – CodeWarrior Jun 11 '20 at 09:45
  • "Generate Synchronous Operations" is now not available. Only "Public" and "Internal" are available. – Jeb50 Sep 07 '20 at 16:56
  • This is the answer! Thank you. – agleno Jun 15 '21 at 19:07