Enable WCF message logging/tracing

Place following in the config file:

<messageLogging logEntireMessage=”true”

. . .

<source name=”System.ServiceModel.MessageLogging” switchValue=”Information, ActivityTracing”>
<add name=”log”
type=”System.Diagnostics.XmlWriterTraceListener” />
<trace autoflush=”true”/>

  • Use SvcTraceViewer.exe to view svclog file (visual studio command prompt).
  • Change parameter swithcValue to “Warning, ActivityTracing” when in production!
  • Message will not be placed into the log, if size will be larger than maxSizeOfMessagesToLog (in bytes)!
Posted in WCF. Tags: . Leave a Comment »

Specify to Run Server Garbage Collection (gcServer)

To enable Garbage Collector to work in server mode put follwing section in config:

  <gcServer enabled="true" />

When to use:

  • On computers with more  than two cores.
  • You have multithreaded service not hosted in IIS.

Typical candidate: self-hosted WCF service.
This option improves service performance and consumes more memory.

For details see:

Call WCF service – error handling & disposing

The final example how to correctly call WCF service and end communication if error occurred:

ServiceClient proxy = null;
bool success = false;
   proxy = new ServiceClient();
   success = true;
   // if failed and not null
   if (!success && proxy != null)

Remember always call Close to end communication. In other case it will can cause to crash server host (IIS for example) after reaching threads or memory limit.

Avoid to call non-IIS WCF service by using the using statement syntax. It will hide the actual communication exceptions details if server side error occurred.

Posted in WCF. Tags: . Leave a Comment »

Choosing the right WCF binding

A WCF binding is the endpoint component that defines how the client needs to communicate with the service. It groups settings such as underlying transport protocol, security requirements, and message encoding.

WCF provides nine built-in bindings:

  1. BasicHttpBinding: Basic web service communication. Exposes WCF services as legacy ASMX web services. Used for interoperability. No security by default.
  2. WSHttpBinding: Web services with WS-* support. Supports transactions and reliable messaging.
  3. WSDualHttpBinding: Web services with duplex contract and transaction support.
  4. WSFederationHttpBinding: Web services with federated security. Supports transactions.
  5. MsmqIntegrationBinding: Communication directly with MSMQ applications. Supports transactions.
  6. NetMsmqBinding: Communication between WCF applications by using queuing. Supports transactions.
  7. NetNamedPipeBinding: Communication between WCF applications on same computer. Supports duplex contracts and transactions.
  8. NetPeerTcpBinding: Communication between computers across peer-to-peer services. Supports duplex contracts.
  9. NetTcpBinding: Communication between WCF applications across computers. Supports duplex contracts and transactions.

Although most bindings will work on scenarios they are not designed for, it’s a good practice to choose the right binding for a given endpoint. In chapter one of the “Programming WCF Services” book, Juval Lowy provides a very useful decision-activity diagram for choosing the right binding:


WCF Bindings

The first question you should ask yourself is whether your service needs to interact with non-WCF clients. If the answer is yes, and if the client is a legacy MSMQ client, choose theMsmqIntegrationBinding that enables your service to interoperate over MSMQ with such a client. If you need to interoperate with a non-WCF client and that client expects basic web service protocol (ASMX web services), choose the BasicHttpBinding, which exposes your WCF service to the outside world as if it were an ASMX web service (that is, a WSI-basic profile). The downside is that you cannot take advantage of most of the modern WS-* protocols. However, if the non-WCF client can understand these standards, choose one of the WS bindings, such as WSHttpBindingWSFederationHttpBinding, orWSDualHttpBinding. If you can assume that the client is a WCF client, yet it requires offline or disconnected interaction, choose the NetMsmqBinding that uses MSMQ for transporting the messages. If the client requires connected communication, but could be calling across machine boundaries, choose the NetTcpBinding that communicates over TCP. If the client is on the same machine as the service, choose the NetNamedPipeBinding that uses named pipes to maximize performance. You may fine-tune binding selections based on additional criteria such as the need for callbacks (WSDualHttpBinding) or federated security (WSFederationHttpBinding).

You can download the book’s sample chapter to read more on this subject. You would also find the diagram in Juval’s article WCF Essentials – A Developer’s Primer on the CoDe Magazine.  

This information was found on Soledad Pano’s Blog.

Posted in WCF. Tags: . Leave a Comment »