SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

Matt Ward

December 2005 - Posts

  • Web References

    Support for web references has now been added to SharpDevelop2 in revision 955.  

    Features

    Add web reference
    • Browse/view web services
    • Automatic web proxy class generation
    Refresh web reference
    • Updates generated web proxy class
    Adding a Web Reference
    1. With a project open in SharpDevelop, view the project browser by selecting View | Projects.
    2. In the project browser, right click either the project node or the References node and select "Add Web Reference" to open a dialog.



    3. Enter a url and browse for a web service.
    4. Once the web service has been located the reference name and namespace will be auto-generated.
    5. Change the reference name and the web proxy namespace if required.



    6. Click the "Add" button to add the web reference to the project.


    Refreshing the Web Reference

    If the web service changes, the generated proxy class will need to be refreshed or updated. 
    1. Open the project browser.
    2. Select the web reference node, right click and select "Refresh Web Reference".


    The proxy class will then be regenerated.  Note that when the web service is refreshed the namespace of the proxy class will be set to the namespace of the project.  This may not correspond to what was entered into the "Add Web Reference" dialog.

    Discovering Web Services

    Now let us take a look at the code.  The web service discovery is handled completely by the DiscoveryClientProtocol class which is part of the .NET Framework.  The following simple example code discovers a web service and saves the discovered documents to disk.

        DiscoveryClientProtocol protocol new DiscoveryClientProtocol();
        protocol.DiscoverAny("http://localhost/Maths.asmx");
        protocol.ResolveOneLevel();
        protocol.WriteAll(@"c:\temp\Web References""Reference.map");

    Generating a Web Proxy

    Once the DiscoveryClientProtocol class has discovered the web service the web proxy can be generated.  To generate a proxy the CSharpCodeProvider and the ServiceDescriptionImporter; class can be used.  Here is a simple example that discovers a web service, saves the discovered documents, and then generates a web proxy.

    using System;
    using System.CodeDom;
    using System.CodeDom.Compiler;
    using System.Collections;
    using System.IO;
    using System.Web.Services.Description;
    using System.Web.Services.Discovery;
    using System.Xml.Schema;
    using System.Xml.Serialization;

    namespace WebServiceTest
    {
        class MainClass
        {
            public static void Main(string[] args)
            {
                // Discover and save web service information.
                DiscoveryClientProtocol protocol new DiscoveryClientProtocol();
                protocol.DiscoverAny("http://localhost/Maths.asmx");
                protocol.ResolveOneLevel();
                protocol.WriteAll(@"c:\temp\Web References""Reference.map");
                
                // Generate the web proxy.
                ServiceDescriptionCollection services GetServiceDescriptionCollection(protocol);
                XmlSchemas schemas GetXmlSchemas(protocol);
                GenerateWebProxy("WebServiceTest"@"c:\temp\Web References\Reference.cs"servicesschemas);
            }
                    
            static ServiceDescriptionCollection GetServiceDescriptionCollection(DiscoveryClientProtocol protocol)
            {
                ServiceDescriptionCollection services new ServiceDescriptionCollection();
                foreach (DictionaryEntry entry in protocol.References) {
                    ContractReference contractRef entry.Value as ContractReference;
                    DiscoveryDocumentReference discoveryRef entry.Value as DiscoveryDocumentReference;
                    if (contractRef != null) {
                        services.Add(contractRef.Contract);
                    }
                }
                return services;
            }
            
            static XmlSchemas GetXmlSchemas(DiscoveryClientProtocol protocol)
            {
                XmlSchemas schemas new XmlSchemas();
                foreach (DictionaryEntry entry in protocol.References) {
                    SchemaReference schemaRef entry.Value as SchemaReference;
                    if (schemaRef != null) {
                        schemas.Add(schemaRef.Schema);
                    }
                }
                return schemas;
            }
            
            static void GenerateWebProxy(string proxyNamespacestring fileNameServiceDescriptionCollection serviceDescriptionsXmlSchemas schemas)
            {
                ServiceDescriptionImporter importer new ServiceDescriptionImporter();
                
                foreach (ServiceDescription description in serviceDescriptions) {
                    importer.AddServiceDescription(descriptionnullnull);
                }
                
                foreach (XmlSchema schema in schemas) {
                    importer.Schemas.Add(schema);
                }
                
                CodeNamespace codeNamespace new CodeNamespace(proxyNamespace);
                CodeCompileUnit codeUnit new CodeCompileUnit();
                codeUnit.Namespaces.Add(codeNamespace);
                ServiceDescriptionImportWarnings warnings importer.Import(codeNamespacecodeUnit);
                
                CodeDomProvider provider new Microsoft.CSharp.CSharpCodeProvider();                        
                using (StreamWriter sw new StreamWriter(fileName)) {
                    CodeGeneratorOptions options new CodeGeneratorOptions();
                    options.BracingStyle "C";
                    provider.GenerateCodeFromCompileUnit(codeUnitswoptions);
                }
            }
        }
    }

    Compatiblity with Visual Studio
    • Web references generated by SharpDevelop are "Static".  Visual Studio generates "Dynamic" web references.
    • Creating and refreshing "Dynamic" web references are not currently supported in SharpDevelop.

    Visual Studio generates dynamic web references, where the web service URL is stored in an app.config file rather than hard coded in the web proxy itself.  The proxy generated by Visual Studio accesses this URL through a Settings class, which is used to access all properties for an application.

     this.Url global::BaseFormTest.Properties.Settings.Default.WebServiceTest_localhost_Maths;

    SharpDevelop is currently not able to generate corresponding code when refreshing the web service.

    Web References Video

    There is also a video, created by Chris, showing the web reference functionality in SharpDevelop 1.1.

  • Insert PInvoke Signatures

    Support for inserting PInvoke signatures has been added to SharpDevelop2 in revision 925.

    Features
    1. Search PInvoke signatures held at pinvoke.net
    2. Insert C# or VB.Net signatures into source code.
    How to use
    1. Position the cursor where you want the pinvoke method signature to be inserted.
    2. Right click, or open the Tools menu option, and select "Insert PInvoke Signatures..." to open a dialog.
    3. Type in the name of the function you want and click the Find button.
    4. The pinvoke signature is retrieved and displayed in the dialog.  Further information can be viewed by clicking the "More Info" hyperlink which will send your browser to the appropriate pinvoke.net web page.
    5. Click the insert button to insert the signature into your code.


    pinvoke.net

    This SharpDevelop2 feature uses a web service from the excellent pinvoke.net web site created by Adam Nathan.  If you want more information than the insert pinvoke signatures dialog provides and examples then explore the pinvoke.net wiki.

    Posted Dec 21 2005, 01:49 PM by MattWard with no comments
    Filed under:
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.