Below is my little learning summary about the web service, of course, is still very simple. I hope that everyone can help. Read more about the criticism.
How to create and use Web services
Create a web service
Generally speaking, there are two ways to create web services
l Write the Web service file directly (* .asmx);
l First implement the business object, then use only the ASMX file exposure;
Note: * .asmx is an extension of the ASP.NET Web service, not all Web services are in this way for users to test, and other manufacturers provide web services are only a port.
1. Create a simple web service file
<% @ Webservice Language = C # Class = HellOMessage%>
Using system;
Using system.Web.services;
Public Class Hellometake: Webserve
{
[WebMethod]
Public String Sayhello (String Strname)
{
Return "Hello," STRNAME;
}
}
First, you need to add WebService's compilation instruction to specify it a web service, followed by language properties, and a Web service class attribute class.
Note: There can be many classes in an ASMX file, which can only have a web service.
HellOMessage inherits this class of System.Web.Services.WebService.
[WebMethod] This attribute must be added to the web method you want to use. Note that the Modifier of this method must be public.
I hope that the Web method accessible to the user must be public, and must contain attributes [WebMethod]. The method containing this property will regard all calorificers as local.
Further instructions for [WebMethod] attributes can see ASP.NET21.
2. Business Law
You can use existing business objects to create a web service, which can greatly reduce workload. Of course, in order to start service support, you must do some corresponding modifications. Simply, you need to do three things:
l object must be inherited from the WebService class;
l Plus [WebMethod] attribute to the method to be exposed, of course, Modifier must also be public;
l Turn the return value of XXDataReader to DataSet because the former cannot transmit in XML;
Note: Because the classes that are inherited from WebService will be exposed to the customer as a service, you can create multiple versions of the object in the same file.
Then, compile the business object into a program set in the corresponding directory.
Then build the .asmx file that exposes business object services, very simple, only one sentence.
<% @ Webserive Class = YourNamespace.YourServiceClass%>
Test web service
For Web services established by the .NET framework, you can see the test interface directly in the IE browser. For example:
http://www.hillfree.com/myservice.asmx
Generally speaking, you can see the web service class and web method. There are three ways to call the Web service. (Of course, not all Web methods can be called with three ways.):
SOAP
2. http-get
3. HTTP-POST
Use the SOAP call service to transfer commands and parameters, you can use many XML-based data types. (I.e. Array, Class, Dataset, Primitive, XMLNode ETC.)
However, if you use HTTP GET or HTTP POST, you can only use the types that can be processed by these protocols. Generally, the primitive basic type and array. HTTP GET / POST can only send name values, and complex data types cannot be transmitted.
You need to view the relevant instruction file, you can enter:
http://www.hillfree.com/myservice.asmx?wsdl
This is an XML file.
Use web services
Using web services, generally include three steps
1. Discover service 2. Agent generation service
3. Write a client program to use the agent to call the required service.
Discover service:
Use Disco.exe tools to discover Web services, such as:
Disco http://localhost/service/day/fain.disco
Generate a service agent class
Use the WSDL.exe tool to generate a web service proxy class. As follows:
WSDL http://www.hillfree.com/service.asmx?wsdl
// If you know the URL of WSDL directly, don't find that step.
This default is generated by a C # class. Of course, if you already have a WSDL file, you can go directly:
WSDL Service.wsdl
Next, you can use this class directly, or compile it as an assembly.
Briefly introduce the agent class:
The generated proxy class contains an attribute from System.Web.Services [WebServiceBindingAttribute].
This attribute defines the interface that must be used in this class.
Alternatively, this type is inherited from SOAPHTTPCLIENTPROTOCOL, which remotely communicates through SOAP and Web services. The proxy class contains a constructor that sets the URL of the web service.