how do u call async webmethod from javascript!??!!

Discussion in 'ASP .Net' started by jojoba@gmail.com, Dec 12, 2007.

  1. Guest

    Hello to all!

    I have a fairly simple webservice running in asp.net ajax under c# (vs
    2008).
    I built the service and it runs just dandy when i test it by itself in
    visual studio.
    However, to make it work, i had to break up the webmethod into the
    BeginXXX, EndXXX model
    as per http://msdn2.microsoft.com/en-us/library/aa480516.aspx
    But now, i can no longer call this method from javascript (since to
    use the BeginXXX, EndXXX, you must remove the [WebMethod] from the
    orig method)
    Specifically, in javascript i originally would be able to call the
    service via
    "WebService_speech.GenerateSpeechDataForText"
    But after adding in the BeginXXX, and EndXXX, i no longer know how to
    call the service from javascript
    I feel like i'm so close, but i just dont know how to call my service.
    Any ideas?

    here's the code snippet from the service

    WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.Web.Script.Services.ScriptService]
    public class WebService_speech : System.Web.Services.WebService
    {
    public System.Speech.Synthesis.SpeechSynthesizer TtsVoice;

    public WebService_speech()
    {
    //Uncomment the following line if using designed components
    //InitializeComponent();
    }

    public delegate string GenerateSpeechDataForTextAsyncStub(string
    text);

    //meat of the method (heavy lifting done here)
    //where the actual speech data is extracted from the text
    public SpeechData GenerateSpeechDataForText(string text)
    {
    //do speech stuff here
    //i removed the processing code
    return SpeechData_;
    }

    //state monitor for the GenerateSpeechDataForText service
    public class MyState
    {
    public object previousState;
    public GenerateSpeechDataForTextAsyncStub asyncStub;
    }

    //start the GenerateSpeechDataForText method
    [WebMethod]
    public IAsyncResult BeginGenerateSpeechDataForText(string text,
    AsyncCallback cb, object s)
    {
    GenerateSpeechDataForTextAsyncStub stub = new
    GenerateSpeechDataForTextAsyncStub(GenerateSpeechDataForText);
    MyState ms = new MyState();
    ms.previousState = s;
    ms.asyncStub = stub;
    return stub.BeginInvoke(text, cb, ms);
    }

    //end the GenerateSpeechDataForText method
    [WebMethod]
    public SpeechData EndGenerateSpeechDataForText(IAsyncResult call)
    {
    MyState ms = (MyState)call.AsyncState;
    return ms.asyncStub.EndInvoke(call);
    }
    }




    Thx a million,
    matt
     
    , Dec 12, 2007
    #1
    1. Advertising

  2. bruce barker Guest

    the async webmethod is only used from .net programs. with .net you use a
    proxy stub that is generated to call the webservice. the aync methods
    are part of the stub (that runs in the caller), and allow specification
    of a completion delegate.

    javascript calls the webservice directly (socket to port) and is always
    async (the a in ajax). when javascript calls a webservice, you always
    supply a completion delegate.

    so to do async from javascript you call the standard webservice method.

    -- bruce (sqlwork.com)

    wrote:
    > Hello to all!
    >
    > I have a fairly simple webservice running in asp.net ajax under c# (vs
    > 2008).
    > I built the service and it runs just dandy when i test it by itself in
    > visual studio.
    > However, to make it work, i had to break up the webmethod into the
    > BeginXXX, EndXXX model
    > as per http://msdn2.microsoft.com/en-us/library/aa480516.aspx
    > But now, i can no longer call this method from javascript (since to
    > use the BeginXXX, EndXXX, you must remove the [WebMethod] from the
    > orig method)
    > Specifically, in javascript i originally would be able to call the
    > service via
    > "WebService_speech.GenerateSpeechDataForText"
    > But after adding in the BeginXXX, and EndXXX, i no longer know how to
    > call the service from javascript
    > I feel like i'm so close, but i just dont know how to call my service.
    > Any ideas?
    >
    > here's the code snippet from the service
    >
    > WebService(Namespace = "http://tempuri.org/")]
    > [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    > [System.Web.Script.Services.ScriptService]
    > public class WebService_speech : System.Web.Services.WebService
    > {
    > public System.Speech.Synthesis.SpeechSynthesizer TtsVoice;
    >
    > public WebService_speech()
    > {
    > //Uncomment the following line if using designed components
    > //InitializeComponent();
    > }
    >
    > public delegate string GenerateSpeechDataForTextAsyncStub(string
    > text);
    >
    > //meat of the method (heavy lifting done here)
    > //where the actual speech data is extracted from the text
    > public SpeechData GenerateSpeechDataForText(string text)
    > {
    > //do speech stuff here
    > //i removed the processing code
    > return SpeechData_;
    > }
    >
    > //state monitor for the GenerateSpeechDataForText service
    > public class MyState
    > {
    > public object previousState;
    > public GenerateSpeechDataForTextAsyncStub asyncStub;
    > }
    >
    > //start the GenerateSpeechDataForText method
    > [WebMethod]
    > public IAsyncResult BeginGenerateSpeechDataForText(string text,
    > AsyncCallback cb, object s)
    > {
    > GenerateSpeechDataForTextAsyncStub stub = new
    > GenerateSpeechDataForTextAsyncStub(GenerateSpeechDataForText);
    > MyState ms = new MyState();
    > ms.previousState = s;
    > ms.asyncStub = stub;
    > return stub.BeginInvoke(text, cb, ms);
    > }
    >
    > //end the GenerateSpeechDataForText method
    > [WebMethod]
    > public SpeechData EndGenerateSpeechDataForText(IAsyncResult call)
    > {
    > MyState ms = (MyState)call.AsyncState;
    > return ms.asyncStub.EndInvoke(call);
    > }
    > }
    >
    >
    >
    >
    > Thx a million,
    > matt
     
    bruce barker, Dec 12, 2007
    #2
    1. Advertising

  3. Guest

    hi bruce,
    thank you for responding.

    I apologize for not being clear enough on my original post.
    When i dont use the BeginXXX and EndXXX methodology, i can call the
    webservice from javascript no problem (i.e. the method "
    GenerateSpeechDataForText").
    But when i use the BeginXXX and EndXXX methodology, javascript no
    longer knows of that method name.
    Isnt that bizarre?
    Any thought,
    Thanks,
    matt
     
    , Dec 12, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. germ
    Replies:
    0
    Views:
    1,330
  2. dgk
    Replies:
    1
    Views:
    458
    Fred Chateau
    Aug 3, 2007
  3. Kevin Clements

    Erratic delays in processing webmethod call.

    Kevin Clements, Apr 8, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    146
    Kevin Clements
    Apr 8, 2004
  4. Matt
    Replies:
    1
    Views:
    162
  5. Steven
    Replies:
    0
    Views:
    403
    Steven
    Nov 30, 2005
Loading...

Share This Page