Create a web service
I will create a web service called SecurityWebService with C #. The extension of a web service file is: .asmx (like the file extension of ASP.NET file extensions), the first line of the file is:
<% @ Webservice Language = "C #" class = "securitywebservice"%>
The meaning of this statement is: telling the compiler to run the Web Service mode, and the C # class name. We also need to access the Web Service namespace, which is also a good practice to reference the system name space.
Using system;
Using system.Web.services;
SecurityWebService should inherit the function of the Web Service class, so we need to add the following line code.
Public Class SecuritywebService: WebService
Now we use object-oriented programming skills to create a class, C # class is very similar to C and Java, using C # to build a class piece as simple as going to the park, and do not need any techniques.
The basic data type of C # is very smart, so if we return "int," "float," or "string", then turn them into standard XML output. Unfortunately, in most examples we need to see the obtained data set as a single entity. Now let's give an example.
Our SecurityWebService stock quotation system requires users to enter stock code, and will return a complete company name and current stock price, so we have three information blocks for a stock.
1, company code (String)
2, company name (String)
3, price (double)
When we submit stocks, we need to extract all three data. There are several ways to complete this work. The best way is to bind them into an enumerated data type, we can use in C # "Struct" is completed, "struct" and C in C # are similar.
Public Struct SecurityInfo
{
Public String Code;
Public String CompanyName;
Public Double Price;
}
We can create a web service through the module, the code is as follows:
<% @ Webservice Language = "C #" class = "securitywebservice"%>
Using system;
Using system.Web.services;
Public Struct SecurityInfo
{
Public String Code;
Public String CompanyName;
Public Double Price;
}
Public Class SecuritywebService: WebService
{
PRIVATE SecurityInfo Security;
Public securityWebservice ()
{
SECURITY.CODE = ""
Security.comPanyName = "";
SECURITY.PRICE = 0;
}
Private Void AssignValues (String code) {
// this is where you use your business component.
// Method Calls on Business Components Are Used to Populate The Data.
// for Demonstration Purposes, I will add a string to the code and
// use a random number generator to create the price feed.
SECURITY.CODE = Code
Security.comPanyName = Code "Pty Ltd";
Random randomnumber = new system.random ();
Security.price = double.parse (NEWMNUMBER.NEXT (1, 10)). Nextdouble (). Format ("##. ##", null);
}
[WebMethod (description = "this method Call Will Get The Company Name and the price for a given security code.", Enablesession = false]
Public SecurityInfo GetSecurityInfo (String Code)
{
AssignValues (Code);
SecurityInfo securityDetails = new securityInfo ();
SecurityDetails.code = security.code;
SecurityDetails.comPanyName = security.comPanyName;
SecurityDetails.price = security.price;
Return securitydetails;
}
}
Remember that all users can access Web Service via HTTP, maybe you will talk about confidential business data in your code and how to conserve data confidentiality. The solution is to protect the business logic function block, allowing access to the representation layer, can be used in C #, you can use the keyword "[Web Method]" to achieve this, let's take a look at the following code:
[WebMethod (Description = "this ...", enablesession = false]
Public SecurityInfo GetSecurityInfo (String Code)
This function is displayed to the public, the Description tag is used to describe the web service's feature, because we cannot store any session data, we will eliminate the session state.
Private Void AssignValues (String Code)
This business logic is not known by the public. We don't want sensitive business information being published on the web (note: even change the private to public, the public still can't see, why? Keywords.)
We can use the business logic to get the latest stock quotes in this function. For this purpose, I add a text box to the code to enter the company name, and the price is generated by a random function.
We save this file in SampleService.asmx in the IIS directory. I saved him under the virtual directory "/ Work / Aspx", similar to the web browser as shown below: (here) This web page is generated by .NET Framework, we did not create this page (this is The system is automatically generated. We didn't write any line of code for him, which is a by-product of the previous code.
Use ASP.NET and Config.Web files to easily change this page. But pay attention to the link of the SDL specification (even if we use the WSDL, .NET version still references SDL, this problem has a promising correction in the next version), this is a description file of the web service to create a proxy object, this Basically give a general introduction to the web service, if you are more familiar with these, you can only look at the "web-only" method, the SDL specification is not described for all private functions and properties, the SDL specification of the SecurityWebService class is in routine See it in A.