Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I want to host a custom WCF service in SharePoint 2010. In the service, I have a method which accepts a byte array for sending files. The file is then processed by some custom logic and stored as an attachment in a list.

On uploading, I get a 400 bad request exception which I think is caused by the filesize. But as this is a selfhosted service, I don't really have any web.config entries on which to set the max request size and stuff like that. So my question: what is a good way to manage this? A requirement is that the deploy should be a single step, no manual editing of config files. I checked the ServiceBehaviour attribute for options, but there doesn't seem to be anything there related to parameters like these.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

The default maximum sizes used by WCF are indeed extremely tight.

Fortunately, anything you can specify in your config file you can also specify programmatically. It is something we do in all our products as we don't want to mess around with massive WCF sections in SharePoint's web.config.

I have included some sample code below, in this case from a WCF Client, but the same applies to the WCF Service

/// <summary>
/// Configure the Bindings, endpoints and open the service using the specified address.
/// </summary>
/// <returns>An instance of the Web Service.</returns>
public static DocumentConverterServiceClient OpenService(string address)
{
    DocumentConverterServiceClient client = null;

    try
    {
        BasicHttpBinding binding = new BasicHttpBinding();
        // ** Use standard Windows Security.
        binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
        binding.Security.Transport.ClientCredentialType = 
                                                    HttpClientCredentialType.Windows;
        // ** Increase the Timeout to deal with (very) long running requests.
        binding.SendTimeout = TimeSpan.FromMinutes(30);
        binding.ReceiveTimeout = TimeSpan.FromMinutes(30);
        // ** Set the maximum document size to 40MB
        binding.MaxReceivedMessageSize = 50*1024*1024;
        binding.ReaderQuotas.MaxArrayLength = 50 * 1024 * 1024;
        binding.ReaderQuotas.MaxStringContentLength = 50 * 1024 * 1024;

        // ** Specify an identity (any identity) in order to get it past .net3.5 sp1
        EndpointIdentity epi = EndpointIdentity.CreateUpnIdentity("unknown");
        EndpointAddress epa = new EndpointAddress(new Uri(address), epi);

        client = new DocumentConverterServiceClient(binding, epa);

        client.Open();

        return client;
    }
    catch (Exception)
    {
        CloseService(client);
        throw;
    }
}
share|improve this answer
    
Ok, but I'm not sure where the serverside binding is created? Upon creating the client proxy object it's easy, but SharePoint takes care of initializing the serverside service, so that's not exactly the same? –  Jasper Apr 26 '11 at 14:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.