Asked by:
outside the bounds of the array when add new method to web service

Question
-
User2108892867 posted
Hello everyone, I have tested for a couple of hours now and still can't figure out why my web service.asmx is not working. I kept getting this error:
System.IndexOutOfRangeException: Index was outside the bounds of the array
Here is my service:
[WebMethod(EnableSession = true)] public void selectMyTest() { if (Session["user_id"] != null) { .... Context.Response.Write(JsonConvert.SerializeObject(events)); } }
This is working but the moment I added method to my service, I got the above error.
[WebMethod(EnableSession = true)] public string addUser(MyUser pro) { string user_id = ""; if (Session["user_id"] != null) { .... } return user_id; }
Any ideas why? I have other webservices which I can add as many methods as I want. But this one seems very odd. What did I do wrong? Thanks
Tuesday, December 4, 2018 3:09 AM
All replies
-
User753101303 posted
Hi,
Where does it happen? The stack trace could help.
Not directly related but rather than having explicit serialization/deserialization code and writing to Response, you can write a web service method that gets object arguments and return object as done usually (and ASP.NET handles all that behind the scene for you).
Tuesday, December 4, 2018 8:26 AM -
User-1716253493 posted
to avoid error, when read an array check that array count greater than index
if(arr.Count()>i) { string x = arr[i]; }
Tuesday, December 4, 2018 8:52 AM -
User2108892867 posted
I did some research and found quite a lot of people having this issue. This is the error I got:
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)
This post has very similar issue
For me, it errors out when I added a service with a parameter which is a class I created. So this won't work.
[WebMethod(EnableSession = true)] public string addUser(MyUser pro) { string user_id = ""; if (Session["user_id"] != null) { .... } return user_id; }
But if I changed it to this by changing the parameter to string
[WebMethod(EnableSession = true)] public string addUser(String test) { string user_id = ""; if (Session["user_id"] != null) { .... } return user_id; }
Then it will work. Or if I remove the parameter completely:
[WebMethod(EnableSession = true)] public string addUser() { string user_id = ""; if (Session["user_id"] != null) { .... } return user_id; }
The weird thing is my js script is not even calling the addUser method. Why does this method matter? Secondly, I have other methods in a different asmx file that is using the class I created as parameter and it's working fine. So what is actually causing this?
Tuesday, December 4, 2018 11:01 PM -
User475983607 posted
The error is related to accessing an array or collection index that does not exist. There is no indication of an array or collection in the posted code. I assume you are not showing the code that is causing the error. Commonly the stack trace shows the file, class, and line of code that caused the error. Is there anyway you can post the relavent code?Tuesday, December 4, 2018 11:29 PM -
User2108892867 posted
Hello mgebhard, thanks for your reply. I could actually put the code in there but the error is not in my code. Here is another post that has the same problem like mine.
And one solution suggested in the post through this link http://rebelheart.squarespace.com/blog/2007/6/6/5-hours-later-and-hello-world-saves-the-day.html is put the hello function back in the webservice. Here is the function
[WebMethod] public string HelloWorld() { return "Hello World"; }
After I did that, it's working fine.
So what is actually causing this behavior in the webservice?
Thanks
Wednesday, December 5, 2018 12:18 AM -
User475983607 posted
If you can post code that reproduces the issue, someone will be able to provide an explanation. I can only guess that you have some naming conflicts that causes an issue with the proxy code generator. Like naming a return type or input type the same as a method or an object model that has a similar design issue.
Perhaps post your proxy "reference.cs" file so we can see what the code looks like.
Wednesday, December 5, 2018 12:35 AM -
User-893317190 posted
Hi asplearning,
Don't know how you call your webserver.
As you said, if you have another method using customized class as parameter.
The method returning void will not work.
One way is to change your method which returns void to a method returning string.
Another way is to change the way you call your webmethod.
Below is my webservice.
public class ArgumentTest : System.Web.Services.WebService { [WebMethod(EnableSession =true)] public void selectMyTest() { // do nothing } [WebMethod(EnableSession = true)] public string addUser(Student stu) { return "success"; } } public class Student { public string Name { get; set; } public int Age { get; set; } } }
If I call my method selectMyTest as follows. It will cause the error.
$.ajax({ url: ' /Services/ArgumentTest.asmx/selectMyTest', type: "post", dataType: "json", success: function (data) { console.log(data); } });
But if I change my method to (just add contentType: "application/json; charset=utf-8",)
<script> $.ajax({ url: ' /Services/ArgumentTest.asmx/selectMyTest', type: "post", dataType: "json", contentType: "application/json; charset=utf-8", success: function (data) { console.log(data); } }); </script>
It could be called normally.
Best regards,
Ackerly Xu
Wednesday, December 5, 2018 5:24 AM -
User2108892867 posted
Thanks for your reply. This is how I called my webservice:
return $http.get(
myWebServiceUrl,
{
params: { start: start, end: end }
}
);I tried by removing the hello function added the
Ackerly Xu
contentType: "application/json; charset=utf-8",
But still the same error. After I put the hello function back, it's working fine. Can't really understand why it's doing that. Looks like naming conflict or something.
Thursday, December 6, 2018 10:48 PM -
User-893317190 posted
Hi asplearning,
It seems you are using angular.js.
Angular.js will deal with content-type in a special way.
So maybe the content type is still not set, you could open F12 to see the request header.
Please refer to the link below to learn how to set Conent-Type in angular.js.
https://stackoverflow.com/questions/16194442/angular-content-type-is-not-being-sent-with-http
Best regards,
Ackerly Xu
Friday, December 7, 2018 1:07 AM -
User-1716253493 posted
Im not sure, seem like the result need to be splited into an array
Can you provide user_id with valid value when session is null?
Maybe something like user_id =","
Friday, December 7, 2018 1:29 AM