• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7799
  • Last Modified:

Web Service - Index was outside the bound of the array

This is kind of a 2 part question.
1.  I have a web service that I am trying to get to work with a client.  Whenever I did my testing by adding a reference to it in a site, creating the objects, and submitting to the service, it works.  Now my client is trying to submit the information by using a Request object and building the xml manually, rather than using the objects and letting .Net handle the SOAP Request.  They submitted a test and got the following response from my server:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&gt; System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.     at System.Xml.XmlTextReaderImpl.Throw(Exception e)     at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)     at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()     at System.Xml.XmlTextReaderImpl.ParseDocumentContent()     at System.Xml.XmlTextReaderImpl.Read()     at System.Xml.XmlTextReader.Read()     at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read()     at System.Xml.XmlReader.MoveToContent()     at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent()     at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement()     at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()     at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)     at System.Web.Services.Protocols.SoapServerProtocol.Initialize()     at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing)     --- End of inner exception stack trace ---</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>

They also sent me the request they sent, and it looks exactly as it should, using the SOAP request structure on my ASMX page.  Does anyone know what that error means?  Or is it too vague?

Now for part 2.  In trying to debug this, I created a Test webmethod that I can invoke by going to my asmx page that emulates a request.  When I run the program and click Invoke, I get this error:
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.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)

At first I though maybe I was overlooking something, so I started commenting everything out, but still got the error.  Eventually I created another WebMethod which is completely empty and does nothing:
    public void DoNothing()
When I invoke this method, I still get the above error.  Any ideas?

  • 4
  • 3
1 Solution
Grant SpiteriSenior consultantCommented:
Sounds to me like the xml being sent has issues what is the output of the xml value before it is parsed ?
AlliedAdminAuthor Commented:
You mean the xml that is passed into my service?  It is exactly as it should be.  The client sent me the xml being sent, and it looks exactly like the asmx page says it should for the SOAP 1.2 Request.  I'm trying to attach SOAP Extensions to it to see what is actually being sent in and out, but i'm having issues getting it to work...

Here is an example of the xml the client sent, I replaced any identifying info:

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
    <MyFunction xmlns="http://mydomain.com/MyService/">
Grant SpiteriSenior consultantCommented:
I have had that error before it usually refers to 1 of 3 things
1: The xml parsed doesnt have the "<?xml version="1.0" encoding="utf-8"?>"
2. The xml parsed contains double quotes instead of single quotes
3. The byteordermark is at the start of the xml string and it is invisible so you need to strip the value from the file prior to parsing.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

AlliedAdminAuthor Commented:
Thanks for the replies so far.  Regarding your above suggestions:
1.  Assuming what they sent me is exactly what they actually sent, it had the xml version and encoding attributes.  I also did testing myself and used this, getting the same result.
2.  The xml string can't contain double quotes like the above?  I tried replacing all quotes with single quotes, and got the same result.  
3.  How do you do this?  Isn't this something that .Net would handle when it Serializes/DeSerializes the data?  My web service never actually looks at any xml strings, its all done by objects and their properties, so I'm not sure how this is even done.

Also, I forgot to mention that when I try to consume my web service and submitan object via a web request, it throws an error, and all it says in the Exception is: The remote server returned an error: (500) Internal Server Error.  Which is weird because it never actually executes a single line of code on the server (otherwise the error would be logged on the server).
Grant SpiteriSenior consultantCommented:
This is a version of serializing a object to remove the byteordermark that i have used that fixes the 3rd issue

public static string SerializeToString<T>(T objectToSerialize)
            XmlSerializer xs = new XmlSerializer(objectToSerialize.GetType());
            MemoryStream streamObj = new System.IO.MemoryStream();
            XmlTextWriter writter = new XmlTextWriter(streamObj, Encoding.UTF8);
            xs.Serialize(writter, objectToSerialize);
            streamObj = (MemoryStream)writter.BaseStream;
            string serializedString = Encoding.UTF8.GetString(streamObj.ToArray()).Replace("\"", "'");
            string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
            if (serializedString.StartsWith(_byteOrderMarkUtf8))
                serializedString = serializedString.Remove(0, _byteOrderMarkUtf8.Length);
            return serializedString;

With regards to the issue cosuming the service itself im not too sure what the problem could be except for the fact there is an issue on there end which doesnt seem to be the case from our previous discussions.
AlliedAdminAuthor Commented:
So I finally figured out the issue (I hope, I was able to get my tests working).  One of the properties of my object is a DateTime.  The client sent the date in the format <MyDate>Dec 21 2009</MyDate>.  However it needs to be in the format <MyDate>2009-12-21T15:10:19.291651-08:00</MyDate> or <MyDate>2009-12-21</MyDate> for short dates.  
I'm guessing that the reason for the error being so vague is that when .Net tried to Serialize/DeSerialize the xml into my custom object, it didn't know how to handle the date format, and returned a generic error?  Either way you would think it would throw a more specific error if that were the case?

Anyways, I'm waiting to hear back from the client if that fixes the issue...
Grant SpiteriSenior consultantCommented:
Well hope it all works, don't you just love the generic erros lol.

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now