SOAP Service with Web Reference

Added in .NET 1.1
This supports any service that implements WS-I Basic Profile 1.1
This uses the ASMX communication stack on the clients side
This was for the original ASP.NET web services which used System.Xml.Serialization.XmlSerializer
This only supports BasicHttpBinding


This is a wrapper over wsdl.exe and creates a client proxy



Add a Web Reference
This dialog box lets you select a web service from those registered in a Universal Description, Discovery and Integration directory.
UDDI directories are a bit like the web services yellow pages in that they list XML web services distributed across the Internet.
Visit www.uddi.org for more details.
Type the path to your .asmx file on your local machine amd press Enter.
The left pane will display a description page associated with the web service.
The right pane contains additional links to view the WSDL contract.
Go ahead and add the reference.


After any changes to the web service you should rebuild the proxy class.
The easiest way is to select the service and select "Update Reference".


Adding a Web reference to your project for the Web service does this by generating a proxy class that interfaces with the Web service and provides a local representation of the Web service.


Example

Open Visual Studio 2022 as Administrator.
New Project, Windows Forms App (.NET Framework) C#
Create folder C:\temp\winforms\


Create new project,
Press Next


Project name - WebServicesProject
Location - C:\temp\winforms
Framework - .NET Framework 4.8


Add a TextBox to the windows form - name - myTextBox
Add a Button to the windows form - name - myButton
Double click the button to add the event handler


Right click on the project and select
Add > Service Reference
press Advanced
press Add Web Reference


Add the following URL and press GO

dneonline.com/calculator.asmx?wsdl 

change the web reference name to MyService
press Add Reference
All the necessary proxy classes will be created.


A Web References folder will be created
SS



View > Other Windows > Object Browser
Expand - WebServicesProject.MyService
You will see four operations and each operation will have an EventArgs and an EventHandler


Synchronous

Add the following code to the button event handler
This will call the service synchronously and block the main thread.

private void button1_Click(object sender, EventArgs e) 
{
    WebServicesProject.MyService.Calculator service = new WebServicesProject.MyService.Calculator();
    int myresult = service.Add(20, 20);
    this.textBox1.Text = myresult.ToString();
}

Asynchronous

Add the following code to the button event handler
This will run the code asynchronously and not block the main thread.
This creates the event handler at run-time.

private void button1_Click(object sender, EventArgs e) 
{
    WebServicesProject.MyService.Calculator service = new WebServicesProject.MyService.Calculator();
    service.AddCompleted += new MyService.AddCompletedEventHandler(this.Method_Service_AddCompleted);
    service.AddAsync(20, 20);
}

private void Method_Service_AddCompleted(object sender, MyService.AddCompletedEventArgs e)
{
    this.textBox1.Text = e.Result.ToString();
}



© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext