WCF RIA Services XMLMetaDataProvider Problem

The current RIA Services project I’m working on has been using the XMLMetaDataProvider due to the fact that our model and DomainService are in different projects.  This rules out defining your metadata in a partial class for each entity.   As the project got bigger we noticed that randomly our metadata was not being created at build time.  Turns out there is a race condition with loading the XML file and the requests for metadata.  Here is the new XmlMetadataProvider.cs file where we now load the dataset in the constructor, not the first time it is requested.  After all that, I think we’ll try using Nikhil Kothari’s fluent API style, I like the look of it over the mass amount of XML.

   1: internal class XmlMetadataProvider : TypeDescriptionProvider
   2:     {
   3:         private Type domainServiceType;
   4:         private MetadataSet metadataSet;
   5:  
   6:         /// <summary>
   7:         /// Constructor that accepts a metadata context to use when generating custom type descriptors
   8:         /// </summary>
   9:         /// <param name="existingProvider">The parent TDP instance</param>
  10:         /// <param name="domainServiceType">The DomainService Type exposing the entity Types this provider will be registered for</param>
  11:         public XmlMetadataProvider(TypeDescriptionProvider existingProvider, Type domainServiceType)
  12:             : base(existingProvider)
  13:         {
  14:             this.domainServiceType = domainServiceType;
  15:             LoadMetadataSet();
  16:         }
  17:  
  18:         public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
  19:         {
  20:             return new XmlMetadataTypeDescriptor(this.MetadataSet, objectType, base.GetTypeDescriptor(objectType, instance));
  21:         }
  22:  
  23:         private MetadataSet MetadataSet
  24:         {
  25:             get
  26:             {
  27:                 if (metadataSet == null)
  28:                 {
  29:                     LoadMetadataSet();
  30:                 }
  31:                 return metadataSet;
  32:             }
  33:         }
  34:  
  35:         private void LoadMetadataSet()
  36:         {
  37:             metadataSet = new MetadataSet();
  38:  
  39:             // load all the metadata files found
  40:             IEnumerable<string> metadataFiles = domainServiceType.Assembly.GetManifestResourceNames().Where(p => p.EndsWith(".metadata.xml", StringComparison.OrdinalIgnoreCase));
  41:             foreach (string metadataFile in metadataFiles)
  42:             {
  43:                 Stream stream = domainServiceType.Assembly.GetManifestResourceStream(metadataFile);
  44:                 StreamReader streamReader = new StreamReader(stream);
  45:                 String metadata = streamReader.ReadToEnd();
  46:  
  47:                 try
  48:                 {
  49:                     metadataSet.Load(metadata);
  50:                 }
  51:                 catch (Exception e)
  52:                 {
  53:                     string msg = string.Format(CultureInfo.CurrentCulture, Resource.MetadataLoadError, metadataFile) + " : " + e.Message;
  54:                     throw new InvalidOperationException(msg, e);
  55:                 }
  56:  
  57:             }
  58:         }
  59:     }
  60: }