Home / Programming / Value can not be null. Parameter name: serviceType

Value can not be null. Parameter name: serviceType

WCF itself isn’t inherently compliated, but learning WCF presents many stumbling blocks. The terminology can be somewhat convoluted, and WCF development and debugging often boils down to making sure many different parts of disparate XML files are edited correctly.

However, the true pain lies in the exceptionally unhelpful error messages. When developing a WCF service that is hosted in a Windows Service (a net/tcp WCF server, for example), you may be presented with this error:

Value can not be null. Parameter name: serviceType

Of course, your code doesn’t have any parameters named “serviceType”. You can’t debug this error because your service can’t even start. “Value can not be null. Parameter name: serviceType” is all you’ve got.

After many hours of frustration, the solution is rather simple. The error happens when the windows service tries to create the WCF server, via this call:

protected override ServiceHost CreateServiceHost(
Type serviceType,
Uri[] baseAddresses
)

CreateServiceHost cannot load the Type information for your WCF service, either because the namespace is wrong of the file itself cannot be loaded. For example, assume your WCF service is defined with the interface IMyService, and implemented in the namespace MyNamespace.MyServiceImplementation, and the file MyServiceImplementation.dll. For one reason or another, MyNamespace.MyServiceImplementation cannot be loaded. Most often this is because of a typo in your namespace declaration. Check both the .config file and the .cs file where it is implementged. Another common problem is that the .dll was not copied to the correct directory. Verify that the MyServiceImplementation.dll is in the same folder as the windows service that is trying to load it. Check your project references or automated build scripts if this was the cause of the error.