Dynamically create a proxy or call a web service from a WSDL.

Hi,

How can I dynamically create a proxy or call a web service from a WSDL.

I need to call a variable number of web services which all implement a known
method.

I can call the services by creating one proxy and changing the URL
property. This fails though because the parameters to
WebServiceBindingAttribute and SoapRpcMethodAttribute are not correct.

Is it possible to change these attributes at runtime
Is there another way

Thanks


Answer this question

Dynamically create a proxy or call a web service from a WSDL.

  • Koukai Chou

    Thanks Igor,

    I have followed your steps and came up with following solution.

        


    System.Diagnostics.Process myProcess = new System.Diagnostics.Process(); String encode_url = MD5Hash(url);
    if (System.IO.File.Exists("compile_"+ encode_url + ".dll"))
    {
        try {
            string strCmdLine = " /nologo /l:CS /out:\"" + Environment.CurrentDirectory + "\\" + encode_url + ".cs\" " + url;
        myProcess.StartInfo.CreateNoWindow = true;
        myProcess.StartInfo.Arguments = strCmdLine; myProcess.StartInfo.FileName = "wsdl.exe";
        myProcess.StartInfo.RedirectStandardOutput = true;    
        myProcess.StartInfo.UseShellExecute = false;    
        myProcess.StartInfo.RedirectStandardError = true;
        myProcess.Start();
        while (myProcess.StandardError.EndOfStream != true)
        {
            MessageBox.Show(myProcess.StandardError.ReadLine());
        }
    }
    catch (Win32Exception error)
    {
        if (error.NativeErrorCode == ERROR_FILE_NOT_FOUND)
        {
            MessageBox.Show(error.Message + ". Check the path.");
        }
        else if (error.NativeErrorCode == ERROR_ACCESS_DENIED)
        {
            MessageBox.Show(error.Message + ". You do not have permission to print this file.");     }
        else
        {
            MessageBox.Show(error.NativeErrorCode + ": " + error.Message);
        }
    }
    }
    CompilerResults proxyResult; proxyResult = CompileCode(encode_url + ".cs", "compiled_" + encode_url + ".dll");
    if (proxyResult.Errors.Count > 0)
    {
        for (int i = 0; i < proxyResult.Output.Count; i++)        
            MessageBox.Show(proxyResult.Output//emoticons/emotion-55.gif" alt="Idea" />);
        for (int i = 0; i < proxyResult.Errors.Count; i++)
            MessageBox.Show(i.ToString() + ": " + proxyResult.ErrorsIdea.ToString());
    }
    else {
        if (proxyResult.PathToAssembly == null)
            MessageBox.Show("The assembly has been generated in memory.");
        else MessageBox.Show("Path to assembly: " + proxyResult.PathToAssembly);
    }
    Assembly a = Assembly.LoadFrom("compiled_"+encode_url+".dll");

     


    From there I can call methods from the assembly. I was wondering if you could outline a better way to use the assembly.

    Would it be faster to compile the proxy class into memory and call the methods from there

    How do I keep the proxy class in the namespace.

  • Whololo

    I recomend you to cache all these compiled assemblies on the disk - it should improve performance of your application.

    Why do you need to keep namespace's name of the proxy class

  • Ivan Ivanyuk

    I am trying to reinstatiate an object of a type defined in the webservice. How do I serialize the data back into a struct. How do I define and include data types defined in the webservice

    Thanks for all your help, Igor.

  • G. Andrew Duthie

    1)Use WSDL comand line tool (wsdl.exe) to generate source code of proxy for a webservice.
    1.1) WSDL.exe is the managed app - thus could just decompile it and put all code into your app. (I'm not aware about licensing issues though - please be careful).
    2) Compile that source code on the fly using particular instance of your favorite ICodeCompiler into standalone assembly.
    3) Create instance of proxy class using Reflection, e.g. Activator.CreateInstance()
    4) Tell me if there's more simple way to do this.





  • Dynamically create a proxy or call a web service from a WSDL.