Transcript Document
.NET web szolgáltatások XSD WSDL SOAP Web szolgáltatás készítés, fogyasztás Bilicki Vilmos [email protected] www.inf.u-szeged.hu/~bilickiv 1 Az előző előadás tartalma XSLT programozás Nevesített sablonok Váltózó, paraméterek Ciklusok, feltételek XSLT hivatkozások Több XML dokumentum használata egy stíluslapban Kereszthivatkozások Kereszthivatkozások az eredmény dokumentumban 2 XML Schema Definition Language XML elemkészletet és nyelvtant definiál XML szintaxist használ (SAX,DOM) A legtöbb programnyelvben használt adattípusokat használhatjuk A névterek használata lehetővé teszi több séma egybeolvasztását 3 XSD Típusokat definiálhatunk Beépített típusokat használhatunk (http://www.w3.org/TR/xmlschema-2/) Szűkítéses származtatás Az érték határok mellett a lexikális formátum is definiált (0,1-true,false) -> kompatibilitás 4 Értékek, Lexikális típusok 5 Beépített adattípusokat használhatunk Típusok definiálása névtéren belül A felhasználók definiálhatnak típusokat(UDT) Típus definiáláskor hasonlóan az OO nyelvekhez célszerű névtereket használni 6 Példa <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.org/publishing" xmlns:tns="http://example.org/publishing"> <!-- type definitions --> <xsd:simpleType name="AuthorId"> <!-- define value space details here --> </xsd:simpleType> <xsd:complexType name="AuthorType"> <!-- define structural details here --> </xsd:complexType> <!-- global element/attribute declarations --> <xsd:element name="author" type="tns:AuthorType"/> <xsd:attribute name="authorId" type="tns:AuthorId"/> ... </xsd:schema> 7 XSD Alapelemek 8 Egyszerű típus: xsd:simpleType csak szöveges elemek értéke és attribútuma lehet Összetett típus: xsd:complexType struktúrát definiál Elem definiálása: xsd:element Attribútum definiálása: xsd:attribute Egyszerű típus konstrukció: xsd:restriction xsd:union xsd:list Használatuk XML-ben <x:author xmlns:x="http://example.org/publishing"> <!-- structure determined by complexType definition --> ... </x:author> Vagy <genericId xmlns:x="http://example.org/publishing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="tns:AuthorId" >333-33-3333</genericId> 9 Szűkítési minták 10 xsd:enumeration (lista) xsd:fractionDigits (a tizedesjegy után) xsd:length () xsd:maxExclusive xsd:maxInclusive xsd:maxLength xsd:minExclusive xsd:minInclusive xsd:minLength xsd:pattern xsd:totalDigits xsd:whiteSpace Példa: Egyszerű típusok ... <xsd:simpleType name="RoyaltyRate"> <xsd:restriction base="xsd:double"> <xsd:minInclusive value="0"/> <xsd:maxInclusive value="100"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="Pubs2003"> <xsd:restriction base="xsd:date"> <xsd:minInclusive value="2003-01-01"/> <xsd:maxInclusive value="2003-12-31"/> </xsd:restriction> </xsd:simpleType> <xsd:element name="rate" type="tns:RoyaltyRate"/> <xsd:element name="publicationDate" type="tns:Pubs2003"/> ... 11 Példa: Lexikális szűkítés ... <xsd:simpleType name="SSN"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{3}-\d{2}-\d{4}"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="PublisherAssignedId"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{2}-\d{8}"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType name="Phone"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\(\d{3}\)\d{3}-\d{4}"/> </xsd:restriction> </xsd:simpleType> <xsd:element name="authorId" type="tns:SSN"/> <xsd:element name="pubsAuId" type="tns:PublisherAssignedId"/> <xsd:element name="phone" type="tns:Phone"/> 12... Példa: felsorolás ... <xsd:simpleType name="PublicationType"> <xsd:restriction base="xsd:NMTOKEN"> <xsd:enumeration value="Book"/> <xsd:enumeration value="Magazine"/> <xsd:enumeration value="Journal"/> <xsd:enumeration value="Online"/> </xsd:restriction> </xsd:simpleType> <xsd:element name="pubType" type="tns:PublicationType"/> ... 13 Példa: unió ... <xsd:simpleType name="AuthorId"> <xsd:union memberTypes="tns:SSN tns:PublisherAssignedId"/> </xsd:simpleType> <xsd:element name="authorId" type="tns:AuthorId"/> ... 14 Komplex típusok xsd:complexType: névtér definiálás: 15 xsd:sequence xsd:choice xsd:all ref ##any ##other ##targetNamespace ##local Lista <xsd:complexType name="AddressType"> <xsd:all> <xsd:element name="street" type="xsd:string"/> <xsd:element name="city" type="xsd:string" minOccurs="0"/> <xsd:element name="state" type="tns:State" minOccurs="0"/> <xsd:element name="zip" type="tns:Zip"/> </xsd:all> </xsd:complexType> <xsd:complexType name="PublicationsListType"> <xsd:choice maxOccurs="unbounded"> <xsd:element name="book" type="xsd:string"/> <xsd:element name="article" type="xsd:string"/> <xsd:element name="whitepaper" type="xsd:string"/> </xsd:choice> </xsd:complexType> <xsd:complexType name="AuthorType"> <xsd:sequence> <xsd:choice> <xsd:element name="name" type="xsd:string"/> <xsd:element name="fullName" type="xsd:string"/> </xsd:choice> <xsd:element name="address" type="tns:AddressType"/> <xsd:element name="phone" type="tns:Phone" minOccurs="0" maxOccurs="unbounded"/> <xsd:element name="recentPublications„ type="tns:PublicationsListType"/> </xsd:sequence> <xsd:attribute name="id" type="tns:AuthorId"/> </xsd:complexType> <xsd:element name="author" type="tns:AuthorType"/> 16 Objektumok mentése XMLként A publikus adattagokat autómatikusan elmenti Ezen tudunk változtatni Attribútumok segítségével public class TaxRates{ [XmlElement(ElementName = "TaxRate")] public decimal ReturnTaxRate; } System.Xml.Serialization XmlRootAttribute XmlElementAttribute XmlAttributeAttribute XmlArrayAttribute XmlArrayItemAttribute 17 Példa • • • • • • • • • • • • • • 18 [XmlRoot("account")] public class Acct { [XmlElement("description")] public string Description; [XmlElement("number")] public string Number; [XmlElement("type")] public string Type; [XmlElement("balance")] public decimal Balance; [XmlAttribute("status")] public string Status; } ... [return:XmlArray("AccountList")] [return:XmlArrayItem("Account")] public Acct[] GetAllAccounts() ... SOAP MEP Message Exchanging Patterns: Request – Response Message Peer-to-Peer … 19 SOAP elemek Envelope Header (mustUnderstand) Body • Fault • • • • VersionMismatch MustUnderstand Client Server SOAP kódolások: 20 RPC Dokumentum Példa <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <soap:Fault> <faultcode>soap:Server</faultcode> <faultstring>Insufficient funds</faultstring> <detail> <x:TransferError xmlns:x="urn:examples-org:banking"> <sourceAccount>22-342439</sourceAccount> <transferAmount>100.00</transferAmount> <currentBalance>89.23</currentBalance> </x:TransferError> </detail> </x:TransferFunds> </soap:Body> </soap:Envelope> 21 C# példa public static string BuildSOAPMessage() { MemoryStream st; st = new MemoryStream(1024); XmlTextWriter tr = new XmlTextWriter(st,Encoding.UTF8); tr.WriteStartDocument(); tr.WriteStartElement("soap","Envelope", "http://schemas.xmlsoap.org/soap/envelope/"); tr.WriteAttributeString("xmlns","xsi",null, "http://www.w3.org/2001/XMLSchema-instance"); tr.WriteAttributeString("xmlns","xsd",null, "http://www.w3.org/2001/XMLSchema"); tr.WriteAttributeString("xmlns","soap",null, "http://schemas.xmlsoap.org/soap/envelope/"); tr.WriteStartElement("Body", "http://schemas.xmlsoap.org/soap/envelope/"); tr.WriteStartElement(null,"GetAccount", "http://woodgrovebank.com"); tr.WriteElementString("acctNumber","1234"); tr.WriteEndElement(); tr.WriteEndElement(); tr.WriteEndDocument(); tr.Flush(); 22 } WSDL Web Service Description Language Ahol az XML Schema befejeződik ott kezdődik XML interfészeket definiál Üzenet szekvencia (Műveletek) Port Szolgáltatás 23 WSDL elemek types – XSD definíciók message – Több típus portType – Üzenet váltás binding – konkrét protokoll és adat formátum service – Összetartozó végpontok gyűjteménye (URI, binding) 24 Types 25 MathInput (Add, Subtract, Multiply, Divide) … <types> <xs:schema targetNamespace="http://example.org/math/types/" xmlns="http://example.org/math/types/" > <xs:complexType name="MathInput"> <xs:sequence> <xs:element name="x" type="xs:double"/> <xs:element name="y" type="xs:double"/> </xs:sequence> </xs:complexType> </xs:complexType> <xs:element name="Add" type="MathInput"/> <xs:element name="AddResponse" type="MathOutput"/> … </xs:schema> </types> ... </definitions> Message … <message name="AddMessage"> <part name="parameter" element="ns:Add"/> </message> <message name="AddResponseMessage"> <part name="parameter" element="ns:AddResponse"/> </message> … 26 Interface <portType name="MathInterface"> <operation name="Add"> <input message="y:AddMessage"/> <output message="y:AddResponseMessage"/> </operation> <operation name="Subtract"> <input message="y:SubtractMessage"/> <output message="y:SubtractResponseMessage"/> </operation> <operation name="Multiply"> <input message="y:MultiplyMessage"/> <output message="y:MultiplyResponseMessage"/> </operation> <operation name="Divide"> <input message="y:DivideMessage"/> <output message="y:DivideResponseMessage"/> </operation> </portType> 27 Binding <binding name="MathSoapHttpBinding" type="y:MathInterface"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="Add"> <soap:operation soapAction="http://example.org/math/#Add"/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> ... </binding> 28 Services, SOAP Add üzenet <service name="MathService"> <port name="MathEndpoint" binding="y:MathSoapHttpBinding"> <soap:address location="http://localhost/math/math.asmx"/> </port> </service> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" > <SOAP-ENV:Body> <m:Add xmlns:m="http://example.org/math/types/"> <x>3.14159265358979</x> <y>3.14159265358979</y> </m:Add> </SOAP-ENV:Body> </SOAP-ENV:Envelope> 29 DEMÓ Web szolgáltatás készítése 30 A Web szolgáltatás elemei(1) Az .asmx fájl Szolgáltatás Súgó oldal Szolgáltatás metódus súgó oldal Szolgáltatás leíró oldal Global.asax Web.config A .vsdisco fájl AssemblyInfo (.cs or .vb) A /bin mappa 31 Web metódusok A WebMethod attribútummal ajánlhatjuk ki Paraméterei BufferResponse CacheDuration Description EnableSession MessageName TransactionOption 32 Web szolgáltatás felfedezés Disco Disco.Exe használata Programozói támogatás Web Service 33 Web Service Consumer Disco Statikus felfedezés Dinamikus felfedezés WS-Inspection 34 Disco.exe Szintaxis disco [options] URL Disco.exe Example disco /out:d:\disco /u:administrator /p:biffle http://www.woodgrovebank.com/catalog.disco 35 Programozói támogatás System.Web.Services.Discovery DiscoveryClientProtocol DiscoveryDocument DiscoveryClientResultCollection 36 Web szolgáltatás proxi-k Kapcsolatuk a WSDL-el Proxi generálás Wsdl.exe segítségével Proxi-k konfigurálása Web Service 37 Web Service Consumer Proxi és WSDL Miért van szükség rájuk? WSDL használata proxi-k generálásához 38 Wsdl.exe Szintaxis Wsdl.exe wsdl [options] {URL | Path} Példa wsdl http://www.woodgrovebank.com/services/bank.asmx?wsdl Példa wsdl /l:VB /protocol:HttpGet /out:Bank.vb http://www.woodgrovebank.com/services/bank.asmx?wsdl 39 Proxi-k beállításai Url tulajdonság Bizonyítékok Timeout Proxy tulajdonság AllowAutoRedirect tulajdonság Bank theBank = new Bank(); theBank.Url = "http://eastcoast.woodgrovebank.com/Bank.asmx"; ICredentials credentials = new NetworkCredential ("Adam", "ABarr-user", "woodgrovebank.com"); theBank.Credentials = credentials; theBank.Timeout = 20000; IWebProxy proxyObject = new WebProxy("http://proxyserver:80", true); theBank.Proxy = proxyObject; theBank.AllowAutoRedirect = true; 40 Szinkron vs. Aszinkron kliens A szinkron hívások korlátai Aszinkron hívások 41 Az előadás tartalma XSD SOAP WSDL Web szolgáltatás készítés, fogyasztás 42 A következő előadás tartalma Web szolgáltatások Biztonság WS-profilok 43