David Pallmann’s WCF Tips #4 - Configuration
Tip: Learn the appropriate knobs to turn to make WCF perform for you. Don’t assume the defaults are best, in many cases they aren’t. Specifics are addressed in several related tips.
Rationale: Some of the default settings in WCF are great. Some of them aren’t. Some of them, in the name of being secure out-of-box, prevent you performing useful work!
What if I Don’t? Your production deployments will fail.
Examples:
· Instancing: the default is Per Session, but you’re better off writing stateless services.
· Bindings: default quota settings severely limit message size, depth, and complexity.
· Service Hosting: default settings limit the number of concurrent clients and sessions.
Choose the Right Binding for the Right Purpose, then Fine-Tune it
Tip: Use the correct binding for your endpoints. First choose the right general binding. Then fine-tune the binding for its purpose.
Rationale: A binding is a pre-packaged profile that controls a great deal about how communication will take place. The key characteristics of a binding are transport and security model, but there is much more under its control. The choice of binding also affects reliability, transaction support, and quota settings such as maximum message size.
It’s first and foremost important to select the right general binding. The importance of these factors will point you to the right binding to use.
1. Desired interop level.
2. Desired security model.
3. Desired reliability.
4. Desired transport.
5. Desired transaction support.
Secondly, you should fine tune your binding. Just because a binding supports your preferred security model, reliability level, or transaction capability doesn’t mean those things are enabled by default. Get acquainted with your binding’s properties and configure their values to fit your needs.
What if I Don’t? You’ll get the luck of the draw. Fortunately, it’s easy to change your mind about bindings down the road.
Example: A service is being created that must be accessible by a partner company. The partner company only has experience with
Configure Bindings to Enable Doing Real Work
Tip: Override the debilitating default properties of bindings so that you can get real work done.
Rationale: In the name of shipping “securely by default”, WCF Services have great limitations on the amount of work they can perform. To get real levels of work done in the enterprise requires overriding many default values. Some of these values are in the binding properties, while others are found in the ServiceHost object. There are limitations on message size, string size, XML depth, array length, and other areas that will make calls fail if not set to real-world values.
Of course, these limits were set with good intentions such as preventing denial of service attacks. Don’t override the binding properties with values larger than you need.
What if I Don’t? You’ll run into limitations at runtime, such as not being able to support large or complex messages.
Example: Here’s an example of overriding binding properties. This example is in code, but you can perform the equivalent in your XML configuration file. Be sure to use reasonable values for your needs, not Int32.MaxValue as shown. Refer to the documentation for the precise meaning of these properties.
NetTcpBinding binding = new NetTcpBinding();
binding.ReaderQuotas.MaxDepth = Int32.MaxValue;
binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
binding.ReaderQuotas.MaxArrayLength = Int32.MaxValue;
binding.ReaderQuotas.MaxBytesPerRead = Int32.MaxValue;
binding.ReaderQuotas.MaxNameTableCharCount = Int32.MaxValue;
binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
binding.MaxReceivedMessageSize = Int32.MaxValue;
binding.MaxConnections = 1000;