Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Web Reference and compile on-the-fly

Posted on 2008-10-09
4
Medium Priority
?
783 Views
Last Modified: 2013-12-17
I have a wsdl file.  I can create a new Class Library project in Visual Studio, add the wsdl file as a Web Reference, and then compile the project into a DLL.

What I would like to do is write a program that will automate this.  The user will specify the path to the wsdl file and click a button.  The program will then automatically do the steps up above.

I am familiar with compiling on-the-fly like this, but am not sure how to incorporate adding the Web Reference.

How can I do this?

Thanks,
Chris
0
Comment
Question by:chlade
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 22680749
command line .. http://msdn.microsoft.com/en-us/library/7h3ystb6(VS.71).aspx

wsdl http://your.com/url

you can also specify the name of the .cs file to generate ... building the .cs from the command line is also extremely easy.

csc /R:dllname.dll file.cs

Cheers,

Greg
0
 

Author Comment

by:chlade
ID: 22683003
Is there a way to do this within code?  Otherwise, I need to shell out, run wsdl.exe to create a file, read the file in, compile, etc.  If I could do this all in memory, that would be ideal.  But perhaps that's not possible?
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 2000 total points
ID: 22683083
I don't know of a call for the wsdl part ... but you can do the rest just using say the CSharpCodeProvider to do the compilation etc (and generate an in memory assembly to load)

Cheers,

Greg
0
 

Author Comment

by:chlade
ID: 22687245
Thanks.  That got me going.  I ended up finding more information and created a small test app that will do it for me.  Here is the code that I ended up with:

Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.Security.Permissions
Imports System.Web.Services.Description
 
Public Class Module1
 
    Public Shared Sub Main()
 
        Dim WsdlFileName As String = "test.wsdl"
        Dim DllFileName As String = "test.dll"
 
        Try
            CompileWsdlIntoDll(WsdlFileName, DllFileName)
            MsgBox("Successfully compiled " & WsdlFileName & " into " & DllFileName & ".")
        Catch ex As Exception
            MsgBox("Error compiling WSDL into a DLL." & ControlChars.CrLf & ControlChars.CrLf & _
                ex.Message)
        End Try
 
    End Sub
 
    <SecurityPermissionAttribute(SecurityAction.Demand, Unrestricted:=True)> _
    Public Shared Sub CompileWsdlIntoDll(ByVal WsdlFileName As String, ByVal DllFileName As String)
 
        Dim ResultMsg As String = ""
 
        Dim description As ServiceDescription = ServiceDescription.Read(WsdlFileName, True)
 
        ' Initialize a service description importer. 
        Dim sdi As New ServiceDescriptionImporter()
        sdi.ProtocolName = "Soap"
        sdi.AddServiceDescription(description, Nothing, Nothing)
 
        ' Generate a proxy client. 
        sdi.Style = ServiceDescriptionImportStyle.Client
 
        ' Generate properties to represent primitive values. 
        sdi.CodeGenerationOptions = System.Xml.Serialization.CodeGenerationOptions.GenerateProperties
 
        ' Initialize a Code-DOM tree into which we will import the service. 
        Dim nmspace As New CodeNamespace()
        Dim unit1 As New CodeCompileUnit()
        unit1.Namespaces.Add(nmspace)
 
        ' Import the service into the Code-DOM tree. This creates proxy code that uses the service. 
        Dim warning As ServiceDescriptionImportWarnings = sdi.Import(nmspace, unit1)
 
        If warning = 0 Then
            ' Generate and print the proxy code in C#. 
            Dim provider As CodeDomProvider = CodeDomProvider.CreateProvider("VisualBasic")
 
            ' Compile the assembly with the appropriate references 
            Dim refs As String() = New String(1) {"System.Web.Services.dll", "System.Xml.dll"}
            Dim cp As New CompilerParameters(refs)
            cp.OutputAssembly = "test.dll"
            Dim cr As CompilerResults = provider.CompileAssemblyFromDom(cp, unit1)
 
            For Each ce As CompilerError In cr.Errors
                ResultMsg &= String.Format("  {0}", ce.ToString()) & ControlChars.CrLf
            Next
 
        Else
            ' Print an error message. 
            ResultMsg &= "Warning: " & warning.ToString
        End If
 
        If ResultMsg <> "" Then
            Throw New Exception("Error compiling WSDL (" & _
                "WSDL FileName: " & WsdlFileName & _
                ", DLL FileName: " & DllFileName & _
                ", Compile Messages: " & ResultMsg & _
                ")")
        End If
 
    End Sub
 
End Class

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question