Support for web references has now been added to SharpDevelop2 in revision 955.
FeaturesAdd web reference
- Browse/view web services
- Automatic web proxy class generation
Refresh web reference
- Updates generated web proxy class
Adding a Web Reference- With a project open in SharpDevelop, view the project browser by selecting View | Projects.
- In the project browser, right click either the project node or the References node and select "Add Web Reference" to open a dialog.

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

- Click the "Add" button to add the web reference to the project.
Refreshing the Web ReferenceIf the web service changes, the generated proxy class will need to be refreshed or updated.
- Open the project browser.
- 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 ServicesNow 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 ProxyOnce 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", services, schemas);
}
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 proxyNamespace, string fileName, ServiceDescriptionCollection serviceDescriptions, XmlSchemas schemas)
{
ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
foreach (ServiceDescription description in serviceDescriptions) {
importer.AddServiceDescription(description, null, null);
}
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(codeNamespace, codeUnit);
CodeDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
using (StreamWriter sw = new StreamWriter(fileName)) {
CodeGeneratorOptions options = new CodeGeneratorOptions();
options.BracingStyle = "C";
provider.GenerateCodeFromCompileUnit(codeUnit, sw, options);
}
}
}
}
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 VideoThere is also a
video, created by
Chris, showing the web reference functionality in SharpDevelop 1.1.