Create SOAP clients dynamic by WSDL
Asked Answered
B

0

6

I'm trying to convert this code, to work at .net core... This code reads a WSDL file, and dynamic generate the assembly (because I have multiple wsdl files). But this code not compile at core... because I don't have for example the CSharpCodeProvider class.

Anyone knows 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;
}
Benedetta answered 20/1, 2017 at 1:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.