SharpDevelop Community

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

Matt Ward

Web References

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


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.WriteAll(@"c:\temp\Web References""");

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.WriteAll(@"c:\temp\Web References""");
            // 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) {
            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) {
            return schemas;
        static void GenerateWebProxy(string proxyNamespacestring fileNameServiceDescriptionCollection serviceDescriptionsXmlSchemas schemas)
            ServiceDescriptionImporter importer new ServiceDescriptionImporter();
            foreach (ServiceDescription description in serviceDescriptions) {
            foreach (XmlSchema schema in schemas) {
            CodeNamespace codeNamespace new CodeNamespace(proxyNamespace);
            CodeCompileUnit codeUnit new CodeCompileUnit();
            ServiceDescriptionImportWarnings warnings importer.Import(codeNamespacecodeUnit);
            CodeDomProvider provider new Microsoft.CSharp.CSharpCodeProvider();                        
            using (StreamWriter sw new StreamWriter(fileName)) {
                CodeGeneratorOptions options new CodeGeneratorOptions();
                options.BracingStyle "C";

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.

Published Dec 31 2005, 02:41 PM by MattWard
Filed under:


No Comments
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this ( email address.