Web-
ASMX Web- , . Calc.asmx wwwroot :
http://localhost/calc.asmx
. 2. ASP.NET HTTP- Calc.asmx HTML- Web-. WebService ASMX- . Web-, , , WebMethods.
. 2. Calc.asmx Internet Explorer
Add , ASP.NET , Add (. 3). ASP.NET , DLL, Calc.asmx.
. 3. Add
ASP.NET HTML-, Add . 2 2 b Invoke. XML, Web-, (. 4).
. 4. XML, Add
, ASP.NET ASMX-, Web- . Web-, .NET Framework, . :
http://terraservice.net/terraservice.asmx
URL Microsoft TerraService Web-, , Microsoft TerraServer. TerraService Web-. , TerraService, , ASP.NET.
Web-
, Web- ASMX- DLL. .5 Calc.asmx , . . , , Calc.cs. Calc.cs Calc.dll:
csc /t:library calc.cs
DLL bin (, wwwrooot\bin).
Calc.asmx
<%@ WebService Class="CalcService" %>
Calc.cs
using System;
using System.Web.Services;
[WebService (Name="Calculator Web Service",
Description="Performs simple math over the Web")]
class CalcService
{
[WebMethod (Description="Computes the sum of two integers")]
public int Add (int a, int b)
{
return a + b;
}
[WebMethod
(Description="Computes the difference between two integers")]
|
|
public int Subtract (int a, int b)
{
return a - b;
}
}
. 5. Web- Calc
Web- , Web-: , , ASP.NET .
WebService
ASMX- Web- WebService:
class CalcService: WebService
WebService System.Web.Services. Application, Session, Context, Server User, Web- ASP.NET Web- , , , WebService.
WebMethod
WebMethod Web-. Web-, .NET Framework Web-. WebMethod, , Web-, , . :
BufferResponse / .
CacheDuration .
Description Web-.
EnableSession / Web-.
MessageName Web-.
Transaction-Option Web-.
CacheDuration ASMX- @ OutputCache ASPX ASCX: , . , Web-, :
[WebMethod]public string GetCurrentTime (){ return DateTime.Now.ToShortTimeString ();}ToShortTimeString , , , . CacheDuration 10 :
WebMethod (CacheDuration="10")]public string GetCurrentTime (){ return DateTime.Now.ToShortTimeString ();}, Web- GetCurrentTime, .
Web- , ASP.NET Web- . EnableSession WebMethod. Web- , WebService ( Session) EnableSession=true , :
class CalcService: WebService{ [WebMethod (EnableSession="true", Description="Adds an item to a shopping cart")] public void AddToCart (Item item) { ShoppingCart cart = (ShoppingCart) Session["MyShoppingCart"]; cart.Add (item); }}Web- , Web-, .
|
|
MessageName Web- , , . , Web- Add , Web-:
[WebMethod]public int Add (int a, int b){ return a + b;} [WebMethod]public float Add (float a, float b){ return a + b;}, . Web- # . ? , WebMethod MessageName:
WebMethod (MessageName="AddInts")]public int Add (int a, int b){ return a + b;} [WebMethod (MessageName="AddFloats")]public float Add (float a, float b){ return a + b;}# - , Web- Addlnts AddFloats.
Web-
Web- (. . ), , , , , Web-cep (URL). WSDL (Web Services Description Language).
WSDL . XML-, IBM, Microsoft . ( . http://www.w3.org/TR/wsdl). . -, . -, WSDL , . -, WSDL Web-, .NET Framework, : URL Web- WSDL:
http://www.wintellect.com/calc.asmx?wsdl
(. 6) service, Web-, operation, Web-, , binding, Web- .
. 6.WSDL- Calc.asmx
Web-, WSDL-. Web- .NET Framework URL ?wsdl . Web-. . , -, Web-. .NET Framework SDK Wsdl.exe
Web-
, Web- . , . ? , Web-? , , , Web-?
, XML . Web- . 7. FindStores, (, ). FindStores FindByState, Pubs, Microsoft SQL Server, Bookstore. (: FindByState Web-, WebMethod) FindStores . Bookstore , ASMX-.
. 8 XML, FindStore . Bookstore XML SysternXmlSerializationXmlSerializer. NET Framework, XML-. , XML , , Bookstore. XML , .
|
|
XML-, Bookstore? , WSDL- . WSDL- Locator.asmx , Bookstore ( Bookstore), types :
<s:complexType name="ArrayOfBookstore"> <s:sequence> <s:element minOccurs="0" maxOccurs="unbounded" name="Bookstore" nillable="true" type="s0:Bookstore" /> </s:sequence></s:complexType><s:complexType name="Bookstore"> <s:sequence> <s:element minOccurs="1" maxOccurs="1" name="Name" nillable="true" type="s:string" /> <s:element minOccurs="1" maxOccurs="1" name="Address" nillable="true" type="s:string" /> <s:element minOccurs="1" maxOccurs="1" name="City" nillable="true" type="s:string" /> <s:element minOccurs="1" maxOccurs="1" name="State" nillable="true" type="s:string" /> </s:sequence></s:complexType>
Locator.asmx
<%@ WebService Language="C#" Class="LocatorService" %>
using System;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
[WebService (Name="Bookstore Locator Service",
Description="Retrieves bookstore information from the Pubs database")]
class LocatorService
{
[WebMethod (Description="Finds bookstores in a specified state")]
public Bookstore[] FindStores (string state)
{
return FindByState (state);
}
Bookstore[] FindByState (string state)
{
SqlDataAdapter adapter = new SqlDataAdapter
("select * from stores where state = \'" + state + "\'",
"server=localhost;database=pubs;uid=sa;pwd=");
DataSet ds = new DataSet ();
adapter.Fill (ds);
DataTable table = ds.Tables[0];
Bookstore[] stores = new Bookstore[table.Rows.Count];
for (int i=0; i<table.Rows.Count; i++) {
stores[i] = new Bookstore (
table.Rows[i]["stor_name"].ToString ().TrimEnd
(new char[] { ' ' }),
table.Rows[i]["stor_address"].ToString ().TrimEnd
(new char[] { ' ' }),
table.Rows[i]["city"].ToString ().TrimEnd
(new char[] { ' ' }),
table.Rows[i]["state"].ToString ().TrimEnd
(new char[] { ' ' })
);
}
return stores;
}
}
public class Bookstore
{
public string Name;
public string Address;
public string City;
public string State;
public Bookstore () {}
public Bookstore (string name, string address, string city,
string state)
{
Name = name;
Address = address;
City = city;
State = state;
}
}
. 7. Web-
, Bookstore Bookstore . , . .NET Framework, , .
|
|
.8. XML, FindStores
Locator.asmx , Web-, , . .
■ , Web- HTTP GET POST. , Web- SOAP, ASP.NET , . No test form is available because this method does not support HTTP GET1 , , . ASP.NET HTTP GET.
■ , Web- , , . , XML-.NET Framework .
, Web- .