What is a Web Service?
Web service, also known as XML Web service, is a networked, distributed software system that is typically delivered via HTTP, which can be invoked remotely across programming languages and operating systems, and typically, Web services include the service itself and the client. The latter is also known as a consumer or requester, and can be accessed over the network to deploy a service provided by the server (by invoking a Web service that exposes the provider externally). It can be a Web browser, a desktop application, or even an application without a graphical interface.
Figure 1.1 A Web service and one of its clients
By definition, Web services and service clients interact over HTTP, and HTTP messages are infrastructure that are combined into a basic session pattern that describes the characteristics of a Web service. The common patterns are 4, as follows:
- If the session of the Web service starts with the expected HTTP request HTTP response, then this pattern is called the request/process mode, which is currently the dominant pattern in 4 modes
- If the session starts with an HTTP message that the server expects to return a message from the client, the pattern is called a request/response
- Unresponsive messages from the client to the server, called unidirectional mode
- No response message from server to customer order, called notification mode
Some concepts in WEB service
soap: Soap initially represents the Simple Object Access Protocol (easy objects, Access Protocol), as we said before that Web service is delivered over HTTP, Web services and web The information that interacts between service clients is encapsulated XML-formatted data and adds the corresponding HTTP header, which is called the SOAP protocol, which is generally the SOAP protocol = HTTP protocol + XML format data
wsdl: Web Services Description Language (Web service Description Language), a language based on XML that describes the name, description information, parameters, return value, and so on of operations defined within a WEB service. It must itself ensure that both Web service and Web client must be able to understand the language. In general, development tools are generated automatically.
xsd : XML Schema definition, from its full name can be seen as an XML-based language, it defines a set of standard data types, Web service is the use of XSD as its data type system, the Web The data used in the service will eventually be converted to XSD format (functionally similar to the IL language of the Net platform)
UDDI:UDDI is a specification that primarily provides a Web service-based registration and discovery mechanism, a directory service that enterprises can use to register and search for Web services. Before a client invokes a Web service, it must know that the producer of the Wsdl,web service for the Web service can expose the WSDL to its consumers in two ways, one that tells the WSDL directly to its caller, and the second, Register the service counterpart WSDL on the UDDI server.
some features of Web Service
- Language transparency
The services of Web services and their clients do not have to be implemented using the same programming language, which is an important factor in Web services interoperability, where Web services and their consumers can seamlessly converge, despite the use of different programming languages, different operating systems and hardware platforms.
If you write a Web service using C #, if you want the service to be used by a client that is developed using Python or Java language, then there must be an intermediate processing layer to handle the different data types between the service and the client, and XML can be used as a middle tier. Because it supports structured document exchange and processing, the other middle tier is JSON (JavaScript Object Notation)
- Modular design
Web Service designers are modular, which enables new services to be combined with existing services. The guiding principle for Web service design is to start with very simple service operations and inherently uncomplicated functions, which can then be combined into services based on real-world requirements
- Non-state sex
A WEB service consists of a number of operations, each of which is implemented as a call to a stateless function (if the return value of a call depends only on the arguments passed to him, the call is stateless)
- Development infrastructure
Web services deploy with industry-standard, vendor-independent protocols and HTTP, XML, JSON, all of which are ubiquitous, and Web services can take advantage of networks, data formats, security, and other infrastructure already in place to reduce their entry costs
Benefits of using Web service
- Modern software systems are typically written in multiple languages, creating a major challenge in modern software development: system integration, how to interact with software systems written in different languages and running on different hardware platforms? Web services provide a relatively simple answer.
- Because Web service is developed based on the idea of modularization, it can greatly improve the reuse rate of code and the progress of software development.
A simple small instance of WEB service used in net
- Open Visual Studio, new project à Web à ASP. NET Web application
- Right-click on the Created Project à Add à Web Services (ASMX)
- The files created by Firstwebservice.asmx are as follows
Public classFirstwebservice:system.web.services.webservice{[webmethod] Public stringHelloWorld () {return "Hello World";} [WebMethod (Description="method of summation")] Public intGetsum (params int[] arr) {intsum =0;foreach(intIincharr) {Sum+=i;}returnsum;}}
View Code
At this point, a simple web Service, even if the deployment is complete, click on the Launcher, in the browser can see
Next, create a Web client call to create a Web service that is just a demonstration, creating the simplest console application client
- Right-click on the created console program to add à Service Reference
In the pop-up window, paste the URL of the Web service you created into the Address column, then click OK
once created, our client's directory structure is as follows:
5. Write Code call Web service
//you need to import the namespace using the service Client.servicereference1;Firstwebservicesoapclient C =Newfirstwebservicesoapclient (); strings =C.helloworld (); Console.WriteLine (s); //Call the Sum function getsum (params int[] arr), but you will find here its parameter type becomes (Arrayofint), go to definition can see that class inherits from List<int>//so the arguments here are either added in the following way, or directly add a collection of list<int> types int[] arr =New int[] {1,2,3 }; Arrayofint ar=NewArrayofint (); intsum =c.getsum (AR); Console.WriteLine ("The SUM is: {0}", sum); Console.readkey ();
View Code
6. At this point, the build and invocation of a simple Web service is all done.
Introduction to Web Service of Web API 2 (i)