Search code examples
wcfpropertiesreadonlydatacontract

Readonly property on WCF DataContract suddenly gives exceptions on one client


Recently, we've been having a problem with our WCF service. This is how it's set up:

  • The service has the necessary ServiceContracts and DataContracts
  • In a DataContract, there's a readonly property (see below for code)
  • The client uses the same DLL with the Service- and DataContracts (so no WSDL)

This is the readonly property:

Dictionary<string, string> _list;

[DataMember]
public IDictionary<string, string> Fields
{
    get
    {
        if (_list == null)
            _list = new Dictionary<string, string>();
        return _list;
    }
}

The client 'sets' this property easily by using the Fields.Add() method.

Now recently we've been getting an exception that Fields should have a setter. I've read this on several other places, but this is the strange thing:

  • It worked fine on the client one day, not anymore the other day
  • It works fine on other clients, and has been working fine for about 1-2 year(s)

So why this sudden change? Could it be a different .NET Framework (the client may have updated overnight)? Are readonly properties really not supported in WCF, because it seems to work fine on other clients?


Solution

  • I never got any news about this anymore, and I'm no longer on that project, so here are some hints at solving this, if you encounter the same problem:

    • Code is running in Partial Trust mode
    • Wrong version of .NET Framework installed (.NET 3.0 SP1 needed)
    • Don't use Service References

    Clearly, in my case, the problem was down to one specific client. So the first two options are a possibility.