Creating SOAP Clients Using WSDL

I am trying to convert this code to work in the .net kernel ... This code reads the WSDL file, and the dynamic one creates the assembly (because I have several wsdl files). But this code does not compile in the kernel ... because I do not have, for example, the CSharpCodeProvider class.

Does anyone know a better way?

private void Teste()
{
    var ass = CriarAssemblie(wsdl);
    dynamic service = Activator.CreateInstance(ass.GetType("Type1"));
}

private Assembly CreateAssembly(Stream wsdlFile)
{
    var serviceDescription = ServiceDescription.Read(wsdlFile);
    StringWriter strWriter = new StringWriter(CultureInfo.CurrentCulture);
    CSharpCodeProvider cProvider = new CSharpCodeProvider();
    cProvider.GenerateCodeFromNamespace(GerarNameSpace(serviceDescription), strWriter, null);

    string codigoClasse = strWriter.ToString();

    CompilerParameters parameters = new CompilerParameters(new string[] { "System.dll", "System.Xml.dll", "System.Web.Services.dll", "System.Data.dll" });
    parameters.GenerateExecutable = false;
    parameters.GenerateInMemory = true;
    parameters.TreatWarningsAsErrors = false;
    parameters.WarningLevel = 4;

    CompilerResults results = cProvider.CompileAssemblyFromSource(parameters, codigoClasse);
    return results.CompiledAssembly;
}

private CodeNamespace GerarNameSpace(ServiceDescription serviceDescription)
{
    var importer = new ServiceDescriptionImporter();
    importer.AddServiceDescription(serviceDescription, string.Empty, string.Empty);
    importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties;

    var @namespace = new CodeNamespace();
    var unit = new CodeCompileUnit();
    unit.Namespaces.Add(@namespace);
    importer.Import(@namespace, unit);

    return @namespace;
}

XDocument parseXmlDocument(XmlElement ele)
{
    var xml = new XmlDocument();
    xml.LoadXml(ele.OuterXml);

    return xml;
}
+4
source share

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


All Articles