private static string GenerateCode(XmlReader reader, string nameSpace) { EntityClassGenerator generator = new EntityClassGenerator(LanguageOption.GenerateCSharpCode) { Version = DataServiceCodeVersion.V2 }; StringWriter targetWriter = new StringWriter(); try { IList <EdmSchemaError> list = generator.GenerateCode(reader, targetWriter, nameSpace); if (list.Count > 0) { throw new DisplayToUserException(string.Concat(new object[] { "Bad schema: ", list[0].Message, " (line ", list[0].Line, ")" })); } } catch (MetadataException exception) { throw new DisplayToUserException("MetadataException: " + exception.Message); } catch (XmlSchemaValidationException exception2) { throw new DisplayToUserException("This schema is unsupported.\r\n\r\nEntityClassGenerator returned the following error: " + exception2.Message); } catch (FileNotFoundException exception3) { if (exception3.Message.Contains("System.Data.Services.Design")) { throw new DisplayToUserException("Cannot load System.Data.Services.Design.dll. (A possible cause is installing only the .NET Framework Client Profile instead of the full .NET Framework.)"); } throw; } return(targetWriter.ToString()); }
private static string GenerateCode(XmlReader reader, string nameSpace) { EntityClassGenerator generator = new EntityClassGenerator(LanguageOption.GenerateCSharpCode) { Version = DataServiceCodeVersion.V2 }; StringWriter targetWriter = new StringWriter(); try { IList<EdmSchemaError> list = generator.GenerateCode(reader, targetWriter, nameSpace); if (list.Count > 0) { throw new DisplayToUserException(string.Concat(new object[] { "Bad schema: ", list[0].Message, " (line ", list[0].Line, ")" })); } } catch (MetadataException exception) { throw new DisplayToUserException("MetadataException: " + exception.Message); } catch (XmlSchemaValidationException exception2) { throw new DisplayToUserException("This schema is unsupported.\r\n\r\nEntityClassGenerator returned the following error: " + exception2.Message); } catch (FileNotFoundException exception3) { if (exception3.Message.Contains("System.Data.Services.Design")) { throw new DisplayToUserException("Cannot load System.Data.Services.Design.dll. (A possible cause is installing only the .NET Framework Client Profile instead of the full .NET Framework.)"); } throw; } return targetWriter.ToString(); }
public override async Task AddGeneratedClientCodeAsync() { await Context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, "Generating Client Proxy ...").ConfigureAwait(false); var generator = new EntityClassGenerator(this.TargetProjectLanguage) { UseDataServiceCollection = this.ServiceConfiguration.UseDataServiceCollection, Version = DataServiceCodeVersion.V3 }; // Set up XML secure resolver var xmlUrlResolver = new XmlUrlResolver { Credentials = System.Net.CredentialCache.DefaultNetworkCredentials }; var permissionSet = new PermissionSet(System.Security.Permissions.PermissionState.Unrestricted); var settings = new XmlReaderSettings { XmlResolver = new XmlSecureResolver(xmlUrlResolver, permissionSet) }; using (var reader = XmlReader.Create(this.MetadataUri, settings)) { var tempFile = Path.GetTempFileName(); var noErrors = true; using (StreamWriter writer = File.CreateText(tempFile)) { var errors = generator.GenerateCode(reader, writer, this.ServiceConfiguration.NamespacePrefix); await writer.FlushAsync().ConfigureAwait(false); if (errors != null && errors.Any()) { noErrors = false; foreach (var err in errors) { await Context.Logger.WriteMessageAsync(LoggerMessageCategory.Warning, err.Message).ConfigureAwait(false); } await Context.Logger.WriteMessageAsync(LoggerMessageCategory.Warning, "Client Proxy for OData V3 was not generated.").ConfigureAwait(false); } } if (noErrors) { var ext = this.TargetProjectLanguage == LanguageOption.GenerateCSharpCode ? ".cs" : ".vb"; var outputFile = Path.Combine(GetReferenceFileFolder(), this.GeneratedFileNamePrefix + ext); await Context.HandlerHelper.AddFileAsync(tempFile, outputFile, new AddFileOptions { OpenOnComplete = ServiceConfiguration.OpenGeneratedFilesInIDE }).ConfigureAwait(false); await Context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, "Client Proxy for OData V3 was generated.").ConfigureAwait(false); } } }
private async Task AddGeneratedCodeAsync(string metadataUri, string outputDirectory, Common.LanguageOption languageOption, ServiceConfiguration serviceConfiguration) { await MessageLogger.WriteMessageAsync(LogMessageCategory.Information, "Generating Client Proxy ..."); var generator = new EntityClassGenerator((System.Data.Services.Design.LanguageOption)languageOption) { UseDataServiceCollection = serviceConfiguration.UseDataServiceCollection, Version = DataServiceCodeVersion.V3 }; // Set up XML secure resolver var xmlUrlResolver = new XmlUrlResolver { Credentials = System.Net.CredentialCache.DefaultNetworkCredentials }; var permissionSet = new PermissionSet(System.Security.Permissions.PermissionState.Unrestricted); var settings = new XmlReaderSettings { XmlResolver = new XmlSecureResolver(xmlUrlResolver, permissionSet) }; using (var reader = XmlReader.Create(metadataUri, settings)) { var tempFile = Path.GetTempFileName(); var noErrors = true; using (StreamWriter writer = File.CreateText(tempFile)) { var errors = generator.GenerateCode(reader, writer, serviceConfiguration.NamespacePrefix); await writer.FlushAsync(); if (errors != null && errors.Any()) { noErrors = false; foreach (var err in errors) { await MessageLogger.WriteMessageAsync(LogMessageCategory.Warning, err.Message); } await MessageLogger.WriteMessageAsync(LogMessageCategory.Warning, "Client Proxy for OData V3 was not generated."); } } if (noErrors) { var ext = languageOption == Common.LanguageOption.GenerateCSharpCode ? ".cs" : ".vb"; var outputFile = Path.Combine(outputDirectory, this.GeneratedFileNamePrefix(serviceConfiguration.GeneratedFileNamePrefix) + ext); await FileHandler.AddFileAsync(tempFile, outputFile, new ODataFileOptions { OpenOnComplete = serviceConfiguration.OpenGeneratedFilesInIDE }); await MessageLogger.WriteMessageAsync(LogMessageCategory.Information, "Client Proxy for OData V3 was generated."); } } }
public override async Task AddGeneratedClientCodeAsync() { await this.Context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, "Generating Client Proxy for OData V3..."); var generator = new EntityClassGenerator(this.ServiceConfiguration.LanguageOption) { UseDataServiceCollection = this.ServiceConfiguration.UseDataServiceCollection, Version = DataServiceCodeVersion.V3 }; using (var reader = ServiceConfiguration.GetXmlReaderForEndpoint()) { var tempFile = Path.GetTempFileName(); var noErrors = true; using (var writer = File.CreateText(tempFile)) { var errors = generator.GenerateCode(reader, writer, this.ServiceConfiguration.NamespacePrefix); await writer.FlushAsync(); if (errors != null && errors.Any()) { noErrors = false; foreach (var err in errors) { await this.Context.Logger.WriteMessageAsync(LoggerMessageCategory.Warning, err.Message); } await this.Context.Logger.WriteMessageAsync(LoggerMessageCategory.Warning, "Client Proxy for OData V3 was not generated."); } } if (noErrors) { var ext = this.ServiceConfiguration.LanguageOption == LanguageOption.GenerateCSharpCode ? ".cs" : ".vb"; await Microsoft.VisualStudio.Shell.ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); var outputFile = Path.Combine(this.ReferenceFileFolder, this.GeneratedFileNamePrefix + ext); await this.Context.HandlerHelper.AddFileAsync(tempFile, outputFile, new AddFileOptions { OpenOnComplete = this.Instance.ServiceConfig.OpenGeneratedFilesOnComplete }); await this.Context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, "Client Proxy for OData V3 was generated."); if (ServiceConfiguration.IncludeExtensionsT4File && this.ServiceConfiguration.LanguageOption == LanguageOption.GenerateCSharpCode) { await AddGeneratedClientExtensionsCodeAsync(outputFile); } } } }
static string GenerateCode(XmlReader reader, string nameSpace) { // We're using EntityClassGenerator to generate C# code, which we'll compile in // the BuildAssembly method. (Another solution is to generate IL directly via Reflection.Emit.) var generator = new EntityClassGenerator (LanguageOption.GenerateCSharpCode); var writer = new StringWriter (); IList<EdmSchemaError> errors = generator.GenerateCode (reader, writer, nameSpace); // If we encounter errors, we can just throw an exception. if (errors.Count > 0) throw new Exception ("Bad schema: " + errors [0].Message + " (line " + errors [0].Line + ")"); return writer.ToString (); }
private async Task GenerateClientCodeWithoutDataSvcUtil(ConnectedServiceHandlerContext context, Project project) { await context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, "Generating Client Proxy ..."); ODataConnectedServiceInstance codeGenInstance = (ODataConnectedServiceInstance)context.ServiceInstance; var address = codeGenInstance.Endpoint; if (address.StartsWith("https:") || address.StartsWith("http")) { if (!address.EndsWith("$metadata")) { address = address.TrimEnd('/') + "/$metadata"; } } EntityClassGenerator generator = new EntityClassGenerator(LanguageOption.GenerateCSharpCode); generator.UseDataServiceCollection = codeGenInstance.UseDataServiceCollection; generator.Version = DataServiceCodeVersion.V3; var referenceFolderPath = await CheckAndAddReferenceFolder(context, project); string outputFile = Path.Combine(referenceFolderPath, "Reference.cs"); XmlReaderSettings settings = new XmlReaderSettings(); if (!String.IsNullOrEmpty(address)) { XmlResolver resolver = new XmlUrlResolver(); { resolver.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; } settings.XmlResolver = resolver; } using (XmlReader reader = XmlReader.Create(address, settings)) { using (StreamWriter writer = File.CreateText(outputFile)) { var error = generator.GenerateCode(reader, writer, codeGenInstance.NamespacePrefix); } } await context.HandlerHelper.AddFileAsync(outputFile, outputFile); }
public async override Task AddGeneratedClientCodeAsync() { await this.Context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, "Generating Client Proxy ..."); EntityClassGenerator generator = new EntityClassGenerator(LanguageOption.GenerateCSharpCode); generator.UseDataServiceCollection = this.ServiceConfiguration.UseDataServiceCollection; generator.Version = DataServiceCodeVersion.V3; // Set up XML secure resolver XmlUrlResolver xmlUrlResolver = new XmlUrlResolver() { Credentials = System.Net.CredentialCache.DefaultNetworkCredentials }; PermissionSet permissionSet = new PermissionSet(System.Security.Permissions.PermissionState.Unrestricted); XmlReaderSettings settings = new XmlReaderSettings() { XmlResolver = new XmlSecureResolver(xmlUrlResolver, permissionSet) }; using (XmlReader reader = XmlReader.Create(this.MetadataUri, settings)) { string tempFile = Path.GetTempFileName(); using (StreamWriter writer = File.CreateText(tempFile)) { var errors = generator.GenerateCode(reader, writer, this.ServiceConfiguration.NamespacePrefix); await writer.FlushAsync(); if (errors != null && errors.Count() > 0) { foreach (var err in errors) { await this.Context.Logger.WriteMessageAsync(LoggerMessageCategory.Warning, err.Message); } } } string outputFile = Path.Combine(GetReferenceFileFolder(), this.GeneratedFileNamePrefix + ".cs"); await this.Context.HandlerHelper.AddFileAsync(tempFile, outputFile, new AddFileOptions { OpenOnComplete = this.ServiceConfiguration.OpenGeneratedFilesInIDE }); } }
internal static string TestConnectionInternal(IConnectionInfo r) { IList <EdmSchemaError> list; EntityClassGenerator generator = new EntityClassGenerator(LanguageOption.GenerateCSharpCode) { Version = DataServiceCodeVersion.V2 }; StringWriter targetWriter = new StringWriter(); using (new PushDefaultWebProxy()) { using (XmlReader reader = GetSchemaReader(r)) { list = generator.GenerateCode(reader, targetWriter, null); } } if (list.Count == 0) { return(null); } return(list.First <EdmSchemaError>().Message); }
private static void CodeGen(FileInfo edmxFile, LanguageOption languageOption) { XDocument xdoc = XDocument.Load(edmxFile.FullName); XElement c = GetCsdlFromEdmx(xdoc); Version v = _namespaceManager.GetVersionFromEDMXDocument(xdoc); StringWriter sw = new StringWriter(); IList <EdmSchemaError> errors = null; // // code-gen uses different classes for V1 and V2 of the EF // if (v == EntityFrameworkVersions.Version1) { // generate code EntityClassGenerator codeGen = new EntityClassGenerator(languageOption); errors = codeGen.GenerateCode(c.CreateReader(), sw); } else if (v == EntityFrameworkVersions.Version2) { EntityCodeGenerator codeGen = new EntityCodeGenerator(languageOption); errors = codeGen.GenerateCode(c.CreateReader(), sw); } else if (v == EntityFrameworkVersions.Version3) { EntityCodeGenerator codeGen = new EntityCodeGenerator(languageOption); errors = codeGen.GenerateCode(c.CreateReader(), sw, EntityFrameworkVersions.Version3); } // write out code-file string outputFileName = GetFileNameWithNewExtension(edmxFile, GetFileExtensionForLanguageOption(languageOption)); File.WriteAllText(outputFileName, sw.ToString()); // output errors WriteErrors(errors); }
public async override Task AddGeneratedClientCode() { await this.Context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, "Generating Client Proxy ..."); EntityClassGenerator generator = new EntityClassGenerator(LanguageOption.GenerateCSharpCode); generator.UseDataServiceCollection = this.ServiceConfiguration.UseDataServiceCollection; generator.Version = DataServiceCodeVersion.V3; XmlReaderSettings settings = new XmlReaderSettings() { XmlResolver = new XmlUrlResolver() { Credentials = System.Net.CredentialCache.DefaultNetworkCredentials } }; using (XmlReader reader = XmlReader.Create(this.MetadataUri, settings)) { string tempFile = Path.GetTempFileName(); using (StreamWriter writer = File.CreateText(tempFile)) { var errors = generator.GenerateCode(reader, writer, this.ServiceConfiguration.NamespacePrefix); await writer.FlushAsync(); if (errors != null && errors.Count() > 0) { foreach (var err in errors) { await this.Context.Logger.WriteMessageAsync(LoggerMessageCategory.Warning, err.Message); } } } string outputFile = Path.Combine(GetReferenceFileFolder(), this.GeneratedFileNamePrefix + ".cs"); await this.Context.HandlerHelper.AddFileAsync(tempFile, outputFile); } }
internal static string TestConnectionInternal(IConnectionInfo r) { IList<EdmSchemaError> list; EntityClassGenerator generator = new EntityClassGenerator(LanguageOption.GenerateCSharpCode) { Version = DataServiceCodeVersion.V2 }; StringWriter targetWriter = new StringWriter(); using (new PushDefaultWebProxy()) { using (XmlReader reader = GetSchemaReader(r)) { list = generator.GenerateCode(reader, targetWriter, null); } } if (list.Count == 0) { return null; } return list.First<EdmSchemaError>().Message; }
public override async Task AddGeneratedClientCode() { await this.Context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, "Generating Client Proxy for OData V3..."); var generator = new EntityClassGenerator(this.ServiceConfiguration.LanguageOption) { UseDataServiceCollection = this.ServiceConfiguration.UseDataServiceCollection, Version = DataServiceCodeVersion.V3 }; var xmlUrlResolver = new XmlUrlResolver { Credentials = this.UseNetworkCredentials ? new NetworkCredential(this.NetworkCredentialsUserName, this.NetworkCredentialsPassword, this.NetworkCredentialsDomain) : System.Net.CredentialCache.DefaultNetworkCredentials }; if (this.UseWebProxy) { xmlUrlResolver.Proxy = new WebProxy(this.WebProxyUri, true); if (this.UseWebProxyCredentials) { xmlUrlResolver.Proxy = new WebProxy(this.WebProxyUri, true, new string[0], new NetworkCredential { UserName = WebProxyNetworkCredentialsUserName, Password = WebProxyNetworkCredentialsPassword, Domain = WebProxyNetworkCredentialsDomain }); } else { xmlUrlResolver.Proxy = new WebProxy(this.WebProxyUri); } } var permissionSet = new PermissionSet(System.Security.Permissions.PermissionState.Unrestricted); var settings = new XmlReaderSettings() { XmlResolver = new XmlSecureResolver(xmlUrlResolver, permissionSet) }; using (var reader = XmlReader.Create(this.MetadataUri, settings)) { var tempFile = Path.GetTempFileName(); var noErrors = true; using (var writer = File.CreateText(tempFile)) { var errors = generator.GenerateCode(reader, writer, this.ServiceConfiguration.NamespacePrefix); await writer.FlushAsync(); if (errors != null && errors.Any()) { noErrors = false; foreach (var err in errors) { await this.Context.Logger.WriteMessageAsync(LoggerMessageCategory.Warning, err.Message); } await this.Context.Logger.WriteMessageAsync(LoggerMessageCategory.Warning, "Client Proxy for OData V3 was not generated."); } } if (noErrors) { var ext = this.ServiceConfiguration.LanguageOption == LanguageOption.GenerateCSharpCode ? ".cs" : ".vb"; var outputFile = Path.Combine(GetReferenceFileFolder(), this.GeneratedFileNamePrefix + ext); await this.Context.HandlerHelper.AddFileAsync(tempFile, outputFile, new AddFileOptions { OpenOnComplete = this.Instance.ServiceConfig.OpenGeneratedFilesOnComplete }); await this.Context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, "Client Proxy for OData V3 was generated."); if (ServiceConfiguration.IncludeExtensionsT4File && this.ServiceConfiguration.LanguageOption == LanguageOption.GenerateCSharpCode) { await AddGeneratedClientExtensionsCode(outputFile); } } } }
/// <summary> /// Generates proxy classes /// </summary> /// <param name="generatorParams"></param> public void GenerateClientProxyClasses(GeneratorParams generatorParams) { _logger.LogInformation(Resources.Generating_Client_Proxy____); try { var edmxTmpFile = GetMetadata(generatorParams, out var version); if (version == Constants.EdmxVersion4) { throw new ArgumentException(string.Format(Resources.Wrong_edx_version, version)); } var generator = new EntityClassGenerator(LanguageOption.GenerateCSharpCode) { UseDataServiceCollection = true, Version = DataServiceCodeVersion.V3 }; // Set up XML secure resolver var xmlUrlResolver = new XmlUrlResolver { Credentials = CredentialCache.DefaultNetworkCredentials }; var permissionSet = new PermissionSet(System.Security.Permissions.PermissionState.Unrestricted); var settings = new XmlReaderSettings { XmlResolver = new XmlSecureResolver(xmlUrlResolver, permissionSet) }; var fileHandler = new FilesHandler(); using (var reader = XmlReader.Create(generatorParams.MetadataUri, settings)) { var tempFile = Path.GetTempFileName(); var noErrors = true; using (StreamWriter writer = File.CreateText(tempFile)) { var errors = generator.GenerateCode(reader, writer, generatorParams.NamespacePrefix); writer.Flush(); if (errors != null && errors.Any()) { noErrors = false; foreach (var err in errors) { _logger.LogError(err.Message); } _logger.LogError(Resources.Client_Proxy_for_OData_V3_was_not_generated_); } } if (noErrors) { var csFile = new FileInfo(Path.Combine(generatorParams.OutputDir, generatorParams.OutputFilename + ".cs")); _logger.LogInformation(string.Format(Resources.Writing_file__0_, csFile.FullName)); fileHandler.AddFileAsync(tempFile, csFile.FullName).ConfigureAwait(true); var edmxFile = new FileInfo(Path.Combine(generatorParams.OutputDir, generatorParams.OutputFilename + ".edmx")); _logger.LogInformation(string.Format(Resources.Writing_file__0_, edmxFile.FullName)); fileHandler.AddFileAsync(edmxTmpFile, edmxFile.FullName).ConfigureAwait(true); foreach (var pluginCommand in generatorParams.Plugins) { var plugin = PluginCreator.Create(_logger, generatorParams, pluginCommand); plugin.Execute(); } } } _logger.LogInformation(Resources.Client_Proxy_for_OData_V3_was_generated_); } catch (Exception e) { _logger.LogCritical(Resources.Errors_during_generation_Client_Proxy_for_OData_V3, e); throw; } }
private async Task GenerateClientCodeWithoutDataSvcUtil(ConnectedServiceHandlerContext context, Project project) { await context.Logger.WriteMessageAsync(LoggerMessageCategory.Information, "Generating Client Proxy ..."); ODataConnectedServiceInstance codeGenInstance = (ODataConnectedServiceInstance)context.ServiceInstance; string address = codeGenInstance.Endpoint; if (address == null) { throw new Exception("Please input the service endpoint"); } if (address.StartsWith("https:") || address.StartsWith("http")) { if (!address.EndsWith("$metadata")) { address = address.TrimEnd('/') + "/$metadata"; } } EntityClassGenerator generator = new EntityClassGenerator(LanguageOption.GenerateCSharpCode); generator.UseDataServiceCollection = codeGenInstance.UseDataServiceCollection; generator.Version = DataServiceCodeVersion.V3; string referenceFolderPath = GetReferenceFolderName(context, project); string outputFile = Path.Combine(referenceFolderPath, "Reference.cs"); XmlReaderSettings settings = new XmlReaderSettings(); if (!String.IsNullOrEmpty(address)) { XmlResolver resolver = new XmlUrlResolver(); { resolver.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; } settings.XmlResolver = resolver; } XmlReader reader = null; try { reader = XmlReader.Create(address, settings); string tempFile = Path.GetTempFileName(); using (StreamWriter writer = File.CreateText(tempFile)) { var errors = generator.GenerateCode(reader, writer, codeGenInstance.NamespacePrefix); if (errors != null && errors.Count() > 0) { foreach (var err in errors) { await context.Logger.WriteMessageAsync(LoggerMessageCategory.Warning, err.Message); } } } reader.Close(); await context.HandlerHelper.AddFileAsync(tempFile, outputFile); } catch (WebException e) { throw new Exception(string.Format("Cannot access {0}", address), e); } finally { if (reader != null) { reader.Dispose(); reader = null; } } }