Calling a C# webservice from the Success function of another Webservice?

拈花ヽ惹草 提交于 2020-01-17 07:53:09

问题


My question is simple. Can you call a C# webservice from the Success function of another webservice? I think that the answer is no.

I have a login webservice which is called when a user tries to do something that requires the user to be logged in. This then calls another webservice, when the action is complete it does not go into the onSuccess function. I am assuming that it is not able to do so? Is this correct?

Example code, not actual code.

webservice.login(onLoginSuccess)

function onLoginSuccess(){

    webservice.doSomething(onDoSomethingSuccess)

}

function onDoSomethingSuccess(){

    .... does not get here.

}

This is the code to it's bare bones.

On client JavaScript call, success of login webservice.

function onLoginSuccess(result){


    negotiateWithUser(true,
                      activeUser.id,
                      negotiation.dateID,
                      activeUser.showChat,
                      true);
}


function negotiateWithUser() {

    <code>

    if (justLoggedIn) updateDateData();

    <code>
}

 function updateDateData(){
     if (populate==false){

         populate=true;       
        WebService.Negotiations.DatesGet(activeUser.id,SucceededtoCallbackDateRefresh, ErrorHandler);          
     }
}

Does not go into the function SucceededtoCallbackDateRefresh even though data is returned and there are no errors.

I think I know what the problem is, in the negotiateWithUser function the updateDateData is called, execution control is given back to the negotiateWithUser function instead of going into the SucceededtoCallbackDateRefresh success funtion, as it is an asynchronous call. I think some kind of callback function is required.

I have got round the problem by calling the updataDateData function in a setInterval() in the onLoginSuccess function instead.

 function onLoginSuccess(result){ 

      if (negotiate) {              
           timerInterval = setInterval("updateDateData()", 300);
      }

 }

It works. If someone has a more graceful approach, I would be very interested.
Thanks.


回答1:


How about to use jQuery.ajax()?

 function Func1() {
    $.ajax({
        url:'MyWebService.asmx/Func1',
        success:Func1Success,
        error: function () {
            alert('there was an error!');
        },
    });
    return false;
}
function Func1Success(output){
  $.ajax({
        url:'MyWebService.asmx/Func2',
        success:Func1SuccessToSuccess,
        error: function () {
            alert('there was an error!');
        },
    });
}
function Func1SuccessToSuccess() {
    return false;
}



回答2:


You definitely can.

Just to give a suggestion/advice theoretically, you can have 1 local variable for the status response of the first web service method, you can have it as boolean, representing the success of the first invocation. Then sequentially, you can call the other web service method if your local variable matches your success condition.

What I provided is just an idea, for a more specific answer, I recommend you posting the actual code you're working on.

Thanks.




回答3:


I am assuming you are using .cs files on your server and then calling them from android. If this is what you are doing then there is one way to call a method on success of another method. Just make two .cs files say x and y and store it on your server. Then call one of them from android (x) and then make object of y in x and that will be all. for eg. This is my one .cs file named abc.cs which il call from android.

 [WebMethod]
 public xyz IsLoggedIn()
 {
    xyz example = new xyz();
    //do something
    .
    .

    return example;
  }

now xyz.cs will be:

[WebMethod]
 public void onSuccessofIsLoggedIn()
 {
    //do something
    .
    .
  }

i hope this helps.... All this is just based on assumption though...please be clear about what you are using and we will be also more clear in our answers.



来源:https://stackoverflow.com/questions/40381194/calling-a-c-sharp-webservice-from-the-success-function-of-another-webservice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!