Tagged: WCF

Tip: C# Automatic Properties, k_BackingField and JSON

If you are using automatic properties to define your data objects and you are trying to serialize these objects to a JSON string using DataContractSerializer (WCF/3.5), make sure you mark your data classes with [DataContract] and [DataMember] attributes (even if you are not really using WCF), a simple [Serializable] attribute will not do. If you just use [Serializable], the emitted JSON string will have a prefix tag “k_backingField” and the string is all mumbo-jumbo!

So if you use automatic properties, your class should look like this 🙂

    public class Employee
        [DataMember(Name = "FirstName", Order = 1)]
        public string FirstName { get; set; }
        [DataMember(Name = "LastName", Order = 2)]
        public string LastName { get; set; }


9 good WCF Links

  1. Fundementals of WCF Security – Explains WCF Security, deployment scenarios etc. by Michele Leroux Bustamante. Also if you want to learn WCF in depth, Michele’s WCF book  is an invaluable resource.
  2. Section of MSDN that outlines WCF technology
  3. Some basic WCF design patterns from Rob Daigneau
  4. How to consume a WCF service – a primer
  5. ASP.Net Ajax and WCF – link .    Also see my earlier post here.
  6. From .Net Remoting to WCF – A good MSDN article.
  7. How to host a WCF service in IIS – MSDN article. For IIS 7.0 see this link.
  8. MSDN article on Federation (Security)
  9. Finally, the best and the required for all WCF developers: patterns and practices: WCF Security Guidance project – Download this PDF book and keep it with you. It explains WCF security and deployment scenarios. Skim thru and find out the scenario that fits your project and then follow the guidelines for that. Great resource !!!


“Could not establish trust relationship for the SSL/TLS secure channel with authority…”

If you are getting this error while consuming your WCF service, read this older post first. The other day I was connecting to one of our WCF services, and I got this infamous error. For few hours this challenged me, because it was working on dev server and I got this message when I moved this new client application to QA. It turned out to be a SSL Cert related issue, SSL Cert on that server was issued to a subject (A DNS Name)..and I was connecting using actual server name to connect to the end point…

Lesson Learned: When connecting to a secure end point, make sure that there is no cert based warning like different server/dns name, expired cert etc.


Using JSON – Some observations

When using JSON with Web Services (classic web service or WCF service), you might get an error “JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.” when your json string is too big. If you are using JavaScriptSerializer class to serilaize your object, then there is a property ser.MaxJsonLength that you can set to allow for more size. But somehow, I still got the error, and I had to make that setting in web.config file. If you are using DataContractJsonSerializer class (using System.Runtime.Serialization.Json namespace), then you don’t have MaxJsonLength property for that class, but adding configuration settings in web.config works like a charm.

Here is how you change MaxJsonLength using web.config:

        <jsonSerialization maxJsonLength=”2147483647″></jsonSerialization>