Web Service - Index was outside the bound of the array

Posted on 2009-12-21
Last Modified: 2012-05-08
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="" xmlns:xsi="" xmlns:xsd=""><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?

Question by:AlliedAdmin
    LVL 9

    Expert Comment

    by:Grant Spiteri
    Sounds to me like the xml being sent has issues what is the output of the xml value before it is parsed ?

    Author Comment

    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="" xmlns:xsd="" xmlns:soap12="">
        <MyFunction xmlns="">
    LVL 9

    Expert Comment

    by:Grant Spiteri
    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.

    Author Comment

    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).
    LVL 9

    Expert Comment

    by:Grant Spiteri
    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.

    Accepted Solution

    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...
    LVL 9

    Expert Comment

    by:Grant Spiteri
    Well hope it all works, don't you just love the generic erros lol.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    While working on Silverlight and WCF application, I faced one issue where fault exception occurred at WCF operation contract is not getting propagated to Silverlight client. So after searching net I came to know that it was behavior by default for s…
    Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now