Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Service method name is not valid

I get the following error "Web Service method name is not valid" when i try to call webmethod from javascript

System.InvalidOperationException: SaveBOAT Web Service method name is not valid. at System.Web.Services.Protocols.HttpServerProtocol.Initialize() at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response) at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)

HTML Code :

<asp:LinkButton runat="server" ID="lnkAddBoat" OnClientClick="javascript:AddMyBoat(); return false;"></asp:LinkButton>

JS Code :

function AddMyBoat() {
            var b = document.getElementById('HdnControlId').value;

            jQuery.ajax({
                type: "GET",
                url: "/AllService.asmx/SaveBOAT",
                data: { Pid: b },
                contentType: "application/text",
                dataType: "text",
                success: function(dd) {
                    alert('Success' + dd);
                },
                error: function(dd) {
                    alert('There is error' + dd.responseText);
                }
            });
}

C# Code (Web method in AllService.asmx file)

[WebMethod]
public static string SaveBOAT(int Pid)
{
    // My Code is here
    //I can put anythng here
    SessionManager.MemberID = Pid;
    return "";
}

I tried all solutions found on Stack Overflow and ASP.NET site.but none of them worked for me.

like image 491
ravidev Avatar asked Aug 09 '12 06:08

ravidev


2 Answers

In my case I had copied another asmx file, but not changed the class property to the name of the new class in the asmx file itself (Right click on asmx file -> View Markup)

like image 110
Matthew Lock Avatar answered Oct 06 '22 03:10

Matthew Lock


As Sundar Rajan states, check the parameters are also correct. My instance of this error was because I had failed to pass any parameters (as a body in a POST request) and the asmx web method was expecting a named parameter, because of this the binding logic failed to match up the request to the method name, even though the name itself is actually correct.

[WebMethod]
        public object MyWebMethod(object parameter)

If there is no parameter in the body of the request then you will get this error.

like image 41
Wilco Avatar answered Oct 06 '22 03:10

Wilco