June 2010 - Posts

WCF Data Services provides an specific operator “$value” for retrieving the underline value of any of the properties in exposed resources. Let’s say you have a resource “Configuration” that exposes a field “Xml” whose content should be treated as a mime type “text/xml”. When you execute a query like this to retrieve the value of that field, “myService.svc/Configurations(1)/Xml/$value”, what you get is the content of that field but expressed as plain text (text/plain). This happens because the default behavior for WCF Data Services is to return most of the primitive types as “text/plain” and some others like byte arrays as “application/octet-stream”. So, how do you change that default behavior to retrieve property content with other mime types ?. Here is where the MimeTypeAttribute can be used to change that behavior.

The MimeTypeAttribute can actually only be used with the ReflectionProvider, which is the default provider for exposing objects that are not EF entities. If you want to change the mime type for the EF Provider, you need to modify the underline CSDL model (This is the part that is not well documented, and I only found some incomplete references in some forums).

For the reflection provider, the attribute must be specified at class level,

[MimeType("Xml", "text/xml")]
public class Configuration
  public string Xml { get; set; }

For the EF provider, a few additional changes are required in the CSDL model. Edit the CSDL by opening the EDM model in Xml view, and adding a reference to this namespace "xmlns:m1="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"" in the <edmx:ConceptualModels> node  of the CSDL section in the EDMX file. Right after that, locate the entity that you want to modify and add the MimeType definition in the property.

<EntityType Name="Configuration">
          <Property Name="Xml" m1:MimeType="text/xml" Type="String" MaxLength="Max" Unicode="true" FixedLength="false" />

Posted by cibrax

The book in which I been working on since last year finally went to the light this week. It has been the result of hard work between me and three other Connected Systems MVP, my friend Fabio Cozzolino, Kurt Claeys and Johann Grabner. If you are interested in learning the new features in WCF 4.0, but also WCF in general and how to apply it in real world scenarios, this book is for you. I dedicated three chapters of this book to one of my favorites topics, Security, from the basics to more complicated scenarios with Claim-Based security and Federated authentication using WCF services with Windows Identity Foundation. You can find more information about the book and the table of contents in the Wrox web site here.

Posted by cibrax | 1 comment(s)
Filed under: ,
More Posts