Definition:
CTP stands for Community Technology Preview, another fancy term for a ‘beta’ or ‘prototype’.
RTM stands for Release To Manufacturing or Release To Market.
CTP stands for Community Technology Preview, another fancy term for a ‘beta’ or ‘prototype’.
RTM stands for Release To Manufacturing or Release To Market.
I came across these two posts on Eric Lippert’s blog yesterday which I found very interesting:
Representation and Identity
Cast operators do not obey the distributive law
The blog posts go into a lot of details but long story short, if you box a value type you can’t unbox it to another type:

It would result in an InvalidCastException, and the big question is, if there exists a conversion from int to long why can’t the runtime work it out?
Well, consider three simple types A, B and C, where B inherits from A and C provides an explicit converter from A:
public class A {}
public class B : A {}
public class C
{
public static explicit operator C(A a)
{
return new C();
}
}
There exists two types of conversion which you can achieve using the Casting operator:
var b = new B(); var a = ( A ) b; // inheritance-based conversion, equivalent to var a = b as A; var c = ( C ) a; // operator-based conversion
In essence, the inheritance-based conversion is just showing the same object in a ‘new light’, whereas the operator-based conversion requires a special treatment on a case-by-case basis in the form of conversion methods.
| Inheritance-based Conversion
(representation-preserving) |
Operator-based Conversion
(representation-changing) |
|
| New object is | not constructed | constructed |
| New variable points to | original object | new object |
| Changing the new variable | changes the original object | doesn’t change the original object |
| Conversion is | fast | slow |
A box value type is an object, so as far as the compiler is concerned it could be anything. Therefore by allowing a boxed value type to be cast to a different type you introduce new challenges to the compiler:
Unfortunately the cost of meeting these challenges is performance, and the sensible default was to be “fast and precise” but still allowing this type of conversion through the Convert class.
Towards the end of the post there was a warning to those looking to abuse the new dynamic type support in C# 4 ;-)
“…if the argument to the cast is of type “dynamic” instead of object. The compiler actually generates code which starts a mini version of the compiler up again at runtime, does all that analysis, and spits fresh code. This is NOT FAST, but it is accurate, if that’s what you really need. (And the spit code is then cached so that the next time this call site is hit, it is much faster.)…”
If you try to serialize/deserialize a type which uses the generic Dictionary<TKey, TValue> type with the XmlSerializer then you’ll get an InvalidOperationException, for instance:
Here’s my class:
public class MyClass
{
// need a parameterless constructor for serialization
public MyClass()
{
MyDictionary = new Dictionary<string, string>();
}
public Dictionary<string, string> MyDictionary { get; set; }
}
I’ll get an InvalidOperationException with an inner exception of type NotSupportedException when I do this:
var xmlSerializer = new XmlSerializer(typeof(MyClass));
And the error message of the NotSupportedException is:
“Cannot serialize member MyClass.MyDictionary of type System.Collections.Generic.Dictionary‘2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], because it implements IDictionary.”
The reason why Dictionary<TKey, TValue> is not supported by the XmlSerializer is that types such as Dictionary, HashTable, etc. needs an equality comparer which can’t be serialized into XML easily and won’t be portable anyhow. To get around this problem, you generally have two options, in the order of my personal preference:
The easiest, and cleanest solution is to switch over to data contract serialization, and the only change required would be to mark your class with the DataContractAttribute and mark the properties you want to serialize with the DataMemberAttribute. My class would now look like this:
[DataContract]
public class MyClass
{
// need a parameterless constructor for serialization
public MyClass()
{
MyDictionary = new Dictionary<string, string>();
}
[DataMember]
public Dictionary<string, string> MyDictionary { get; set; }
}
And to serialize it into Xml, just use the DataContractSerializer and write the output with a XmlTextWriter like this:
var serializer = new DataContractSerializer(typeof(MyClass));
string xmlString;
using (var sw = new StringWriter())
{
using (var writer = new XmlTextWriter(sw))
{
writer.Formatting = Formatting.Indented; // indent the Xml so it's human readable
serializer.WriteObject(writer, marketplace);
writer.Flush();
xmlString = sw.ToString();
}
}
The alternative is to create your own Dictionary implementation which is Xml serializable. See the references section for an implementation Paul Welter has created.
As good and innovative as WCF is, it also introduced a lot of new complexities and whilst it is easy to get something up and running quickly it takes much more understanding to make your service perform as well as it could.
There are many things you need to consider such as binding types, serialization type, Datatable or POCO, etc. etc. and any of these choices can have a telling effect on the overall performance of your WCF service. Scott Weinstein wrote a very good article on how to create a high performance WCF service (see reference section) using 6 simple steps.
Without going into the subjects of binding selection and data normalization I want to just focus on how you can achieve greater concurrency because for services hosted on the web you won’t be able to use NetTcpBinding and data normalization is almost irrelevant because you won’t (or at least shouldn’t!) be sending large amounts of data back and forth.
Generally speaking, the most common way to improve the performance of a WCF service is to encourage greater concurrency and if you have used WCF before then chances are you’ve had to change the default throttling behaviour configuration because the defaults are too low for any real world applications to be useful.
These defaults are set to ensure your service is safe from DOS attacks but unfortunately also means your service will run in lock-down mode by default. They have since been raised to more sensible numbers in the new WCF 4 release:
| MaxConcurrentSessions | MaxConcurrentCalls | MaxConcurrentInstances | |
| WCF 3.5 SP1 | 10 | 16 | 26 |
| WCF 4 | 100 * Processor Count | 16 * Processor Count | 116 * Processor Count |
The new defaults in WCF 4 should provide a good guideline for you when configuring the ServiceThrottlingBehavior of your service (assuming you’re not using WCF 4 already).
The InstanceContextMode also plays a significant role in the overall performance of your service, and of the three options available to you – PerCall, PerSession and Singleton – you should consider PerCall or PerSession for a highly scalable WCF service.
Whilst the PerCall instance context mode is generally regarded as the most scalable option it does carry with it the need to create a instance of your class for each request and you need to ensure that 1) you have a parameterless constructor, and 2) this constructor should do as little as possible. If there are any significant steps that need to be performed, such as loading some reference data, you should avoid doing these in the parameterless constructor so they aren’t performed for each request:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class MyService : IMyService
{
public MyService()
{
ReferenceData = LoadReferenceData(); // will be called for EACH request…
}
public MyReferenceData ReferenceData { get; private set; }
…
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class MyService2 : IMyService
{
// called the first time the reference data is used and shared across all instances
private static MyReferenceData ReferenceData = LoadReferenceData();
public MyService2()
{
// ideal constructor which does nothing
}
…
}
In cases where the initialization steps are lengthy and unavoidable, or your class require a number of parameters in the constructor (for instance, when you programmatically host a service retrieve from an IoC container) the parameterless constructor can become a problem. To get around this, you could create a wrapper for your class and expose the wrapper as the service instead but hold a static instance of the underlying service which all the requests are passed on to:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class MyServiceWrapper : IMyServiceWrapper
{
// get the underlying service from the container
private static IMyService MyService = _container.Resolve<IMyService>();
public MyServiceWrapper()
{
// parameterless constructor which does nothing, so easy to constructor
}
public void DoSomething()
{
MyService.DoSomething();
}
}
// dummy interface to ensure the wrapper has the same methods as the underlying service
// but helps to avoid confusion
public interface IMyServiceWrapper : IMyService
{
}
For a sessionful service, the PerSession instance context mode gives you all the benefit of the PerCall instance context mode and at the same time reduces the overhead you pay for that extra concurrency because new instances of your class are no longer created for each request but for each session instead.
If your service is session dependant then you should definitely go with PerSession, but beware, if the channel does not create a session your service will behave as if it was a PerCall service.
Perhaps the most overlooked aspect when it comes to increasing concurrency of a WCF service. If you’ve set your service to use PerCall or PerSession instance context mode and upped the throttling settings, but are still not getting the response times you’re looking for, then it’s worth investigating whether the calls are being queued because there is not enough IO threads in the ThreadPool to handle the requests.
You can establish whether or not the requests are actually taking longer to process under load (as opposed to being queued at a service level) either by profiling locally or using some form of run-time logging (I wrote a LogExecutionTime attribute which might come in handy). If the calls aren’t taking longer to process and you’re not seeing very high CPU utilisation then the increase in response time is likely a result of the request being queued whilst WCF waits for a new IO thread to be made available to handle the request.
WCF uses the IO threads from the ThreadPool to handle requests and by default, the ThreadPool keeps one IO thread around for each CPU. So on a single core machine that means you only have ONE available IO thread to start with, and when more IO threads are needed they’re created by the ThreadPool with a delay:
“The thread pool maintains a minimum number of idle threads. For worker threads, the default value of this minimum is the number of processors. The GetMinThreads method obtains the minimum numbers of idle worker and I/O completion threads.
When all thread pool threads have been assigned to tasks, the thread pool does not immediately begin creating new idle threads. To avoid unnecessarily allocating stack space for threads, it creates new idle threads at intervals. The interval is currently half a second, although it could change in future versions of the .NET Framework.
If an application is subject to bursts of activity in which large numbers of thread pool tasks are queued, use the SetMinThreads method to increase the minimum number of idle threads. Otherwise, the built-in delay in creating new idle threads could cause a bottleneck.”
However, as WenLong Dong pointed out in his blog (see references section), raising the MinIOThreads setting in the ThreadPool doesn’t work as you’d expect in .Net 3.5 because of a known issue with the ThreadPool which has since been fixed in .Net 4. So if you’re still running .Net 3.5 like most of us, then you will need to go and grab the hotfix from here:
http://support.microsoft.com/kb/976898
Performance tuning isn’t an exact science, and you have to make a case by case judgement on how best to approach your performance issues. Encouraging greater concurrency is just one of the ways you can improve performance, but it’s by no means a silver bullet! In fact, if you go too far down the concurrency route you could find yourself facing a number of problems:
WenLong Dong’s post on WCF responses being slow and SetMinThreads does not work
WenLong Dong’s post on WCF request throttling and Server Scalability
WenLong Dong’s post on WCF becoming slow after being idle for 15 seconds
Scott Weinstein’s post on creating high performance WCF services
Dan Rigsby’s post on throttling WCF service and maintaining scalability
By default, arithmetic operations and conversions in C# are executed in an unchecked context. This means that for a signed integer it overflows from int.MaxValue to int.MinValue and underflows from int.MinValue to int.MaxValue, hence both statements below evaluates to true:
(int.MinValue – 1) == int.MaxValue; (int.MaxValue + 1) == int.MinValue;
Similarly, for an unsigned integer it will underflow from 0 to uint.MaxValue and overflow from uint.MaxValue to 0:
(0 – 1) == uint.MaxValue; // uint.MinValue = 0 (uint.MaxValue + 1) == 0;
This also applies to conversions too:
(int)((long) int.MaxValue + 1) == int.MinValue; // true
This default behaviour of swallowing the exceptions seems rather strange and unexpected to me, given that when an overflow happens it’s usually of interest to me as a developer to know about it and deal with it as appropriate because:
With that said, unchecked context performs significantly better than checked implementation, which is probably why it was chosen as the default.
Also, you will probably want to use unchecked blocks for calculating hash codes where it’s the bit patterns that matters not the magnitude of the integer value, i.e.:
public class MyClass
{
…
public override int GetHashCode()
{
unchecked
{
return ….
}
}
}
There are two things to keep in mind when using checked/unchecked blocks:
They’re always LOCAL to the method
Which means if you call another method from within a checked block the method will still execute in the default context (unchecked):
checked
{
// this method will still execute in unchecked context
DoSomethingThatOverflows();
}
...
public void DoSomethingThatOverflows()
{
// no overflow exception is thrown...
var overflowed = int.MaxValue + 1;
}
The context a line of code executes in is determined by the MOST inner checked/unchecked statement block
Hence the following code will execute in unchecked context:
checked
{
unchecked
{
var over = int.MaxValue + 1;
}
}
If you require arithmetic overflow/underflow checking on a project-wide scale, there is a property you can set from within Visual Studio. Go to project properties and find the Build tab, click “Advanced…” and tick the “Check for arithmetic overflow/underflow” box. But remember, you would probably still want to make sure GetHashCode is executed in an unchecked context.