Creating a proxy class from WSDL on the fly?

I currently work in a Windows service that loads scripts at startup and compiles them for scheduled use, however some of these scripts must have access to ASMX web services.

My preference would be to use these WSDL files in your code to generate a .vb file ready for compilation.

How can I achieve this without the command line?

+4
source share
1 answer

I really don't understand why you don't want to use native / legacy-Tools from CommandLine, but here you go:

var wsdlDescription = ServiceDescription.Read(YourWSDLFile);
      var wsdlImporter = new ServiceDescriptionImporter();
      wsdlImporter.ProtocolName = "Soap12"; //Might differ
      wsdlImporter.AddServiceDescription(wsdlDescription, null, null);
      wsdlImporter.Style = ServiceDescriptionImportStyle.Server;
      wsdlImporter.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties;
      var codeNamespace = new CodeNamespace();
      var codeUnit = new CodeCompileUnit();
      codeUnit.Namespaces.Add(codeNamespace);
      var importWarning = wsdlImporter.Import(codeNamespace, codeUnit);
      if (importWarning == 0) {
        var stringBuilder = new StringBuilder();
        var stringWriter = new StringWriter(stringBuilder);
        var codeProvider = CodeDomProvider.CreateProvider("Vb");
        codeProvider.GenerateCodeFromCompileUnit(codeUnit, stringWriter, new CodeGeneratorOptions());
        stringWriter.Close();
        File.WriteAllText(WhereYouWantYourClass, stringBuilder.ToString(), Encoding.UTF8);

      } else {

        Console.WriteLine(importWarning);

      }

Note

System.CodeDom System.CodeDom.Compiler
#, CodeDomProvider.CreateProvider("Vb") CodeDomProvider.CreateProvider("CSharp")
WSDL, .

+2

Source: https://habr.com/ru/post/1648516/


All Articles