WCF Service Custom SOAP Header Causes Index was outside the bounds of the array

0 votes
asked Nov 12, 2014 in WCF by anonymous

Hello,

I have written a WCF service with custom SOAP Header. I have exposed the header to the WSDL description using Custom behavior. I am able to see the header when I test it using wcfTestClient. I can successfully send the request to service and I am able to read header from context and my service operation executes properly but, wcfTestClient and SOAPUI, both are receiving below error. I am not able to catch any exception for this issue so, not sure where is really happening.

But, from the stack trace below, all I can understand is, some issue is happening when it is trying to add header using AddHeadersToMessage.

One thing I am not sure is, even though I am not sending any header in the response, why am I getting this error? I tried to find solution in google but no luck so far. Any one faced the same issue any time.

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <s:Fault>
         <faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</faultcode>
         <faultstring xml:lang="en-US">Index was outside the bounds of the array.</faultstring>
         <detail>
            <ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
               <HelpLink i:nil="true"/>
               <InnerException i:nil="true"/>
               <Message>Index was outside the bounds of the array.</Message>
               <StackTrace><![CDATA[at System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.AddHeadersToMessage(Message message, MessageDescription messageDescription, Object[] parameters, Boolean isRequest)
   at System.ServiceModel.Dispatcher.OperationFormatter.SerializeReply(MessageVersion messageVersion, Object[] parameters, Object result)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.SerializeOutputs(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)]]></StackTrace>
               <Type>System.IndexOutOfRangeException</Type>
            </ExceptionDetail>
         </detail>
      </s:Fault>
   </s:Body>
</s:Envelope>
Share

1 Answer

+1 vote
answered Nov 12, 2014 by Aadhira (1,213 points)

I faced the same issue for exactly the same scenario, when I used custom soap header in my wcf service. I was also adding SOAP Header description using custom behavior and not sending any header in the service response. Then I also received the same issue and got same exception. Then when I see my behavior closely, I have noticed I was adding header description for both In and Out direction of SoapHeaderDirection. So, once I removed the code which was adding soap header description for the out direction, everything work fine. Hopefully, this will help you.

Before Fix

foreach (MessageDescription msgDescription in operationDescription.Messages)
            {
                if ((msgDescription.Direction == MessageDirection.Input && input)
                    || (msgDescription.Direction == MessageDirection.Output && output))
                {
                    msgDescription.Headers.Add(header);
                }
            }

After Fix

foreach (MessageDescription msgDescription in operationDescription.Messages)
            {
                if (msgDescription.Direction == MessageDirection.Input && input)
                {
                    msgDescription.Headers.Add(header);
                }
            }

Your answer

Preview

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015 pinfaq.com
...