0

How to call web method in c# using async. I'm using like below

    public void SaveSuggestedItems(string emailbody, string OrderHeaderID)
    {
       try{
            ...
        }
        catch{
           ...
        }
    }

and in c# Im using like this

  public async Task<string> saveSuggestedItem()
    {
        OHDWebService OHDService = new OHDWebService();
        var saveSuggestedItem = OHDService.SaveSuggestedItems(eBody.Text, hfdOrderRecordID.Value);
        var suggestedItems = await saveSuggestedItem;
        return suggestedItems;
    }

but it is showing error like cannot assign void to an implicit typed local variable

PK-1825
  • 1,431
  • 19
  • 39
  • You can't try to assing the return value fo a void method like `SaveSuggestedItems` to a variable. This has nothing to do with async/await. `async/await` won't make anything asynchronous automagically either, they make *awaiting* already asynchronous operations easier. Whatever is inside `SaveSuggestedItems` should use asynchronous methods itself – Panagiotis Kanavos Jan 15 '19 at 10:14
  • BTW that's explained in ASP.NET tutorials. You'll probably save a lot of time if you follow a tutorial before starting a new project. Check for example [Tutorial: Create a web API with ASP.NET Core MVC](https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-2.2&tabs=visual-studio) – Panagiotis Kanavos Jan 15 '19 at 10:16

2 Answers2

4

You need to make the method async. Change this:

public void SaveSuggestedItems(string emailbody, string OrderHeaderID)

to this:

public async Task SaveSuggestedItems(string emailbody, string OrderHeaderID)

Now you can await it.

Note: You don't have to (and can't) actually return the Task explicitly. The compiler generates one for you.

Also, consider changing the name to SaveSuggestedItemsAsync().

Finally, make sure there's actually a need to make it async.

John Wu
  • 50,556
  • 8
  • 44
  • 80
  • Thanq, actually "SaveSuggestedItems" method is reside in webservice its return type is void so we cant assign void to local variable, but i want to call that method in C# using asyn, so i created that function "saveSuggestedItem", I want make saveSuggestedItem as async but its giving error. – PK-1825 Jan 15 '19 at 11:36
  • Is `SaveSuggestedItems` code that was generated by adding a service reference? You need to tell Visual Studio to [generate async or task-based methods](https://stackoverflow.com/questions/13777710/how-to-use-visual-studio-generated-async-wcf-calls) as well (in the dialog that is shown when adding the reference). – John Wu Jan 15 '19 at 17:41
2

Just replace:

var saveSuggestedItem = OHDService.SaveSuggestedItems(eBody.Text, hfdOrderRecordID.Value);

to standard calling:

OHDService.SaveSuggestedItems(eBody.Text, hfdOrderRecordID.Value);

Your problem appears because you try to assign void to some variable and your method does not return any type. It's not related to async calling.

Maciej S.
  • 752
  • 10
  • 22