Search code examples
c#web-serviceshttpsalesforceoutbound

System.IndexOutOfRangeException on SalesForce OutBound Message Listener


I have a webservice in ASMX and I am trying to create a Listener and here is what I have in the webservice.cs file

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
class MyNotificationListener : NotificationBinding
{
    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]

    public notificationsResponse Notifications(notifications n)
    {
        Method.SendMail("info@domain.com", "test@domain.com", "Here I am ", "I am loaded ", "", "");// This is to see if it loaded
        notificationsResponse r = new notificationsResponse();
        r.Ack = true;
        return r;
    }

}

in my outboun message configuration I am calling this webservice like so www.domain/webservice.asmx/Notification but when I load this service I See the following:

`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)`

In my config file I have the following

`<webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
        <add name="HttpSoap" />
      </protocols>
    </webServices>`

Here is what I have in the Notification Object this was generated from WSDL file to a class using wsdl.exe as directed in this example.

[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.7.3081.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://soap.sforce.com/2005/09/outbound")]
public partial class notifications
{

    private string organizationIdField;

    private string actionIdField;

    private string sessionIdField;

    private string enterpriseUrlField;

    private string partnerUrlField;

    private LeadNotification[] notificationField;

    /// <remarks/>
    public string OrganizationId {
        get {
            return this.organizationIdField;
        }
        set {
            this.organizationIdField = value;
        }
    }

    /// <remarks/>
    public string ActionId {
        get {
            return this.actionIdField;
        }
        set {
            this.actionIdField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
    public string SessionId {
        get {
            return this.sessionIdField;
        }
        set {
            this.sessionIdField = value;
        }
    }

    /// <remarks/>
    public string EnterpriseUrl {
        get {
            return this.enterpriseUrlField;
        }
        set {
            this.enterpriseUrlField = value;
        }
    }

    /// <remarks/>
    public string PartnerUrl {
        get {
            return this.partnerUrlField;
        }
        set {
            this.partnerUrlField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Notification")]
    public LeadNotification[] Notification {
        get {
            return this.notificationField;
        }
        set {
            this.notificationField = value;
        }
    }
}

Solution

  • So, After looking at your code I see that in your webservice the implementation of the public interface is named wrong, when I generated the Class with a WSDL from salesforce it creates the interface and its named INotificationBinding not NotificationBinding which makes me think that you didn't create the class properly from the wsdl, in the example you shown above that you are following says to use wsdl.exe /serverInterface leads.wsdl.

    Did you create the above class notification using the wsdl.exe ? if so did you make sure it was using the option of /serverInterface this is important(See image below), on salesforce it clearly mentions that you need to implement a class that calls the notification interface if this is not properly handled it will give the error of System.IndexOutOfRangeException

    Please try again using the serverinterface option. That should fix the issue.