ASP.NET Web Method that accepts a List is failing with “Web Service method name is not valid.”

后端 未结 10 763
感动是毒
感动是毒 2020-12-10 07:25

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 an

相关标签:
10条回答
  • 2020-12-10 07:37

    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.

    0 讨论(0)
  • 2020-12-10 07:40

    You may want to check your webServices/protocols element in the system.web section and ensure that HttpPost is added. Typically, local post is enabled by default as development mode but not for remote, which is what it will be when the service is deployed on the server...

    <system.web>
     ...
     <webServices>
      <protocols>
       <add name="HttpPost" />
      </protocols>
     </webServices>
    </system.web>
    

    I go into more detail here

    0 讨论(0)
  • 2020-12-10 07:40

    Do you have the required httpHandler registered for allowing webservices marked as a ScriptService?

    <httpHandlers>
       <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </httpHandlers>
    
    0 讨论(0)
  • 2020-12-10 07:40

    Ensure you have this at the top of your class

    [ScriptService] <--- this bit :D
    
    public class Surveyors : WebService
    

    Also check all of your parathesis, resharper went a bit crazy on me and it took a while to check through.

    Also if you have many webmethods, try to make a basic hello world webservice, make sure its actually working in the first place then you can troubleshoot from there.

    Good Luck.

    0 讨论(0)
  • 2020-12-10 07:51

    Whenever I get these kinds of errors (working on local but not on server) I fire up IIS admin tool (or whatever you are using) and check that everything is EXACTLY the same. If not make them the same then change 1 little thing at a time.

    Depending on your role/organisation you might not have access to the server in this way but often these errors come down to the weirdest random checkbox on a tab you never look at.

    It is a long shot but can't help to try.

    0 讨论(0)
  • 2020-12-10 07:54

    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.

    0 讨论(0)
提交回复
热议问题