C # .NET Web Services utilizzando HttpGet Restituisce System.IndexOutOfRangeException
-
30-09-2019 - |
Domanda
Ho installato questo servizio web ad esempio per indagare l'origine del mio errore:
namespace userControlPanel.webservice
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public OutputData1 AjaxGetMore(InputData1 input)
{
return new OutputData1()
{
id = input.id,
message = "it's work!",
myInt = input.myInt + 1
};
}
}
public class OutputData1
{
public string id { get; set; }
public string message { get; set; }
public int myInt { get; set; }
}
public class InputData1
{
public string id { get; set; }
public int myInt { get; set; }
}
}
E 'basato su un esempio qui
Così ho cercato di recuperare un risultato qui:
http://localhost:57109/webservice/WebService1.asmx/AjaxGet?id=li1234
E ho il risultato,
System.IndexOutOfRangeException: Index was outside the bounds of the array
at System.Web.Services.Protocols.HttpServerType..ctor(Type type)
at System.Web.Services.Protocols.HttpServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)
Quindi, suppongo che di web.config giusto? Così ho implementare impostazione suggerita (PS, ho anche creato un servizio web HTTP POST e funziona solo grande)
Da web.config:
<webServices>
<protocols>
<add name="HttpPost"/>
<add name="HttpPostLocalhost"/>
<add name="HttpGet"/>
</protocols>
</webServices>
<httpHandlers>
<add verb="*" path="*.asmx" type="System.Web.Script.Services.ScriptHandlerFactory" validate="false"/>
</httpHandlers>
</system.web>
Soluzione
Si scopre che questo problema era il risultato di qualche povero codice re-factoring e le questioni di classe / nome-spazio da parte mia.
Inoltre, vorrebbe sottolineare che stare lontano formano HttpGet quando si utilizza jQuery webservices è una buona idea, ri-informazioni che ho letto qui
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow