Contract First Web Service Development in .NET
Tags: xml, wsdl, soap, visualstudio
Published: 2021-05-07 11:01:07

---
title: Contract First Web Service Development in .NET
published: true
description: Working on information systems integration, often comes the task of implementing a Web Service following a previously defined contract.
tags: 
- XML
- WSDL
- SOAP
- VisualStudio
---
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e5x1ssnp750246bymh94.jpg)
Working on information systems integration often comes with the task of implementing a Web Service following a previously defined contract. This can happen with a design-first approach or a partner interoperability project where you must implement the expected receiver for the incoming data.

To develop an XML web service, having an existing WSDL, we can use the tooling provided with Microsoft Visual Studio. 

We will use an ASMX project. If you're worried about support because this is no longer a part of .NET from version 5 upward, I'd advise you to read [this guy's opinion](https://forums.asp.net/post/6306984.aspx):
> To be accurate, both WCF and ASMX will be supported after .NET 5 is released. They will be included in .NET 4.8 and that will be supported as a component of the OS that they are installed on.  

Sure, we could use [Soap Core](https://github.com/DigDes/SoapCore), but to work with existing contracts I find it to be a very manual solution and with some risk of inadvertently altering the schema.

Getting down to business, let's go through the steps of a quick and easy proof of concept for an existing, simple, and free of charge public web service: 

[www.learnwebservices.com/services/hello?WSDL](http://www.learnwebservices.com/services/hello?WSDL)

Download the WSDL file onto a folder on your local computer.
You'll need Visual Studio installed. Open "Developer Command Prompt for VS 2019":
![Developer Command Prompt for VS 2019](https://www.ozzie.eu/post/contract-first-ws-development/VSTools2019.webp)

On the command prompt, go to the folder where you saved the WSDL file.
Run the WSDL command:
```
wsdl /namespace:My.Contract.First /serverInterface <file.wsdl>
```

Where \<file.wsdl\> is the name you gave when saving the contract. Choose an adequate namespace also. Upon successful completion of this command you should have an interface definition corresponding to the methods available on the given Web service:

```csharp
// 
// This source code was auto-generated by wsdl, Version=4.8.3928.0.
// 
namespace My.Contract.First {
    using System;
    using System.Web.Services;
    using System.Diagnostics;
    using System.Web.Services.Protocols;
    using System.Xml.Serialization;
    using System.ComponentModel;
    
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.8.3928.0")]
    [System.Web.Services.WebServiceBindingAttribute(Name="HelloEndpointServiceSoapBinding", Namespace="http://learnwebservices.com/services/hello")]
    public interface IHelloEndpointServiceSoapBinding {
        
        /// <remarks/>
        [System.Web.Services.WebMethodAttribute()]
        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestNamespace="http://learnwebservices.com/services/hello", ResponseNamespace="http://learnwebservices.com/services/hello", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
        [return: System.Xml.Serialization.XmlElementAttribute("HelloResponse")]
        helloResponse SayHello(helloRequest HelloRequest);
    }
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.8.3928.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://learnwebservices.com/services/hello")]
    public partial class helloRequest {
        
        private string nameField;
        
        /// <remarks/>
        public string Name {
            get {
                return this.nameField;
            }
            set {
                this.nameField = value;
            }
        }
    }
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.8.3928.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://learnwebservices.com/services/hello")]
    public partial class helloResponse {
        
        private string messageField;
        
        /// <remarks/>
        public string Message {
            get {
                return this.messageField;
            }
            set {
                this.messageField = value;
            }
        }
    }
}
```

Open Visual Studio and create a new empty ASP.NET Web Application (.NET Framework):

![ASP.NET Web Application (.NET Framework)](https://www.ozzie.eu/post/contract-first-ws-development/aspnet-web-app.webp)

Right click on your project and "Add > Existing Item". Browse to your .CS interface file, click "Add":

![Add existing item ...](https://www.ozzie.eu/post/contract-first-ws-development/add-existing-item.webp)

Now Add a new ASMX item:

![Add new ASMX item ...](https://www.ozzie.eu/post/contract-first-ws-development/new-item-asmx.webp)

Implement the Interface generated by the WSDL tool on your ASMX:

```csharp
using System.Web.Services;

namespace My.Contract.First
{
    /// <summary>
    /// Summary description for HelloService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class HelloService : System.Web.Services.WebService, IHelloEndpointServiceSoapBinding
    {

        [WebMethod]
        public helloResponse SayHello(helloRequest HelloRequest)
        {
            helloResponse resp = new helloResponse();
            resp.Message = "Hi there " + HelloRequest.Name;
            return resp;
        }
    }
}
```

Build and Run. That's all for developing the stub web service using an existing WSDL contract.

The SOAP/XML Web services are gradually losing ground to REST APIs for their lack of simplicity and for failing to deliver on the promise for guaranteed cross-platform integration. But most enterprise applications still use it for the enhanced protocol features like formal XML schema definition, transactionality, and security.

So, if you're building something new, give REST a try. If you're maintaining previously developed platforms, better off using the existing tooling. 

Original post [here](https://www.ozzie.eu/post/contract-first-ws-development/).

[Background photo created by natanaelginting - www.freepik.com](https://www.freepik.com/photos/background")