PageMethods quirk

Last post 11-28-2006 4:52 PM by antoine64. 32 replies.

Sort Posts:

  • PageMethods quirk

    11-09-2006, 6:51 PM
    • Member
      57 point Member
    • pseudoname
    • Member since 03-03-2006, 7:50 PM
    • San Francisco
    • Posts 12

    So here's what I found while using PageMethods in Beta 2

    1. A server side method needs the attribute [System.Web.Services.WebMethod]
    2. A server side method should be static or else you'll get a PageMethods is undefined error
    3. A server side method should NOT be defined in code behind but should be in the ASPX page in <script runat="server"> tag.
    4. Here's the quirk. A server side method CANNOT have two parameters with names that differ only in the case of their letters. eg. someParam and SomeParam, even if they are of different types, e.g. string and int This will result in a Sys.ArgumentTypeException with the message Object of type Number cannot be converted to type Function. Parameter name OnSuccess. 
      <script runat="server">
              [System.Web.Services.WebMethod]
              public static string SomeMethod(string someParam, int SomeParam)
              {
                  return "Hello " + someParam + " you are " + SomeParam.ToString() + " years old";
              }
          </script>
        
      <script type="text/javascript" language="javascript">
          function CallMethod()
          {
              var age = 28;
              PageMethods.SomeMethod('MyName',age, OnCallResponse);
          }
          function OnCallResponse(arg)
          {
              alert(arg);
          }
          </script>
       
    5. Change the SomeParam to age and it will work.

    So my question is : Is this a bug, quirk or by design? If a bug or quirk will it be fixed? Sure one NEVER names two parameters the same with just a change in the case but then again, NEVER say NEVER.

    Thanks

  • Re: PageMethods quirk

    11-10-2006, 10:58 AM
    • Member
      172 point Member
    • rac12
    • Member since 11-13-2003, 11:05 PM
    • NJ
    • Posts 48
    I SO MUCH SUPPORT your finding #3. It's so stupid that the server side code won't work in code-behind but inline code would work. What the heck?
  • Re: PageMethods quirk

    11-10-2006, 10:59 AM
    • Member
      172 point Member
    • rac12
    • Member since 11-13-2003, 11:05 PM
    • NJ
    • Posts 48

    To any MSFT moderator:

    Is there any workaround for #3? This is driving me crazy.

  • Re: PageMethods quirk

    11-14-2006, 3:54 PM
    • Member
      10 point Member
    • fsabau
    • Member since 07-05-2005, 8:35 PM
    • Posts 2

    Weird, but I just tried to create a page just like yours and it works even if the static method is in the code behind...

     This is the class:

    public partial class _Default : System.Web.UI.Page 
    {   
        [WebMethod]
        public static string GetServerDate()
        {
            return DateTime.Now.ToString();
        }
    }


    And this is the ASPX:

      

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="~/Default.aspx.cs" Inherits="_Default" %>
    <%@ Import namespace="System.Web.Services"%>
    
    "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">Untitled Page
    "server">
        
    
        "form1" runat="server" method="post">   
            "ScriptManager1" runat="server" />
            
    "button" value="get date" onclick="GetDate();" id="Button1" />
    "date" />
    "text/javascript"> function GetDate() { var div = document.getElementById("date"); PageMethods.GetServerDate(function (result) { div.innerText = result; }); }
     
  • Re: PageMethods quirk

    11-14-2006, 4:04 PM
    • Member
      10 point Member
    • fsabau
    • Member since 07-05-2005, 8:35 PM
    • Posts 2

    Sorry about my last post which is unreadable... Angel 

    Reposted: 

    Weird, but I just tried to create a page just like yours and it works even if the static method is in the code behind...

     This is the class:

    public partial class _Default : System.Web.UI.Page 
    {
    [WebMethod]
    public static string GetServerDate()
    {
    return DateTime.Now.ToString();
    }
    }


    And this is the ASPX:

     

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="~/Default.aspx.cs" Inherits="_Default" %>
    <%@ Import namespace="System.Web.Services"%>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server" method="post">   
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
            <div>
                <input type="button" value="get date" onclick="GetDate();" id="Button1" />
                <div id="date" />
                
            </div>
        </form>    
    </body>
    </html>
    
    <script type="text/javascript">
    function GetDate()
    {
        var div = document.getElementById("date");
        PageMethods.GetServerDate(function (result)
        {
            div.innerText = result;
        });
    }
    </script>
    
      
  • Re: PageMethods quirk

    11-14-2006, 4:45 PM
    • Participant
      959 point Participant
    • HaoK
    • Member since 07-18-2003, 2:06 PM
    • Posts 185
    • AspNetTeam

    This looks a bug, thanks for reporting this issue, and it will be fixed in a future build.
    -Hao

  • Re: PageMethods quirk

    11-14-2006, 5:30 PM
    • Member
      30 point Member
    • Advance
    • Member since 07-25-2006, 3:11 PM
    • Milano - Italy
    • Posts 6

    Hi,

    that is not a bug. Remember that vbscript is case unsensitive.

    Roberto Paolo Pellegrinelli
    roby@adivi.biz
    www.adivi.biz
  • Re: PageMethods quirk

    11-15-2006, 9:24 AM
    • Member
      10 point Member
    • Anil Jaswal
    • Member since 09-12-2006, 7:46 PM
    • Posts 2

    Hi,

    I tried the approach of using static methods for PageMethods in AJAX v1.0 Beta 2, but it is not working. The only change that happened is that the error message has changed from "PageMethods is undefined" to "PageMethods is null or  not a object.

     PLEASE HELP !!!!

  • Re: PageMethods quirk

    11-15-2006, 11:18 AM
    • Member
      172 point Member
    • rac12
    • Member since 11-13-2003, 11:05 PM
    • NJ
    • Posts 48

    Advance,

    Where the heck is "vbscript" in whole picture? We are trying to access a WebMethod from Javascript using MS Ajax Beta 2 in the code-behind file.

  • Re: PageMethods quirk

    11-15-2006, 3:59 PM
    • Member
      35 point Member
    • JMichael2468
    • Member since 11-01-2006, 2:28 PM
    • Posts 9

    I was hoping this would have been corrected in Beta 2.  I guess I'll keep using the ATLAS CTP until this is resolved.  Oh well, maybe in build 3??????

  • Re: PageMethods quirk

    11-15-2006, 4:55 PM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 1:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    • TrustedFriends-MVPs

    hello.,

    this is correct. the problem is related with the EnsureParameters of the WebServiceMethodData class. I don't know why, but it's "eating parameters" when they have the same name and differ only in case. here's the "bad" code:

    private void EnsureParameters()
    {
    if (this._parameterData == null)
    {
    lock (this)
    {
    Dictionary<string, WebServiceParameterData> dictionary1 = new Dictionary<string, WebServiceParameterData>(StringComparer.OrdinalIgnoreCase);
    int num1 = 0;
    foreach (ParameterInfo info1 in this._methodInfo.GetParameters())
    {
    dictionary1[info1.Name] = new WebServiceParameterData(info1, num1);
    num1++;
    }
    this._parameterData = dictionary1;
    }
    }
    }
     
    I've tried reproducing this with a simple method and i get the 2 parameters and if i add them to a dictionary, i get 2 entries. this is not happening here (yes, i've used reeflection to reproduce what's going on  and i can say that the dic only has one parameter.
    if anyone have an idea on why is that, please,share with us!
    thanks. 

     

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: PageMethods quirk

    11-15-2006, 5:01 PM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 1:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    • TrustedFriends-MVPs

     

    hi.

    dom't you hate it when you spend 1 hour looking at code and writing reflection just to see that  the damn thing is right in front of you?:

    new Dictionary<string, WebServiceParameterData>(StringComparer.OrdinalIgnoreCase);
    thanks Garbin!
     
     
    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: PageMethods quirk

    11-15-2006, 5:02 PM
    • Participant
      959 point Participant
    • HaoK
    • Member since 07-18-2003, 2:06 PM
    • Posts 185
    • AspNetTeam

    Yeah, there are two places in the code that are using OrdinalIgnoreCase comparers which is what's causing the problem, removing the IgnoreCase for both dictionaries fixes the problem.

    Hope that helps,
    -Hao

  • Re: PageMethods quirk

    11-15-2006, 5:18 PM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 1:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    • TrustedFriends-MVPs

    hello.

    does that mena that it'll be removed in the next release? 

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: PageMethods quirk

    11-15-2006, 5:24 PM
    • Participant
      959 point Participant
    • HaoK
    • Member since 07-18-2003, 2:06 PM
    • Posts 185
    • AspNetTeam

    Yes I've already fixed it in the codebase.

    Hope that helps,
    -Hao

Page 1 of 3 (33 items) 1 2 3 Next >