1

I am trying to understand how the following snippet of code works. The person who designed it is not around to explain to me how its working (also why he/she didn't use ajax calls). I've asked few co-workers and they seem to not have the proper explanation for me to understand how the JavaScript was able to access webmethod directly. I am hoping my example is good enough to at least get a reasonable explanation since I can't post the original code.

Web Service

namespace ClientName.Version.Services
{
   [WebService(Namespace = "http://tempuri.org/")]
   [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
   [System.ComponentModel.ToolboxItem(false)]
   [System.Web.Script.Services.ScriptService]
   public class ClassName : System.Web.Service.WebService
   {
      [WebMethod(EnableSession = true)]
      public List<ReturnData> WebMethod(string param1)
      {
            .
            .
            .
      }
    }
}

Javscript Call

(function(){
    var param = "Broken down calls";
    .
    .
    .


    ClientName.Version.Services.ClassName.WebMethod(
        param, function(dataReturnedFromService){
            if(dataReturnedFromService != null){

                //process data and append to html

            }

        }   
    );


})();

Edited: After @matt pointed me to the right spot, on top of his response if anyone cares to now more http://msdn.microsoft.com/en-us/library/bb398998(v=vs.100).aspx

or google

"Exposing Web Services to Client Script" for more.

0

2 Answers 2

2

What's probably going on is this:

http://msdn.microsoft.com/en-us/library/bb310823(v=vs.100).aspx

You are generating a proxy class on the client side that hides the AJAX calls behind an interface that looks almost the same as on the server side.

If you watch the network tab in your browser's console, you'll probably see the actual AJAX calls.

A simple search on MSDN for ScriptServiceAttribute should have pointed you in the right direction:

To invoke a Web service method from ECMAScript (JavaScript), you must apply the ScriptServiceAttribute attribute to the related Web service class. When you apply ScriptServiceAttribute to a Web service class definition that contains one or more methods with WebMethodAttribute applied, the proxy generation script creates a proxy object that corresponds to the Web service class.

Look at your pages, you should have a section that looks something like this:

<asp:ScriptManager runat="server" ID="scriptManager">
  <Services>
    <asp:ServiceReference
       path="~/WebServices/SimpleWebService.asmx" />
  </Services>
</asp:ScriptManager>

That's where the proxy gets added.

Sign up to request clarification or add additional context in comments.

2 Comments

there is another project where we keep the proxies generated using wsdl.exe and we access them differently by instantiating soaphttpclientprotocol (server side) or using ajax calls (client side) But in this case, there is no proxies for the web service class
Look at the actual rendered page in your browser. I believe that ASP generates the javascript proxy automatically and injects it into your page (or rather adds a reference to a file with the proxy) if you include a ScriptManager in your page.
0

I am currently maintaining an old project and what they did was they created a service class like your example

namespace ClientName.Version.Services
{
   [WebService(Namespace = "http://tempuri.org/")]
   [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
   [System.ComponentModel.ToolboxItem(false)]
   [System.Web.Script.Services.ScriptService]
   public class ClassName : System.Web.Service.WebService
   {
      [WebMethod(EnableSession = true)]
      public List<ReturnData> WebMethod(string param1)
      {
            .
            .
            .
      }
    }
}

Then the asmx file had a reference to it.

<%@ WebService Language="C#" Class="ClientName.Version.Services.ClassName" %>

While the scriptmanager tag location on the aspx or the master page

<asp:ScriptManager ID="ScriptManager1" runat="server" AsyncPostBackTimeout="3600"
</asp:ScriptManager>

adds the service through the code behind.

ScriptManager1.Services.Add(new ServiceReference("~/WebServices/Service.asmx"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.