What do all these parameters when calling a WCF web method from Anguilla JavaScript mean?

旧街凉风 提交于 2019-12-08 19:07:05

问题


I have the following (from Tridion PowerTools), which gets a user name from the CoreService when some JavaScript runs.

JavaScript (Anguilla):

PowerTools.Popups.Example.prototype._onbtnGetUserInfoClicked = function () { 
    var onSuccess = Function.getDelegate(this, this._handleUserInfo);
    var onFailure = null;
    var context = null;
    //call function
    PowerTools.Model.Services.Example.GetUserInfo(onSuccess, onFailure, 
                                                  context, false);
}; 

// Delegate function "onSuccess"
PowerTools.Popups.Example.prototype._handleUserInfo = function (response) {
    var p = this.properties;
    $j("#lblUserInfo").text(response.UserName);
};

CoreService side: (C# .svc)

[OperationContract, WebGet(ResponseFormat = WebMessageFormat.Json)]
public ExampleData GetUserInfo()
{
    var coreService = Client.GetCoreService();
    _exampleData = new ExampleData()
    {
        UserName = coreService.GetCurrentUser().Title
    };
    return _exampleData;
}

This sends an asynchronous call:

PowerTools.Model.Services.Example.GetUserInfo(onSuccess, onFailure, context, false)

Whereas this assigns a different function to handle the response:

Function.getDelegate(this, this._handleUserInfo)

But where does onSuccess, onFailure, context, and the Boolean come from in: PowerTools.Model.Services.Example.GetUserInfo(onSuccess, onFailure, context, false)?

This four-parameter signature doesn't match the no-paramater GetUserInfo() in the service code. Why that order and these four?


回答1:


The onSuccess and onFailure are the callback functions that are assigned for handling the response from the WCF service.

Assuming this is code from the PowerTools project, there is an auto-generated JavaScript method that acts as a proxy method for a WCF service (source of the service is here) method called GetUserInfo().

In there you can actually see the call to the CoreService. That should explain to you the mapping of the proxy parameters.

  1. onSuccess is the function to process the response of the WCF service
  2. onFailure is the function to run if the call fails
  3. context is a variable that will be passed back into your callback functions, so you can use it to pass things around.
  4. false is whether the call is synchronous or not

If your WCF service were to take parameters, the generated proxy would form a different signature, something like

PowerTools.Model.Services.Example.GetOtherInfo(param1, param2, onSuccess, 
                                               onFailure, context, false);


来源:https://stackoverflow.com/questions/9385892/what-do-all-these-parameters-when-calling-a-wcf-web-method-from-anguilla-javascr

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