DEV Community

João Osório
João Osório

Posted on • Edited on

Contract First Web Service Development in .NET

Alt Text
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:

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, 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

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

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>


Enter fullscreen mode Exit fullscreen mode

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:



// 
// 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;
            }
        }
    }
}


Enter fullscreen mode Exit fullscreen mode

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

ASP.NET Web Application (.NET Framework)

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

Add existing item ...

Now Add a new ASMX item:

Add new ASMX item ...

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



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;
        }
    }
}


Enter fullscreen mode Exit fullscreen mode

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.

Background photo created by natanaelginting - www.freepik.com

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.