SYSK 286: What Does a Web Service Proxy Look Like on a Client Side?

AJAX.NET makes it extremely simple to call a web service from a client using JavaScript… But if you’re having problems, it helps to understand what your web service proxy looks like…

 

For example, if you have the following web service:

using System;

using System.Web;

using System.Collections;

using System.Web.Services;

using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.Web.Script.Services.ScriptService]

[System.Web.Script.Services.GenerateScriptType(typeof(MethodXResult))]

public class MyService : System.Web.Services.WebService {

    public MyService ()

    {

       

    }

    [WebMethod]

    public MethodXResult MethodX(string param1)

    {

        return null;

    }

   

}

[System.Runtime.Serialization.DataContract]

public class MethodXResult

{

    private string _someProperty;

    [System.Runtime.Serialization.DataMember] public string SomeProperty

    {

        get { return _someProperty; }

        set { _someProperty = value; }

    }

}

 

then the client side JavaScript proxy would look something like this:

 

var MyService=function() {

MyService.initializeBase(this);

this._timeout = 0;

this._userContext = null;

this._succeeded = null;

this._failed = null;

}

MyService.prototype={

MethodX:function(param1,succeededCallback, failedCallback, userContext) {

return this._invoke(MyService.get_path(), 'MethodX',false,{param1:param1},succeededCallback,failedCallback,userContext); }}

MyService.registerClass('MyService',Sys.Net.WebServiceProxy);

MyService._staticInstance = new MyService();

MyService.set_path = function(value) { MyService._staticInstance._path = value; }

MyService.get_path = function() { return MyService._staticInstance._path; }

MyService.set_timeout = function(value) { MyService._staticInstance._timeout = value; }

MyService.get_timeout = function() { return MyService._staticInstance._timeout; }

MyService.set_defaultUserContext = function(value) { MyService._staticInstance._userContext = value; }

MyService.get_defaultUserContext = function() { return MyService._staticInstance._userContext; }

MyService.set_defaultSucceededCallback = function(value) { MyService._staticInstance._succeeded = value; }

MyService.get_defaultSucceededCallback = function() { return MyService._staticInstance._succeeded; }

MyService.set_defaultFailedCallback = function(value) { MyService._staticInstance._failed = value; }

MyService.get_defaultFailedCallback = function() { return MyService._staticInstance._failed; }

MyService.set_path("/WebSite/MyService.asmx");

MyService.MethodX= function(param1,onSuccess,onFailed,userContext) {MyService._staticInstance.MethodX(param1,onSuccess,onFailed,userContext); }

var gtc = Sys.Net.WebServiceProxy._generateTypedConstructor;

if (typeof(MethodXResult) === 'undefined') {

var MethodXResult=gtc("MethodXResult");

MethodXResult.registerClass('MethodXResult');

}

 

 

To find out what your web service proxy looks like, navigate to your web service .asmx file followed by /js. For example, http://localhost:56238/WebSite/MyService.asmx/js

 

You should get a File Download prompt… Choose Save option, type in the file name, press Save button. Then, use your favorite text editor to open and view contents of the saved file containing the JavaScript proxy generated for your web service…