private static void ProcessCSDLUri(ArgsParser parser) { Dictionary <string, Dictionary <string, string> > tablesToColumnMappingsInfo = new Dictionary <string, Dictionary <string, string> >(); ValidateCSDLMode(parser); string serviceUri = null; DbSyncScopeDescription scopeDescription = CSDLParser.GetDescriptionFromUri(parser, parser.CSDLUrl, out serviceUri); Log("Generating files..."); EntityGenerator generator = EntityGeneratorFactory.Create(parser.CodeGenMode, null /* null syncSchema - not needed for client code generation */); generator.GenerateEntities(parser.GeneratedFilePrefix, string.IsNullOrEmpty(parser.Namespace) ? string.IsNullOrEmpty(parser.GeneratedFilePrefix) ? scopeDescription.ScopeName : parser.GeneratedFilePrefix : parser.Namespace, scopeDescription, tablesToColumnMappingsInfo, parser.WorkingDirectory, parser.Language, serviceUri); }
public static DbSyncScopeDescription GetDescriptionFromUri(ArgsParser parser, string uriString, out string serviceUri) { // Issue the request. WebClient request = new WebClient(); SyncSvcUtil.Log("Trying to connect to Uri '{0}' to check for SyncScopeSchema document", uriString); string content = request.DownloadString(uriString); // Check to see if its a SyncScopes <services> document or an SyncScopeSchema <edmx> document. if (parser.UseVerbose) { SyncSvcUtil.Log("Download succeeded. Checking to see if its a SyncScope <Service> document or an SyncScopeSchema <edmx> document."); SyncSvcUtil.Log("Downloaded document content:\n {0}", content); } SyncSvcUtil.Log("Parsing downloaded document.", uriString); XDocument document = XDocument.Parse(content); if (document == null) { throw new CsdlException("Downloaded content is not a valid XML document."); } if (document.Root.Name.Equals(Constants.SyncScopeServicesElement)) { if (parser.UseVerbose) { SyncSvcUtil.Log("Found a <service> document. Checking for SyncScopes workspace."); } XElement workspaceElement = document.Root.Element(Constants.SyncScopeWorkspaceElement); if (workspaceElement == null) { throw new CsdlException("Remote SyncScope services document did not contain a <workspace> element."); } // Look for <collection> element XElement[] collectionElements = workspaceElement.Elements(Constants.SyncScopeWorkspaceCollectionElement).ToArray(); if (collectionElements.Length == 0) { throw new CsdlException("Remote SyncScope services document did not contain a <collection> element."); } else if (collectionElements.Length > 1) { SyncSvcUtil.Log("Multiple SyncScopes were found in the <service> document. Please specify the correct Url."); foreach (XElement elem in collectionElements) { XAttribute hrefAttr = elem.Attribute(Constants.SyncScopeWorkspaceCollectionHrefAttribute); SyncSvcUtil.Log("\t\t{0} - Uri: {1}{2}{0}/$metadata", hrefAttr.Value, parser.CSDLUrl, parser.CSDLUrl.EndsWith("/") ? string.Empty : "/"); } throw new CsdlException("Multiple SyncScopes found."); } else { // We have exactly one SyncScope. Download the schema for that XAttribute hrefAttr = collectionElements[0].Attribute(Constants.SyncScopeWorkspaceCollectionHrefAttribute); if (hrefAttr == null) { throw new CsdlException("No Href attribute was found in the <collection> element."); } // Ensure the href param is not empty as this is the scopeName if (string.IsNullOrEmpty(hrefAttr.Value)) { throw new CsdlException(string.Format("Href attribute in <collection> must have a non empty string.\n Content: {0}", collectionElements[0].ToString())); } // Look for and remove $syncScopes string origUrl = parser.CSDLUrl; if (origUrl.EndsWith("$syncscopes", StringComparison.InvariantCultureIgnoreCase)) { origUrl = origUrl.Substring(0, origUrl.LastIndexOf("/")); } uriString = string.Format("{0}{1}{2}/$metadata", origUrl, origUrl.EndsWith("/") ? string.Empty : "/", hrefAttr.Value); return(CSDLParser.GetDescriptionFromUri(parser, uriString, out serviceUri)); } } else if (document.Root.Name.Equals(Constants.SyncScopeEdmxElement)) { // Set the service URI and remove $metadata token from it. //Remove the / at the end if present. serviceUri = (uriString.EndsWith("/")) ? uriString.Substring(0, uriString.Length - 1) : uriString; //The service will render the schema only if there is a $metadata at the end in the Uri. serviceUri = serviceUri.Substring(0, serviceUri.Length - "/$metadata".Length); //Remove the scope name serviceUri = serviceUri.Substring(0, serviceUri.LastIndexOf("/") + 1); return(ParseCSDLDocument(parser, uriString, document)); } else { throw new CsdlException(string.Format("Downloaded XML content is not a valid <service> document. \nDocument Content: {0}", content)); } }