Shakespears sonetter
Vi bygger en Web Service i Visual Studio. Vi lager ServiceSonette.asmx og App_Code/ServiceSonette.cs, og vi plasserer fila: App_Data/sonetter.xml.
Fila ServiceSonette.asmx inneholder kun en linje som kopler oss til kodefila:
<%@ WebService Language="C#" CodeBehind="~/App_Code/ServiceSonette.cs" Class="ServiceSonette" %>
Koden ser slik ut, etter at vi har redigert den:
using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml; /// <summary> /// Summary description for ServiceSonette /// </summary> [WebService(Namespace = "http://www.donau.hiof.no/borres/dn/demosite1/s2/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class ServiceSonette : System.Web.Services.WebService { public ServiceSonette() { } [WebMethod(Description = "Returnerer en sonette som en string-array." + "Input er et heltall i omr�det [1..154]")] public string[] Sonette(int n) { n = Math.Min(Math.Max(0,n-1), 153); string physicalPath = HttpContext.Current.Server.MapPath(".").ToString(); String filename = physicalPath + "\\App_Data\\sonetter.xml"; String[] result; try { XmlDocument doc = new XmlDocument(); doc.Load(filename); XmlNodeList list = doc.GetElementsByTagName("sonnet"); result = new string[list[n].ChildNodes.Count]; list[n].Normalize(); XmlNode nod = list[n].FirstChild; int ix = 0; while (nod != null) { result[ix++] = nod.InnerText; nod = nod.NextSibling; } } catch (Exception ex) { result = new string[] { "Fant ingen", "sonnette" }; } return result; } }
Bruk fra et program

Vi lager på vanlig måte en Windows Application i Visual Studio. I Solution Explorer, under References, legger vi til en WebReference og oppgir url'en til vår webservice: "http://donau.hiof.no/borres/dn/service1/ServiceSonette.asmx". Vi kaller den "sonette".
Vi kan da skrive følgende kode:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace UseSonette { public partial class Form1 : Form { public Form1() { InitializeComponent(); ShowSonette(1); } private void button1_Click(object sender, EventArgs e) { try{ int n=Convert.ToInt32(textBox2.Text); if( (n > 154) || (n < 1) ) throw new Exception(); ShowSonette(n); } catch(Exception ex){ label3.Visible=true; } } private void ShowSonette(int n) { sonette.ServiceSonette son = new sonette.ServiceSonette(); textBox1.Lines = son.Sonette(n); label3.Visible = false; } } }
Bruk fra webside
Framgangsmåten blir den samme som fra et program.
Vi kan da skrive følgende kode:
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { TextBox2.Text = "1"; ShowSonette(1); } } protected void Button1_Click(object sender, EventArgs e) { try { int n = Convert.ToInt32(TextBox2.Text); if ((n < 1) || (n > 154)) throw new Exception(); ShowSonette(n); } catch (Exception ex) { Label3.Visible = true; } } protected void ShowSonette(int n) { String T = ""; sonette.ServiceSonette son=new sonette.ServiceSonette(); String[] slist=son.Sonette(n); for(int ix=0;ix<slist.Length;ix++) T+="\n"+slist[ix]; TextBox1.Text = T; Label3.Visible = false; } }
Fler detaljer
Fila som beskriver tjenesten sett fra programmet side ser slik ut:
<?xml version="1.0" encoding="utf-8"?> <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://www.donau.hiof.no/borres/dn/demosite1/s2/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://www.donau.hiof.no/borres/dn/demosite1/s2/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> <wsdl:types> <s:schema elementFormDefault="qualified" targetNamespace="http://www.donau.hiof.no/borres/dn/demosite1/s2/"> <s:element name="Sonette"> <s:complexType> <s:sequence> <s:element minOccurs="1" maxOccurs="1" name="n" type="s:int" /> </s:sequence> </s:complexType> </s:element> <s:element name="SonetteResponse"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="SonetteResult" type="tns:ArrayOfString" /> </s:sequence> </s:complexType> </s:element> <s:complexType name="ArrayOfString"> <s:sequence> <s:element minOccurs="0" maxOccurs="unbounded" name="string" nillable="true" type="s:string" /> </s:sequence> </s:complexType> </s:schema> </wsdl:types> <wsdl:message name="SonetteSoapIn"> <wsdl:part name="parameters" element="tns:Sonette" /> </wsdl:message> <wsdl:message name="SonetteSoapOut"> <wsdl:part name="parameters" element="tns:SonetteResponse" /> </wsdl:message> <wsdl:portType name="ServiceSonetteSoap"> <wsdl:operation name="Sonette"> <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> Returnerer en sonette som en string-array. Input er et heltall i området [1..154] </wsdl:documentation> <wsdl:input message="tns:SonetteSoapIn" /> <wsdl:output message="tns:SonetteSoapOut" /> </wsdl:operation> </wsdl:portType> <wsdl:binding name="ServiceSonetteSoap" type="tns:ServiceSonetteSoap"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="Sonette"> <soap:operation soapAction="http://www.donau.hiof.no/borres/dn/demosite1/s2/Sonette" style="document" /> <wsdl:input> <soap:body use="literal" /> </wsdl:input> <wsdl:output> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:binding name="ServiceSonetteSoap12" type="tns:ServiceSonetteSoap"> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="Sonette"> <soap12:operation soapAction="http://www.donau.hiof.no/borres/dn/demosite1/s2/Sonette" style="document" /> <wsdl:input> <soap12:body use="literal" /> </wsdl:input> <wsdl:output> <soap12:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="ServiceSonette"> <wsdl:port name="ServiceSonetteSoap" binding="tns:ServiceSonetteSoap"> <soap:address location="http://donau.hiof.no/borres/dn/service1/ServiceSonette.asmx" /> </wsdl:port> <wsdl:port name="ServiceSonetteSoap12" binding="tns:ServiceSonetteSoap12"> <soap12:address location="http://donau.hiof.no/borres/dn/service1/ServiceSonette.asmx" /> </wsdl:port> </wsdl:service> </wsdl:definitions>