WCF gives a very rich set of standard bindings that you can use for your endpoints. However we might need to tweak properties that might not be exposed on the standard bindings. You can handcraft the whole binding or you can start with standard binding as a template. Here are some ways.
- Create the BindingElementCollection from the StandardBinding and update properties on it and use the BindingElementCollection to create the CustomBinding. This creates a full clone of binding elements and you can reset values on the elements using Find<BindingElement> on the collection.
- Create the CustomBinding directly from the StandardBinding by passing the binding into the CustomBinding constructor. The first approach has an issue where properties like SendTimeout/ReceiveTimeout etc don’t get copied onto the CustomBinding since they are held by the Binding and not its child elements & for this simple reason would recommend the second approach.
[code:c#]
BasicHttpBinding httpBinding = new BasicHttpBinding();
httpBinding.SendTimeout = TimeSpan.FromSeconds(123);
Console.WriteLine(httpBinding.ToString());
BindingElementCollection bec = httpBinding.CreateBindingElements();
bec.Find<HttpTransportBindingElement>().KeepAliveEnabled = false;
CustomBinding copy1 = new CustomBinding(bec);
Console.WriteLine("SendTimeout = {0} KeepAliveEnabled = {1}",
copy1.SendTimeout,
copy1.Elements.Find<HttpTransportBindingElement>.KeepAliveEnabled);
CustomBinding copy2 = new CustomBinding(httpBinding);
copy2.Elements.Find<HttpTransportBindingElement>.KeepAliveEnabled = false;
Console.WriteLine("SendTimeout = {0} KeepAliveEnabled = {1}",
copy2.SendTimeout,
copy2.Elements.Find<HttpTransportBindingElement>().KeepAliveEnabled);
[/code]
An easy way for getting performance and diagnostic information. You can quickly collect etw/minidumps vmmaps etc.
http://visualstudiogallery.msdn.microsoft.com/en-us/e8649e35-26b1-4e73-b427-c2886a0705f4
This is the command you can use to do an automated 5 second network capture using netmon for default net.tcp traffic on any stack such as wcf or Sapphire. The default net.tcp port is 808 so thats what we use here.
[code:c#] nmcap /network * /capture tcp.port == 808 /startwhen /timeafter 0 sec /stopwhen /timeafter 5 sec /file output.cap [/code]
- You can download network monitor from here
- Check out this post incase you are not aware of nmcap.
Here are some quick walkthroughs on how you can write native services and consume web services natively.
For the samples you can get the Microsoft Windows SDK for Windows 7 and .NET Framework 3.5 SP1
Here was an interesting set of questions comparing WCF & WWS
1) With .NET 4.0, are we going to see any improvement to close the gap?
2) There seems very little information about this except on Channel 9 or various blogs. Are we going to see more information?
3) Is there a way to get the best of both WWS and .NET in a typical application? For example, use WWS for the web service API for
Here is the reply from Bob (our Product Unit Manager)
WCF and Windows Web Services (WWS) are complementary technologies. WCF is the premier Web Services stack to use when writing managed applications; if you are writing native code and want a SOAP stack then definitely use the WWSAPI. Introducing a native SOAP stack underscores our commitment to interop and WS-*. WWSAPI supports a subset of WS-* and is not as full featured or extensible as WCF. It definitely has a smaller footprint than WCF and it also has higher throughput for the scenarios it supports. This is due to a reduced feature set and implementation in native code. It also interops on the wire with WCF.
To answer the specific questions below:
- The current performance of WCF is industry leading. Please see the link for comparison with WebSphere where WCF easily outperforms the competition: http://download.microsoft.com/download/4/8/6/486B4B4F-5A87-4B5C-BEEC-455290F83274/IBMPower570_WebSphere_7_%20NET_Benchmark_WinSrv2008.pdf
- For most WCF scenarios .NET 4 performance is similar, or slightly better than, WCF perf in .NET 3.5 SP1
- The article references does not show WWSAPI to be 10X faster than WCF (as the customer claims); it does show the working set of WWS (native) to be 0.5-1MB compared to 4.5MB for WCF (managed/.NET). Server throughput is also better for WWS than WCF, but as noted above, WCF is already industry leading. The fact that a highly tuned, native, less-feature-rich SOAP stack has higher server throughput on ping-like service is not concerning. The cost of any realistic service will dwarf the infrastructure cost of either WCF or WWS. Put another way, either stack should meet the performance needs of a service and the deciding factor should be whether you require a native or managed solution.
- The article does not state that most teams in MS are moving to WWSAPI as the customer notes. What is happening is that those teams which require a native SOAP stack can move to WWSAPI; the majority however are using WCF as they are managed.
- Finally, trying to wrap or PInvoke between the two is not recommended. If you are writing managed code use WCF; if you are writing native code use WWSAPI.
We generally need to have a quick set of performance counters to identify a performance issue with a service. Shown below are three new counters that you will find with WCF 4.0. I also want to emphasize on the Calls outstanding counter here since this is one very u
|
PercentOfMaxCalls
|
Reports # of active requests as % of max calls
|
Reports the numbers of messages being processed + in the wait queue as a percentage of MaxConcurrentCalls throttle
|
|
PercentOfMaxSessions
|
Reports # of active requests as % of max sessions
|
Reports the numbers of active instances + calls in the wait queue waiting for an instance as a percentage of MaxConcurrentInstances throttle
|
|
PercentOfMaxInstances
|
Reports # of active requests as % of max instances
|
Reports the numbers of messages in the wait queue due to MaxConcurrentSessions throttle
|
|
CallsOutstanding ( from 3.0)
|
Reports the # of calls waiting to be completed
|
Reports the number of in-progress calls to this service.
|
Here you see that the %Instance throttle has maxed out. What do you think we should do? You can guess what throttle we are hitting here.
Some common questions when trouble shooting performance problems:
Why are my clients timing out, the service is showing 100% for % of MaxConcurrentCalls?
% of MaxConcurrentCalls gives you an indication of how much closer you are to hitting your max throttle value. This means that if you have a max concurrent call of 10 and you have 10 outstanding calls then you have utilized 100% of your throttle and there is no more work that your service can do. So if you see that your % maxConcurrentCalls is very high it probably indicates you have a very low throttle value. Remember the default is 16 till v3.5 and 16* proc count for v4.0. Before 4.0 you needed to work this out by checking the calls outstanding throttle and your web.config to figure out this value. These new % performance counters would help you identify if you are hitting the max values from now.
What if % MaxConcurrentSessions is showing 100% ?
We have bumped up the default number of sessions for service throttling behavior to 100 per CPU. But then again if you are seeing that %MaxConcurrentSessions is very high this means that you have exhausted all your session. This is probably because your clients are not closing either proxies and ter
minating sessions when you have less clients or possibly due to a very small MaxConcurrentSessions configuration.
This is an amazing post by Wenlong giving you a full walkthrough of how we generally go about debugging a performance problems.
Crack Throttling: Debugging WCF TimeoutException for a Middle-Tier Application
One of the key takeaways from this is the emphasis of client settings in your service. These are as important on a server as it is on the client side when you have a middle tier. You have to keep in mind that the middle tier becomes the client for your back end service and all client settings and behaviors apply to your proxy in the middle tier including things like channel caching etc. The below diagram shows you one setting that is most commonly forgotten when we try to perform common tasks like
- Load testing your service (no middle tier)
- Load testing your backend service with a Middle tier which needs to saturate your back end process.

This was one was interesting as the service was exposed as a fully typed service, but the client wanted to modify some parts of the xml. Ideally you can plug into any part to perform these operations, but the general requirement here was that they needed a simple pointer to the Body and didn’t care much about anything else.
Here is an example how how you can access the body similar to an XElement by using the Message directly.
[code:c#]
[ServiceContract(Namespace="http://www.sajay.com/")]
public interface IClientProxy
{
[OperationContract(Action="http://www.sajay.com/getdata",
ReplyAction="http://www.sajay.com/getdataresponse")]
Message DoWork();
}
IClientProxy proxy = cf.CreateChannel();
Message msg = proxy.DoWork();
XNode node = XElement.ReadFrom(msg.GetReaderAtBodyContents());
[/code]
So the service can return a typed object, but the client doesn’t have to handle the message in the same way. Understanding the Message class here helps you appreciate how the serializer and encoder is decoupled from the message representation. Basically WCF does not constrain the native format of your message on either the producer’s or the consumer’s side. It merely conforms to some rules of invoking a set of abstract classes which you wire up by virtue of specifying the encoder and the serializer.
Though we have XML semantics around with abstract classes like the XmlReader/XmlDictionaryWriter etc in conjuction with the Message class, this doesn’t actually mean that WCF converts everything to XML before it can process the message. It merely uses these to invoke the required parts to obtain things like the header/body and other properties. The Message however can be represented natively in any form like binary/json/atom/ or any whacky type as long as you wire them up properly and can retrieve them in some manner.
This should give you some insight into how much more powerful the message model is compared to simple RPC style contracts. With 4.0 and workflow services, you will see this becoming more and more the default way of thinking about messages and services.
MessageToXElement.cs (2.90 kb)
Really good article on Performance of HTTP polling duplex server-side channel in Microsoft Silverlight 3 by Tomek from the ServiceModel/SL team.
http://tomasz.janczuk.org/2009/08/performance-of-http-polling-duplex.html