Adjusting WCF default message limits

Introduction

So if you are reading this post you’ve probably discovered that when publishing a WCF service there are certain default limits imposed on the messages being sent and received. For example arrays are limited to 16384 elements, strings are limited to 8192 characters and the total message is limited to 64K.

Now of course one of the major benefits of WCF is its configurability and the good news is these limits are very easy to change. Before we dive into how to change these… a work of warning… the limits are there to protect the availability and scalability of our service, if we increase the limits to very large values then we have no control over the amount of data being pushed into our service and can leave our service open to denial of service attacks. Consumers may also unknowingly overload our service and affect its availability and or performance for other users. The point I’m trying to make is don’t just wack a few zeros on the end off all the values to make your service work… pick out the values you need to change and increase them to sensible levels.

Where Are the Limits Set?

The limits are imposed by the receiver of messages so if you’re having issues sending large data from your service to the client you need to adjust the configuration file for the client (web.config for websites and app.config for thick clients). Alternatively if you want to send large requests into your service then you need to adjust the config file for the service.

Client Settings

Clients are very simple to adjust, when you create a reference to a service it adds a few entries to the section of your config file, the message limit values are shown on line 8 below;

<system.serviceModel>
 <bindings>
  <span style="font-size: x-small;"><wsHttpBinding></span>
   <binding name="WSHttpBinding_IMyBizHRService" closeTimeout="00:01:00"
    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
    bypassProxyOnLocal="false" transactionFlow="false"
    hostNameComparisonMode="StrongWildcard"
    maxBufferPoolSize="524288" maxReceivedMessageSize="524288"
    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
    allowCookies="false">
    .....
   </binding>
  <span style="font-size: x-small;"></wsHttpBinding>
 </bindings></span>

Service Settings

So depending on how you have setup your service you may not have a binding configuration yet (i.e. you are using the default configuration for the type of binding you are using), the good news is you can simply copy and paste the binding tag from your client and adjust the values as required. The one thing to note is if you didn’t have an existing binding configuration you will need specify that your endpoint should use your new binding configuration as shown on line 2 below;

<endpoint binding="wsHttpBinding"
  bindingConfiguration="WSHttpBinding_IMyBizHRService"
  contract="MyBizHRService.IMyBizHRService"
  name="WSHttpBinding_IMyBizHRService">

Conclusion

As you would expect it is very easy to adjust the default limits when publishing a WCF service. Remember that these limits are there to protect your service so make sure you know what you are adjusting and take the time to choose sensible values.

Advertisements