Mittwoch, 10. Juni 2015

WCF WebService Factory for SharePoint 2010

Problem:
When you create your own SharePoint WCF Service in the ISAPI folder you might encounter the following error when sending to large Xml.

The maximum array length quota (16384) has been exceeded while reading XML data. This quota may be increased by changing the MaxArrayLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader.

Solution:
You need to create your own Service Host Factory.
<% @ServiceHost Debug="true"
    Service="ExampleHostFactory.TestService, ExampleHostFactory, Version=1.0.0.0, Culture=neutral, PublicKeyToken=11e99323d9a22b04"
    Factory="ExampleHostFactory.ServiceHostFactory, ExampleHostFactory, Version=1.0.0.0, Culture=neutral, PublicKeyToken=11e99323d9a22b04" %>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client.Services;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace ExampleHostFactory
{
    public class ServiceHostFactory : MultipleBaseAddressBasicHttpBindingServiceHostFactory
    {
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            ServiceHost serviceHost = new MultipleBaseAddressBasicHttpBindingServiceHost(serviceType, baseAddresses);

            if (serviceHost != null)
            {
                serviceHost.Opening += new EventHandler(HostOpening);
            }

            return serviceHost;
        }

        void HostOpening(object sender, EventArgs e)
        {
            ServiceHost serviceHost = sender as ServiceHost;

            if (serviceHost != null && serviceHost.Description != null && serviceHost.Description.Endpoints != null)
            {
                foreach (ServiceEndpoint endpoint in serviceHost.Description.Endpoints)
                {
                    if (endpoint != null && endpoint.Binding != null)
                    {
                        BasicHttpBinding basicBinding = endpoint.Binding as BasicHttpBinding;
                        if (basicBinding != null)
                        {
                            // configure binding settings
                            basicBinding.MaxReceivedMessageSize = Int32.MaxValue;
                            basicBinding.ReaderQuotas.MaxArrayLength = Int32.MaxValue;
                            basicBinding.ReaderQuotas.MaxBytesPerRead = Int32.MaxValue;
                            basicBinding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
                        }
                    }
                }
            }
        }
    }
}

Keine Kommentare:

Kommentar veröffentlichen