Web Services

A web service is an application that exposes a Web accesible API. That means you can invoke the application programmatically over the web. Applications invoking this Web Service are referred to as clients.
An XML web service is a .NET component that replies to HTTP requests that are formatted using the SOAP syntax.
Web services allow a degree of interoperability among applications over the Internet.
XML web services are just ASP.NET applications that intercept requests for .asmx files.


These are application components
They communicate using open protocols
They are self contained and self describing
They can be discovered using UDDI
A basic web service is XML plus HTTP



You construct a web page that takes parameters in the query string and returns the relevant data. The client then sends an HTTP Get request with the appropriate URL.


These allow access to software components through standard web protocols such as HTTP and SMTP.
Web services allow us to use the Internet and XML to create software components that communicate with each other regardless of language or platform.


XML - This is used to structure the data
SOAP - This is used to transfer the data
This stands for Simple Object Access Protocol.
SOAP is an XML based protocol that allows applications to exchange information over http.
It is a communication protocol
It is platform independent
It is based on XML, HTTP and SMTP
It is language independent


WSDL - This is used to describe the web service, the description of the methods provided (Web Service Description Language)
UDDI - This is used to get a list of services available. This is a directory service where companies can register and search for web services. This stands for Universal Description, Discovery and Integration.
SMTP -


Relevant Namespaces

System.Web.Services 
System.Web.Services.Protocols
System.Net
System.Threading
System.Web.Security
System.IO


Accessing

Any remote client that knows where to look for this service and what arguments to pass can call this method remotely by posting an HTTP Get request.
This is just one of three protocols that you can use to access this web service through the Internet.
The other two being HTTP Post and SOAP.


Any client that can post an HTTP request can be a client of the XML web service.


Windows Application

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".


3 Web Service Protocols

You should use SOAP in all your applications and only use the other two for testing purposes.
HTTP GET
This method lets you type in the whole query string directly into a browser.
When you invoke this method the text will be automatically sent to the .asmx page.


HTTP POST
Similar to the HTTP GET except that the arguments are passed in the HTML body rather than the query string.


HTTP SOAP
This uses SOAP messages for both the input arguments and for the return value.
The root node of any SOAP message is the message envelope which contains the message body
In turn the body contains an XML tag named after the target method and all arguments are sent inside this block.



Any soap message can contain an optional header, which might contain additional information not strictly related to the method being called.
For example a client might use the header to send its credentials so that the web service can record who accessed its methods and when.
SOAP is the only protocol that support objects and structures.
The only requirement for an object fed to (or returned) is that it must be serializable.
SOAP is also the only protocol that supports output arguments using ByRef.


web.config Changes

All three protocols are enabled by default although you can disable one or more of them by editing your local web.config file.
In a production web service you might want to drop all the add tags except the SOAP one.

<configuration> 
   <system.web>
      <webServices>
         <protocols>
            <add name="HttpSoap" />
            <add name="HttpPost" />
            <add name="HttpGet" />
            <add name="Documentation" />
         </protocols>
      </webServices>
   </system.web>
</configuration>


1) ASMX Web Service

These use the [WebMethod] attribute to assign to methods we want to expose
These are much simpler than WCF web references.
These must be added using the Web Reference
This is a wrapper over the wsdl.exe and can be used to create proxies for .NET 1.1 and .NET 2.0




2) WCF Services

Windows Communication Foundation (WCF)
This is a wrapper over svcutil.exe
It creates client proxies and also web.config entries.
All communications with a WCF Service occur through endpoints.
Each endpoint contains the following information:
Address - where to find the service (represented by the EndPointAddress class). Every endpoint must have a unique address.
Binding - how the communication is performed
Contract - specifies which operations are available
These must be added using the Service Reference



Service References

Service Reference is the name given to a whole bunch of different types of reference services of which a web reference (or web service) is one type
Visual Studio 2008 introduced WCF services. We program to a specific contract , we attribute an interface, these are more complicated and more powerful.




EndPointClass

URI - (Uniform Resource Identifier) represents the address
Identity - represents the security identitiy
Headers - (optional) - providing more detailed addressing information.


EndPoints provide clients with access to the service
If any of the following Address, Binding or Contrat are changed then a new proxy must be created.



EndPoint Specification in a Config File



EndPoint Specification in Code

This can be done using the EndPointAddress class
The URI can be a full path or a path relative to the service base address.
In order to use a WCF Service you need to create a WCF client proxy.
In Visual Studio this client proxy is created for you when you add the Service Reference.
If you don't want to add a service reference you can achieve the same result using svcutil.exe


2 Types

Synchronous - the caller waits for the web service to respond before continuing
Asynchronous - the caller can continue to use the thread while waiting for the web service to respond.





Web Queries



Discovery File (.disco)

This provides information to help browsers determine the URLs at any websites which web services are available.



Launching a Web Page

System.Diagnostics.Process.Start("https://bettersolutions.com")




Async Calls

When you fire off multiple async web calls maintain an arraylist of all the calls we are waiting for - need a consistent naming convention
May be nicer to have an event that fires when they have all been returned.



Proxy Class



Fiddler

This is free Microsoft Web Services debugging tool


How to test a Web Service

The easiest way to test a web service is by running the project.
Make sure that your ProjectName.asmx file is defined as the start page.


The title of the page is the name of the class
The description property of the WebService attribute appears immediately below the title.
You then get a list of available methods with their descriptions below each one.
Not all methods can be tested in this way though.
For example you cannot test methods that take an object or ByRef arguments.


The Service Description link displays the WSDL contract for this web service.
This is an XML file that describes the web service with information about each method and its arguments.
You can browse the WSDL contract for an .asmx page from inside a browser by appending "?WSDL" to the page's URL.



Clicking an any method will allow you to enter the necessary arguments and the result will be returned in XML.



Advanced

Where does this page come from ?
When asp.net intercepts a request for an .asmx page without anything on the query string, it used reflection to extract the attributes and the method names of the first class in the .asmx file.
It then synthesizes the HTML page for you automatically.
Only the first class in this file is visible in a browser.
The page that produces the output is just an .aspx page.
This file is the DefaultWsdlHelpGenerator.aspx file which can be customised if you want to.
Changing this file will affect all the XML web services running on this machine.




© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopNext