WebService project practices

Source: Internet
Author: User
1. WebService class, such as Syc. asmx. cs in class Syc, that is, class with WebMethod.
2. the WebService class must inherit from System. Web. Services. WebService.
3. WebService class, which references the System. Web. Services namespace
4. the WebService class does not need to be labeled as [WebMethod] for each method, but is required for public interface methods.
5. serializable object, that is, the object entity class returned by WebMethod
6. Serializable object, which must be marked with [Serializable]
7. For serializable object, the System. Xml. Serialization namespace must be referenced.
8. The serializable object can still use its attributes and methods by class instance after being passed through WebService.
9. serializable object, which can have a pair of getter/setter methods for valuable content
10. Compression not only saves the amount of data transmitted, but also keeps data confidential to a certain extent.
11. The KEY and Auth verification code are generated for security verification before communication.
12. The ID attribute of the object is usually a 32-bit GUID. It not only guarantees uniqueness, but also supports distributed Insert of data.
13. The Web service provider (response) only needs to write *. asmx (. cs) and serialized object. The wsdl document and SOAP agent are automatically generated using Microsoft ASP. NET.
14. After obtaining a SOAP proxy file that includes the WebService proxy class and serializable object entity class, the requester can use the code like calling a local class library.
15. For different versions of ASP. NET or completely different runtime environments, such as Mono, the interface file to be exchanged should be a wsdl document.

Read more

Microsoft ASP. NET QuickStart quick start tutorial Web Service Section (xxxClient is the service request end)
Http://chs.gotdotnet.com/quickstart/aspplus/doc/writingservices.aspx

Web Service class declaration

[WebService (Namespace = "http://www.abc123.com/WebService/")]
Public class Syc
{
......
}

Note: it is also possible to add a label without adding a label, just to declare the namespace.

Web Service method declaration

[WebMethod (Description = "...")]
Public Return Value Method Name (parameter table)
{

The returned object must be a serialized object.
Valid object data can be DataSet,
If compression is required, the data should be XML or text content (byte array)

}

[Serializable]
Object entity class

(Same as UI)
WebService \ UsersEntity. cs

Main attributes:
Attribute Users-byte array
Int result
String ErrorString
String CRC
Public void SetUserData (ds) -- (ds --> xml --> ZIP compression)
Public void SetResult (resultCode)

Synchronous Interface
WebService \ Syc. asmx. cs-Public WebService method [WebMethod]

Main Methods:
Public string CreateAuth (string entityID, string entityName, string key)-generate Verification Code

Public bool CheckAuth (string auth)-check the verification code

[WebMethod]
Public UsersEntity GetOrgUsers (string orgID, string auth)
Main process:
CheckAuth (orgID, auth)
SetResult (resultCode)
Call DataProvider for select to return ds
Call UsersEntity to set data (ds)
Returns UsersEntity.

Data Interface (ds for select)
Class Library \ Data \ DataProvider. cs

Main Methods:
Public DataSet GetOrgUsersRequest (string orgID)
{
DataSet ds = new DataSet ();
DataHelper. FillDataSet (..., SQL, ds, dtName );
Return ds;
}

Test Cases

1.
String key = "n-bit key"; // Public key
Syc syc = new Syc (); // load synchronization Interface
String auth = syc. CreateAuth (entityID, entityName, key); // generate a verification code

2. Console. WriteLine (auth); // obtain the verification code (Public Key)

3.
WebService/UsersEntity res = syc. GetOrgUsers ("1", auth); // call the WebMethod of the synchronous Interface
Byte [] r = Zip. DecompressData (res. Users); // extract XML (DS) data

4. Console. WriteLine (System. Text. Encoding. UTF8.GetString (r); // use UTF8 to output user result data (XML Text)

Test Case Process

1. Verification Code: encrypt the public KEY with the current SHA1 time
(The requester provides the plaintext elements and encryption results to the responder to see if the plaintext elements can generate the encrypted results)

2. Use the synchronous interface [WebMethod] to retrieve data (call the data interface)

(Return) Get a serialized object (. NET object)

3. Get the byte array from the serialization object Method

4. Decompress (ZIP)

5. Get the (XML) Text

Prepare for the service requester-1. Generate the WSDL interface document (XML format interface API)

1. In the browser address bar, enter:

Http: // localhost/.../Syc. asmx? WSDL

2. Save the webpage as a Syc. wsdl file in XML format, which will be used in the next section.

Prepare for the service requestor-2. Generate a SOAP proxy (provided to the C # program of the Service Requestor)

1. Enable command line

2. How to Use the Microsoft WSDL Conversion Tool:

Microsoft (R) Web Service Description Language Utility
[Microsoft (R). NET Framework, version 1.1.4322.573]
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.

Wsdl.exe-
Used to discover documents from the WSDL protocol file, XSD architecture, and. discomap,
A utility used to generate code for the Xml Web service client and Xml Web services of ASP. NET
.
This tool can be used with disco.exe.

Wsdl.exe <option> <URL or path>

-Option-

<Url or path>-
The URL or path of the WSDL protocol, XSD architecture, or. discomap document.

/Nologo
Undisplay the copyright logo.

/Language: <language>
The language used to generate the proxy class. From "CS ",
Select from "VB", "JS", and "VJS", or
The System. CodeDom. Compiler. CodeDomProvider class provides a fully qualified name. The default value is CS"

(CSharp ). It is abbreviated as "/l :".

/Server
Generate abstract classes for Xml Web service using ASP. NET Based on the Protocol.
The client proxy class is generated by default.

/Namespace: <namespace>
The generated proxy or template namespace. Default namespace
Is a global namespace. The abbreviation is "/n :".

/Out: <fileName>
The name of the generated proxy code. The default name is from
Derived from the service name. The abbreviation is "/o :".

/Protocol: <protocol>
Rewrite the default protocol to be implemented. From "SOAP", "SOAP12 ",
Select from "HttpGet", "HttpPost", or the custom protocol specified in the configuration file.

/Username: <username>
/Password: <password>
/Domain: <domain>
Connect to
The Credential used by the server. The abbreviations are "/u:", "/p:", and "/d :".

/Proxy: <url>
The URL of the proxy server used for the HTTP request.
By default, system proxy settings are used.

/Proxyusername: <username>
/Proxypassword: <password>
/Proxydomain: <domain>
The Credential used to connect to the proxy server that requires authentication.
The abbreviations are "/pu:", "/pp:", and "/pd :".

/Appsettingurlkey: <key>
The default configuration item used to read the URL attribute in code generation. The default value is non-slave configuration.
File. It is abbreviated as "/urlkey :".

/Appsettingbaseurl: <baseurl>
The base URL used to calculate the URL segment.
You must also specify the deleettingurlkey option. The URL segment is
Calculated from appsettingbaseurl
The result of the relative URL of the URL in the WSDL document. The abbreviation is "/baseurl :".

/Parsableerrors
Output Error. The format is similar to that reported by the compiler.

3. Run the wsdl.exe command on the command line:

F: \ WorkPlace \ MyProject \ WebService> "C: \ Program Files \ Microsoft Visual Studio. NET 2003 \ SDK \ v1.1 \ Bin \ wsdl.exe "/l: CS/n: namespace of the SOAP proxy class/out: C # File Name of the SOAP proxy class. cs WSDL source file. wsdl

For example:

F: \ WorkPlace \ MyProject \ WebService> "C: \ Program Files \ Microsoft Visual Studio. NET 2003 \ SDK \ v1.1 \ Bin \ wsdl.exe "/l: CS/n: Stephen cat. webService. sycService/out: SycService. cs syc. wsdl

Screen output after execution:

Microsoft (R) Web Service Description Language Utility
[Microsoft (R). NET Framework, version 1.1.4322.573]
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.

The file "SycService. cs" is being written ".

The generated SOAP proxy file contains the following content:

1. proxy class with the same name as WebService class and with the same method, such as class Syc
2. All serializable object entity classes used in the WebService class, such as class UsersEntity
3. All the above classes are included in the same namespace, which is specified in the parameters for executing the wsdl in the command line.

The service requester calls the Syc synchronization interface.

See Microsoft ASP. NET quick start tutorial Web service example:
Http://chs.gotdotnet.com/quickstart/util/srcview.aspx? Path =/quickstart/aspplus/samples/services/DataTypes/DataTypesClient. src & file = CS \ DataTypesClient. aspx & font = 3

The SOAP proxy file generated by WDSL:
Http://chs.gotdotnet.com/quickstart/util/srcview.aspx? Path =/quickstart/aspplus/samples/services/DataTypes/DataTypesClient. src & file = CS \ DataTypes. cs & font = 3

Where:
DataTypes is a SOAP proxy class
Order is an object class
From http://www.cnblogs.com/stephencat/archive/2006/04/06/368122.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.