Asynchronous Programming in WCF

Common WCF Mistakes Developers Make and How to Avoid Them

Windows Communication Foundation (WCF) is still one of the most widely used frameworks for creating secure, reliable, and service-oriented applications in enterprise environments. Even if WCF is a mature technology, many developers still fall into the same traps that cause performance issues, security issues, bandwidth issues, scalability issues, and unexpected application errors.

Whether you are updating existing WCF services or creating new WCF services for the enterprise environment, knowing the common mistakes can help you to enhance your services to be more seamless, more reliable, and quicker to debug! In this guide we will discuss the most common mistakes WCF services suffer from, and how you can solve or prevent them using proven best practices!

1. Choosing the Wrong Binding

One of the common mistakes developing WCF services is choosing a binding that is incorrect for the given situation. Each binding serves a different purpose, and will affect the overall speed, security, and performance is you choose a binding that is not suitable.

Common Mistakes

  • Using BasicHttpBinding when secure or high-performance applications are the requirement
  • Using NetTcpBinding when the application needs inter-operability and the systems need to communicate across platforms
  • Using WSHttpBinding when secure bindings are not required

How to Prevent

  • First identify the communication environment—intranet or internet
  • Use NetTcpBinding when designing to run in a high-performance application internal to an enterprise system
  • Use BasicHttpsBinding, if the service is working publicly, to ensure proper security implementation for SOAP services
  • Use custom bindings only when necessary

If you select the correct bindings you will get speed from the communication layer, that will ultimately increase reliability!

2. Badly Configured Timeout Values

It’s common for developers to forget to configure timeout values appropriately resulting in waiting, unresponsive requests, or instant termination.

Common Examples

  • Default timeout values are very low
  • Waiting indefinitely for clients
  • Operations hang under load

How to Avoid

Make sure you configure all four timeout values, each of the four must be in place: OpenTimeout, CloseTimeout, ReceiveTimeout, and SendTimeout

  • Choose reasonable values based on the complexity of the operation
  • Log the timeout if/when it occurs to identify the bottleneck
  • Proper timeout management keeps services from freezing, and timeout values improve fault handling.

3. Ignoring Fault Handling and Error Messaging

WCF uses FaultContracts to send structured error messages. Many developers skip these and users becomes confused about exceptions thrown or the client becoming unusable.

Common Examples

  • Returning back raw .NET exceptions
  • Leaking sensitive information
  • A client does not understand the reason for the error

How to Avoid

  • Utilize FaultException and FaultContract to properly structured messages
  • Implement custom faults however they clearly don’t bloat the response entity
  • Enable IncludeExceptionDetailInFaults only in a development environment

Using Fault and FaultContract will provide clarity in your error handling without exposing the underlying logic of your operation.

4. Lacking or Badly Configured Security Configurations

Security is paramount when using WCF but many will overlook important configurations.

Common Examples

  • Not using encrypted bindings
  • Weak message credentials
  • Lacking proper SSL/TLS over https setups
  • Exposed endpoints

How to Avoid

  • Always use HTTPS or better yet TLS
  • Use either Protection or Message Security according to what make sense in your scenario.
  • Avoid exposing internal endpoints outward
  • Secure all user input and credentials

A secure WCF service environment

5. Not Utilizing Asynchronous Operations

Some developers are still doing synchronous operations, which results in bottlenecks and diminished scalability.

Common Problems

  • Blocked threads
  • Slow response during high load
  • Increased load on the server

How to Avoid

  • Use async/await
  • Enable asynchronous service contracts
  • Combine good-async programming with proper throttling

Asynchronous programming is a major opportunity for improved scalability under high-load conditions.

6. Poor Throttling Settings

Using incorrect throttling settings means your server can be overwhelmed or, less frequently, can reject legitimate requests.

Common Problems

  • Too many concurrent connections
  • Unintentional blocking of requests
  • Inefficient use of resources

Steps to Avoid

Set throttle settings properly for example:

  • maxConcurrentCalls
  • maxConcurrentSessions
  • maxConcurrentInstances

Test various settings under load to find the perfect level.

7. Ineffective use of Serialization

Serialization is critical in WCF performance, yet it is often the most misused source of WCF performance.

Common Problems

  • Too complex data contracts
  • Big object graphs
  • Data types that are not supported in serialization
  • DataContract attribute is not used correctly on all of the data objects

How to Avoid

  • Keep data contracts as lean as possible
  • No deep/nested objects
  • Utilize the DataContractSerializer for maximum performance of serialization
  • Appropriately use attributes like [DataMember] and [DataContract]

Optimized serialization can dramatically increase the speed of service communication.

8. Forgetting to Use Logging and Diagnostics

If you do not take the proper steps into logging, debugging quickly becomes difficult.

Common Problems

  • No view into failures
  • Unknown performance issues in the service that is not obvious
  • Cannot resolve faults/track faults for analysis

How to Avoid

  • Enable WCF tracing and message logging
  • Use ETW/Windows Event Viewer or other option

9. Inadequate Endpoint Configuration

Endpoints indicate where and how your service communicates with external systems, and poor endpoint configuration can result in unpredictable service behavior.

Common Mistakes

  • Missing endpoints
  • Duplicate endpoints
  • Incorrect address or contract mappings

How to Avoid

When planning your service, clearly define all the endpoints:

  • Address — where your service will respond to calls
  • Binding — how to connect to the service
  • Contract — which operations are available

Your services should always have well-defined endpoints.

10. Not Leveraging Service Hosting

Hosting is critical to understanding WCF performance.

Common Mistakes

  • Hosting in an incorrect environment
  • Outdated IIS (Internet Information Services) versions
  • No load balancing

How to Avoid

When it comes to hosting, think about three options:

  • IIS (Internet Information Services)
  • Windows Services
  • Azure App Service
  • Additionally, for the load balancing consideration, you should utilize Web Farms.
  • Lastly, make sure you keep the server patched and configured correctly.

The importance of effective hosting cannot be underscored. Good hosting means more reliability and scalability.

Conclusion

While WCF is a mature technology, I come across many of the same issues and mistakes in the WCF applications I support that cause performance, security, and reliability issues. In conclusion, a better understanding of and avoidance of WCF development mistakes, such as improper binding selection, inadequate exception handling, security misconfiguration, and failure to performance-tune, will lead to implementations for your enterprise applications that are fast, secure, and scalable.

Whether you are maintaining a legacy WCF application, or trying to improve a current WCF service, following these best practices will give you the requisite tools to ensure the deliverance of an enterprise-grade high-quality solution to support your enterprise for years to come.