public string GenFromXsd( string uri, string targetNamespace, string outputFile) { var pd = new prepareData(); var msg = checkAndPrepareData(uri, outputFile, targetNamespace, pd); if (!msg.IsNullOrWhiteSpace()) { return(msg); } var sb = DynamicCodeHelper.GenerateCodeFromXsd(XDocument.Load(pd.MainTempFile), pd.TargetCSNamespace, pd.Imperts.Select(x => XDocument.Load(x)).ToArray()); File.WriteAllText(outputFile, sb.ToString()); return(null); }
public string GenFromWsdl( string uri, bool createAsync, string targetNamespace, string outputFile, Boolean generateClient) { var pd = new prepareData(); var msg = checkAndPrepareData(uri, outputFile, targetNamespace, pd); if (!msg.IsNullOrWhiteSpace()) { return(msg); } MetadataSet mdSet = new MetadataSet(); mdSet.MetadataSections.Add(MetadataSection.CreateFromServiceDescription(WebDescription.ServiceDescription.Read(pd.MainTempFile))); foreach (var item in pd.Imperts) { using (var xr = XmlReader.Create(item)) mdSet.MetadataSections.Add(MetadataSection.CreateFromSchema(XmlSchema.Read(xr, null))); } WsdlImporter importer = new WsdlImporter(mdSet); var xsdDCImporter = new XsdDataContractImporter(); xsdDCImporter.Options = new ImportOptions(); xsdDCImporter.Options.Namespaces.Add("*", pd.TargetCSNamespace); importer.State.Add(typeof(XsdDataContractImporter), xsdDCImporter); var xmlOptions = new XmlSerializerImportOptions(); xmlOptions.ClrNamespace = pd.TargetCSNamespace; importer.State.Add(typeof(XmlSerializerImportOptions), xmlOptions); var generator = new ServiceContractGenerator(); generator.NamespaceMappings.Add("*", pd.TargetCSNamespace); //var options = ServiceContractGenerationOptions.TypedMessages; var options = ServiceContractGenerationOptions.None; if (generateClient) { options |= ServiceContractGenerationOptions.ClientClass; } if (createAsync) { options |= ServiceContractGenerationOptions.TaskBasedAsynchronousMethod; } generator.Options = options; foreach (var contract in importer.ImportAllContracts()) { generator.GenerateServiceContractType(contract); } if (generator.Errors.Count != 0) { return(generator.Errors.Select(x => x.Message).JoinValuesToString(separator: Environment.NewLine)); } StringBuilder sb = new StringBuilder(); using (var sw = new StringWriter(sb)) CodeDomProvider.CreateProvider("C#") .GenerateCodeFromCompileUnit( generator.TargetCompileUnit, new IndentedTextWriter(sw), new CodeGeneratorOptions() { BracingStyle = "C" }); File.WriteAllText(outputFile, sb.ToString()); return(null); }
private string checkAndPrepareData( string uri, string outputFile, string targetNamespace, prepareData pd) { if (uri.IsNullOrWhiteSpace()) { return("Не указан uri"); } var tempPath = Path.Combine(DomainContext.TempPath, uri.ComputeMD5ChecksumString().ToString()); if (Directory.Exists(tempPath)) { Utils.DeleteDirectory(tempPath); } Directory.CreateDirectory(tempPath); var mainTempFile = Path.Combine(tempPath, uri.ComputeMD5ChecksumString().ToString()); if (File.Exists(mainTempFile)) { File.Delete(mainTempFile); } var sourseUri = new Uri(uri); if (!sourseUri.IsFile) { if (outputFile.IsNullOrWhiteSpace()) { return("Не указан выходной файл"); } HttpDownloadFile(uri, mainTempFile); } else { File.Copy(uri, mainTempFile); } String sourseDir = null; if (outputFile.IsNullOrWhiteSpace()) { sourseDir = Path.GetDirectoryName(uri); outputFile = Path.Combine(sourseDir, Path.GetFileNameWithoutExtension(uri) + ".cs"); try { // проверяем доступность места записи if (File.Exists(outputFile)) { File.Delete(outputFile); } File.WriteAllText(outputFile, null); } catch (Exception ex) { return("Нет доступа к месту назначения результурующего файла. Ошибка: " + ex.Message); } if (targetNamespace.IsNullOrWhiteSpace()) { targetNamespace = Path.GetFileNameWithoutExtension(uri); } } #region проверяем доступность места записи if (File.Exists(outputFile)) { File.Delete(outputFile); } File.WriteAllText(outputFile, null); File.Delete(outputFile); #endregion #region проверка пространства имен if (targetNamespace.IsNullOrWhiteSpace()) { targetNamespace = "CodeFromWsdl_" + DateTime.Now.ToString("yyyy_MM_dd__HH_mm_ss"); } targetNamespace = targetNamespace.Trim('.'); if (Char.IsDigit(targetNamespace[0])) { targetNamespace = "_" + targetNamespace; } foreach (var c in targetNamespace) { if (!char.IsDigit(c) & !char.IsLetter(c) & c != '_' & c != '.') { return("В прострвнстве имен могут быть только буквы, цифры, знак подчеркивания и точка"); } } #endregion pd.Imperts = downloadImport(mainTempFile, tempPath, sourseUri, sourseUri.IsFile ? sourseUri.AbsolutePath : null); pd.TargetCSNamespace = targetNamespace; pd.MainTempFile = mainTempFile; return(null); }