/// <summary> /// Fetches the metadata for a specified 'service'. This method is automatically called /// internally by an EntityManager before its first query against a new service. /// </summary> /// <param name="dataService"></param> /// <returns></returns> public async Task <DataService> FetchMetadata(DataService dataService) { String serviceName; serviceName = dataService.ServiceName; var ds = GetDataService(serviceName); if (ds != null) { return(dataService); } await _asyncSemaphore.WaitAsync(); String metadata; try { ds = GetDataService(serviceName); if (ds != null) { return(dataService); } metadata = await dataService.GetAsync("Metadata"); } catch (Exception e) { throw new Exception("Unable to locate metadata resource for: " + dataService.ServiceName, e); } finally { _asyncSemaphore.Release(); } metadata = metadata.Trim(); if (metadata.Substring(0, 1) == "\"" && metadata.Substring(metadata.Length - 1, 1) == "\"") { metadata = Regex.Unescape(metadata.Substring(1, metadata.Length - 2)); } var metadataProcessor = new CsdlMetadataProcessor(); metadataProcessor.ProcessMetadata(this, metadata); dataService.ServerMetadata = metadata; AddDataService(dataService); return(dataService); }
public async Task MetadataWithEmbeddedQuotes() { // this is a legacy service that has quoted metadata. var serviceName = "http://sampleservice.breezejs.com/api/todos/"; var ds = new DataService(serviceName); var metadata = await ds.GetAsync("Metadata", new CancellationToken(false)); var metadata2 = System.Text.RegularExpressions.Regex.Unescape(metadata).Trim('"'); var jo = (JObject) JsonConvert.DeserializeObject(metadata2); var em = new EntityManager(ds); try { var x = await em.FetchMetadata(); Assert.Fail("should not get here - CLR types for this metadata are not available"); } catch (Exception e) { Assert.IsTrue(e.Message.Contains("CLR Entity")); } }
/// <summary> /// Fetches the metadata for a specified 'service'. This method is automatically called /// internally by an EntityManager before its first query against a new service. /// </summary> /// <param name="dataService"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task <DataService> FetchMetadata(DataService dataService, CancellationToken cancellationToken) { String serviceName; serviceName = dataService.ServiceName; var ds = GetDataService(serviceName); if (ds != null) { return(dataService); } await _asyncSemaphore.WaitAsync(); cancellationToken.ThrowIfCancellationRequested(); String metadata; try { ds = GetDataService(serviceName); if (ds != null) { return(dataService); } metadata = await dataService.GetAsync("Metadata", cancellationToken); cancellationToken.ThrowIfCancellationRequested(); metadata = metadata.Trim(); // this section is needed if metadata is returned as an escaped string - ( not common but does happen ... ). if (metadata.Substring(0, 1) == "\"" && metadata.Substring(metadata.Length - 1, 1) == "\"") { metadata = Regex.Unescape(metadata.Substring(1, metadata.Length - 2)); } metadata = regExGeneric.Replace(metadata, string.Empty); var json = (JObject)JsonConvert.DeserializeObject(metadata); var schema = json["schema"]; if (schema != null) { // metadata returned in CSDL format var metadataProcessor = new CsdlMetadataProcessor(); metadataProcessor.ProcessMetadata(this, json); } else { // metadata returned in breeze native format this.ImportMetadata(metadata, true); } var errorMessages = GetMessages(MessageType.Error).ToList(); if (errorMessages.Any()) { throw new DomainException("Metadata errors encountered: \n" + errorMessages.ToAggregateString("\n")); } dataService.ServerMetadata = metadata; AddDataService(dataService); return(dataService); } catch (Exception e) { if (!(e is DomainException)) { throw new Exception("Unable to locate metadata resource for: " + dataService.ServiceName, e); } throw; } finally { _asyncSemaphore.Release(); } }