0

## How to call an async function inside web service method in c# ##

Here is my sample code and I want to call btndisplayword_Click from DisplayNextWord and DisplayNextWord is async so that won't work

[WebMethod]
    public static void DisplayNextWord(int id)
    {

        ///How to call below async function inside webservice Here

    }


protected async void btndisplayword_Click(object sender, EventArgs e)
    {


    }
Pouya Samie
  • 3,718
  • 1
  • 21
  • 34
Princy
  • 11
  • 7
  • 1
    The same way you would call it from any syncronous method. Have you checked if is already answered? First search gives me a duplicated https://stackoverflow.com/questions/9343594/how-to-call-asynchronous-method-from-synchronous-method-in-c – Cleptus May 07 '18 at 06:38
  • Possible duplicate of [Is there some way to handle async/await behind an ASMX service?](https://stackoverflow.com/questions/18622372/is-there-some-way-to-handle-async-await-behind-an-asmx-service) – sɐunıɔןɐqɐp May 07 '18 at 06:57
  • add some words to make it clear – Pouya Samie May 07 '18 at 10:02
  • 1
    @Princy are you limited to ASMX or can go to WCF or WebAPI? ASMX doesn't supports TPL by design, and the hacks required may lead to clumsy code – NitinSingh May 07 '18 at 13:51

2 Answers2

0

You cannot call the non-static function into a static function. Either make your click event static or DisplayNextWord method non-static.

    public void DisplayNextWord(int id)
    {
        btndisplayword_Click(null, null);
    }

    protected async void btndisplayword_Click(object sender, EventArgs e)
    {


    }
Monis Azhar
  • 1
  • 1
  • 3
  • A webmethod when its a part of ASPX code behind will need to be a static method. However if its part of a webservice method, it can be an instance one. Here, its a part of aspx (has a button click handler) and the handler itself need to be instance one to allow the event to bind – NitinSingh May 07 '18 at 13:54
-1

This code works...... ####

 [WebMethod]
            public static void DisplayWord(int id)
            {

                SpellingBee_spellbyte Sg = new SpellingBee_spellbyte();//pageclassname
                Task.Run(async () => await Sg.getstartWord(id));


            }


  protected async Task getstartWord(int tagId)
        {
         }
Princy
  • 11
  • 7