I have a fairly simple WCF service, running as a Windows service with WSHttpBinding and consumed by a website. The service works most of the time. Sometimes I cause the following error when calling various methods in a service:
Array length '0' provided by the get-only collection of type 'System.String[]' is less than the number of array elements found in the input stream. Consider increasing the length of the array.
I went through the debugger, and the results of the function returns look correct. I am new to WCF and have no idea what might make this error know where to start looking for problems.
Any help is greatly appreciated and I can provide additional information if necessary.
Update 2 . I solved this problem and answer below.
Update
Service agreement
[ServiceContract(Namespace="http://Foo.Service")] public interface IExchangeManager { [OperationContract] IList<DistributionGroup> GetDistributionGroups(Organisation org); }
Implementation
public class ExchangeManager : IExchangeManager { public IList<DistributionGroup> GetDistributionGroups(Organisation org) { var groups = new List<DistributionGroup>();
A distribution group object has several row properties and a List<User> . I commented on the setting of these properties and the error persists, so I do not believe that the problem is with these objects.
Service binding configuration
protected override void OnStart(string[] args) { if (serviceHost != null) { serviceHost.Close(); } var baseAddress = "http://hosted.local:8001/ExchangeManager/service"; serviceHost = new ServiceHost(typeof(ExchangeManager), new Uri(baseAddress)); // Check to see if the service host already has a ServiceMetadataBehavior ServiceMetadataBehavior smb = serviceHost.Description.Behaviors.Find<ServiceMetadataBehavior>(); // If not, add one if (smb == null) { smb = new ServiceMetadataBehavior(); } smb.HttpGetEnabled = true; smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; serviceHost.Description.Behaviors.Add(smb); // Check to see if we already have a debug behaviour ServiceDebugBehavior debug = serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>(); // if not found - add behavior with setting turned on if (debug == null) { serviceHost.Description.Behaviors.Add(new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true }); } else { debug.IncludeExceptionDetailInFaults = true; } // Add end points // Setting quotes to try and fix array length problem var binding = new WSHttpBinding(); binding.MaxReceivedMessageSize = 1073741824; var myReaderQuotas = new XmlDictionaryReaderQuotas(); myReaderQuotas.MaxStringContentLength = 1073741824; myReaderQuotas.MaxArrayLength = 1073741824; myReaderQuotas.MaxBytesPerRead = 4096; myReaderQuotas.MaxDepth = 32; myReaderQuotas.MaxNameTableCharCount = 16384; binding.GetType().GetProperty("ReaderQuotas").SetValue(binding, myReaderQuotas, null); serviceHost.AddServiceEndpoint(typeof(IExchangeManager), binding, ""); serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex"); serviceHost.Open(); }
Full exception
System.ServiceModel.Dispatcher.NetDispatcherFaultException: The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http:
I get this error for a number of methods of my service, and all of these methods have worked and still work in some cases. The error occurs in service methods that have a void return type, so I don't think it complains about the data returned by the service.