ASP.NET Web Method that accepts a List<CustomObject> is failing with "Web Service method name is not valid."

12,295

Solution 1

I make the assuption based on comments that you can directly go to the web service in the browser.

Just to isolate your custom object from configuration, you could put another service in place like:

[WebMethod]
public static string GetServerTimeString()
{
    return "Current Server Time: " + DateTime.Now.ToString();
}

Call that from a client side jQuery ajax call. If this works, then it is probably related to your object specifically and not configuration on the server side. Otherwise, keep looking on the server side config track.

EDIT: Some sample code:

[WebMethod(EnableSession = true)]
public Category[] GetCategoryList()
{
    return GetCategories();
}
private Category[] GetCategories()
{
     List<Category> category = new List<Category>();
     CategoryCollection matchingCategories = CategoryList.GetCategoryList();
     foreach (Category CategoryRow in matchingCategories)
    {
         category.Add(new Category(CategoryRow.CategoryId, CategoryRow.CategoryName));
    }
    return category.ToArray();
}

And here is an example of where I post a complex data type JSON value

[WebMethod]
 public static string SaveProcedureList(NewProcedureData procedureSaveData)
 {
          ...do stuff here with my object
 }

This actually includes two arrays of objects inside it... my NewProcedureData type is defined in a class which lays those out.

EDIT2:

Here is how I handle a complex object in one instance:

function cptRow(cptCode, cptCodeText, rowIndex)
{
    this.cptCode = cptCode;
    this.cptCodeText = cptCodeText;
    this.modifierList = new Array();
//...more stuff here you get the idea
}
/* set up the save object */
function procedureSet()
{
    this.provider = $('select#providerSelect option:selected').val(); // currentPageDoctor;
    this.patientIdtdb = currentPatientIdtdb;// a javascript object (string)
//...more object build stuff.
    this.cptRows = Array();
    for (i = 0; i < currentRowCount; i++)
    {
        if ($('.cptIcdLinkRow').eq(i).find('.cptEntryArea').val() != watermarkText)
        {
            this.cptRows[i] = new cptRow($('.cptIcdLinkRow').eq(i).find('.cptCode').val(), $('.cptIcdLinkRow').eq(i).find('.cptEntryArea').val(), i);//this is a javscript function that handles the array object
        };
    };
};
//here is and example where I wrap up the object
    function SaveCurrentProcedures()
    {

        var currentSet = new procedureSet();
        var procedureData = ""; 
        var testData = { procedureSaveData: currentSet };
        procedureData = JSON.stringify(testData);

        SaveProceduresData(procedureData);
    };
    function SaveProceduresData(procedureSaveData)
    {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: procedureSaveData,
the rest of the ajax call...
        });
    };

NOTE !IMPORTANT the procedureSaveData name must match exactly on the client and server side for this to work properly. EDIT3: more code example:

using System;
using System.Collections.Generic;
using System.Web;

namespace MyNamespace.NewProcedure.BL
{
    /// <summary>
    /// lists of objects, names must match the JavaScript names
    /// </summary>
    public class NewProcedureData
    {
        private string _patientId = "";
        private string _patientIdTdb = "";

        private List<CptRows> _cptRows = new List<CptRows>();

        public NewProcedureData()
        {
        }

        public string PatientIdTdb
        {
            get { return _patientIdTdb; }
            set { _patientIdTdb = value; }
        }
       public string PatientId
        {
            get { return _patientId; }
            set { _patientId = value; }
        }
        public List<CptRows> CptRows = new List<CptRows>();

}

--------
using System;
using System.Collections.Generic;
using System.Web;

namespace MyNamespace.NewProcedure.BL
{
    /// <summary>
    /// lists of objects, names must match the JavaScript names
    /// </summary>
    public class CptRows
    {
        private string _cptCode = "";
        private string _cptCodeText = "";

        public CptRows()
        {
        }

        public string CptCode
        {
            get { return _cptCode; }
            set { _cptCode = value; }
        }

        public string CptCodeText
        {
            get { return _cptCodeText; }
            set { _cptCodeText = value; }
        }
     }
}

Hope this helps.

Solution 2

Since the error mentions a problem with the web method name, I suspect this is entirely due to problems in how the WSDL is generated and mapped to the web service. I see two potential problems:

  1. ID is an awfully common name and may be reserved or may cause conflicts. Try renaming it to objectId (capitalized parameter names are not the norm for .NET anyway)
  2. WSDL does not have a concept of generic types. Depending on the generator, the type name generated for List<CustomObject> or CustomObject[] may conflict with your parameter name CustomObjectList. Try renaming your second parameter customObjects instead.

Or maybe there's some other silly framework issues at play here. In any case, you should add to your question the WSDL generated by .NET. You can get the WSDL XML by downloading /manageobjects.asmx?WSDL from your web server.

Share:
12,295
Bara
Author by

Bara

Updated on June 16, 2022

Comments

  • Bara
    Bara almost 2 years

    I want to create a web method that accepts a List of custom objects (passed in via jQuery/JSON).

    When I run the website locally everything seems to work. jQuery and ASP.NET and everyone is happy. But when I put it on one of our servers it blows up. jQuery gets a 500 error after the ajax request with the response being:

    System.InvalidOperationException: EditCustomObjects Web Service method name is not valid.

    Here's the web service method:

    [WebMethod]
    public void EditCustomObjects(int ID, List<CustomObject> CustomObjectList)
    {
      // Code here
    }
    

    And my jQuery code (which I don't think matters, since the error seems to be happening on the web service level):

    var data = JSON.stringify({
      ID: id,
      CustomObjectList: customObjectList
    });
    
    $.ajax({
      type: "POST",
      url: "/manageobjects.asmx/EditCustomObjects",
      data: data,
      contentType: "application/json; charset=utf-8",
      async: false,
      dataType: "json",
      success: function(xml, ajaxStatus) {
        // stuff here
      }
    });
    

    The customObjectList is initialized like so:

    var customObjectList = [];
    

    And I add items to it like so (via a loop):

    var itemObject = { 
      ObjectTitle = objectTitle,
      ObjectDescription = objectDescription,
      ObjectValue = objectValue
    }
    
    customObjectList.push(itemObject);
    

    So, am I doing anything wrong here? Is there a better way of passing an array of data from jQuery to an ASP.NET web service method? Is there a way to resolve the "Web Service method name is not valid." error?

    FYI, I am running .NET 2.0 on a Windows Server 2003 machine, and I got the code for the above from this site: http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/

    EDIT: Someone requested some more info on the web service, I'd rather not provide the whole class but here is a bit more that may help:

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService] 
    public class ManageObjects : Custom.Web.UI.Services.Service 
    {
    }
    

    Bara

  • Bara
    Bara about 14 years
    I already have both HttpPost and HttpGet in my Web.Config. I still get the same error when hitting the URL of the web service.
  • Bara
    Bara about 14 years
    Unfortunately, locally I am running IIS7 while the server is IIS6, so this kind of check isn't very practical or perhaps even possible. I guess I could try putting the site on a different server.
  • Bara
    Bara about 14 years
    Other methods in the service work fine (via AJAX and URL). However, your previous assumption is incorrect. When I visit the website via the URL, I get the error mentioned in my initial post, regardless of whether pass in data or not. Firebug tells me the same error after submission of an AJAX request.
  • Bara
    Bara about 14 years
    Here is what I have in my web.config: <add path="/ws/*.asmx" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>. Should I remove the "/ws/" part of it?
  • zincorp
    zincorp about 14 years
    Yes, remove the /ws/, unless your service is located at the path /ws/manageobjects.asmx
  • Fermin
    Fermin about 14 years
    Do you have any other web services running on the IIS6 machine? It could be possible that the issue could be related to the differing versions of IIS. If possible I'd try to get it on an IIS7 server.
  • Mark Schultheiss
    Mark Schultheiss about 14 years
    Great, so then we know it has to do with the actual data and not a generic web service/config issue. SO, we then need to serialize the data structure. I have not used the structure you have, but have used array and complex types that I have defined in a class...
  • Mark Schultheiss
    Mark Schultheiss about 14 years
    Is it possible to used an array of CustomObject instead of the List as you have described?
  • Mark Schultheiss
    Mark Schultheiss about 14 years
    For instance, I have used the MyObjectList.ToArray(); with a method like: public MyObject [] GetMyObjectList(){....return MyObjectList.ToArray();}
  • Bara
    Bara about 14 years
    Yes, that's correct. I wrote this code from memory, but I know what the right method is. Regardless, the problem isn't from the javascript, it's from the asp.net web method. As stated before, just visiting the URL for the method gives me the error mentioned in the topic.
  • Bara
    Bara about 14 years
    We're planning on moving the site in the future, but for the moment it has to stay where it is. I'm not sure of any other web services on that machine, but I'm leaning towards not.
  • Bara
    Bara about 14 years
    I removed the /ws/, but unfortunately that did not help.
  • Bara
    Bara about 14 years
    I tried passing in an array instead of a List: public void EditCustomObjects(int ID, CustomObject[] CustomObjectList) but got the same error. I created a new class that contains an array of CustomObject, then set the EditCustomObjects method to accept an object of that class, but I got the same error yet again. However, I'm not sure I completely understood what your suggestion is.. What exactly is contained in NewProcedureData?
  • Jeff Sternal
    Jeff Sternal about 14 years
    @Bara - have you confirmed that the URL the ajax request produces exists?
  • Mark Schultheiss
    Mark Schultheiss about 14 years
    For simplicity, I ussually just add a class with that name. I will paste a simple example, but basically you mimic the client side object as I have listed above with a class.
  • Mark Schultheiss
    Mark Schultheiss about 14 years
    NOTE: I am actually passing a complex object NewProcedureData in that instance, and not an array - it just contains an array. I cut lots of the code out to make it simpler as it is a large class with sub classes (arrays of objects)
  • Mark Schultheiss
    Mark Schultheiss about 14 years
    It just needs to be able to deserialize your object basically.
  • Bara
    Bara about 14 years
    Yes, the URL absolutely exists.
  • Bara
    Bara about 14 years
    Tried this before, did not work. Got the same error. Also tried having an array instead of a list inside of ObjectData with no luck. I don't have ASP.NET ajax extensions installed however. I'm not sure if I want to install them just for this one problem.
  • Bara
    Bara about 14 years
    I've removed ID and renamed my list, but it did not resolve the problem. I'll take a look at the WSDL next chance I get (tomorrow morning).
  • Jacob
    Jacob about 14 years
    Great. Also, try posting the CustomObject class definition as well.