WebService Simple Implementation

Source: Internet
Author: User
Tags soap

1. WebService

SOAP, WSDL, UDDI
SOAP, Simple Object access Protocal, is a simple protocol for exchanging information in decentralized or distributed environments.
WSDL (Web Services Description language,web Service Description Language) explanation of the WebService document describing the XML format of the Web service publication
The abbreviation for UDDI Unified Description, Discovery, and integration (Universal Description, Discovery, and integration) is the Yellow pages of Web services, which is an XML-based cross-platform description specification, It allows companies worldwide to publish their services on the Internet for other customers to inquire about.

WebService works: The client makes a call request to the server segment, emits a SOAP message, and the service side returns the result after the SOAP message is processed, based on the SOAP protocol.

Advantages: Cross-language, cross-platform, not limited to LAN and WAN, distributed offsite call. Soap Orchestration uses HTTP and XML (text language) for communication

Disadvantage: The amount of data transmitted is too large, redundant data too much, the sense of speed delay.

Web service: Interoperable, Distributed Application Web Service: Communication with HTTP and XML Web service: Can traverse firewalls, truly free communication call the default 80 port by using SOAP implementation offsite

The following properties are available for Web service (Web services).

Namespace: The default is "http://tempuri.org/", and the value of this property contains the default namespace for the XML Web Service. The XML namespace provides a way to create a name in an XML document that can be identified by a Uniform Resource Identifier (URI). Use an XML namespace to uniquely identify an element or attribute in an XML document thus, in the service description of the XML Web service, Namespace is used as the default namespace for XML elements that are directly related to the XML Web service. If you do not specify a namespace, the default namespace, http://tempuri.org/, is used.

Name: The value of this property contains the name of the XML Web Service. By default, this value is the name of the class that implements the XML Web Service.

Description: The value of this property contains a descriptive message that will be displayed to the potential user of the XML Web service after the XML Web service's description file (such as the service Description and Service help page) is generated.

The WebMethod (Web service method) has the following 6 properties.

Description: is a descriptive information about the Web Service method. Just like the functional annotations of the Web Service method, annotations can be made visible to callers.

EnableSession: Indicates whether the Web Service starts the Session flag, which is done primarily through a Cookie, false by default.

MessageName: Major implementation of method overloading after renaming:

TransactionOption: Indicates transaction support for the Web Service method.

CacheDuration: Sets the number of seconds the response should remain in the cache. This way, the Web Service does not need to be repeated multiple times to improve access efficiency, and cacheduration is the attribute that specifies the cache time.

The simple code is implemented as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.EnterpriseServices;usingSystem.Linq;usingsystem.web;usingSystem.Web.Services;usingSystem.Web.Services.Protocols;namespacetestwebservice1{/// <summary>    ///Summary description of WebService1/// </summary>[WebService (Namespace ="http://yxl.org/", name="MyWebService", description="my webservice.")] [WebServiceBinding (ConformsTo=wsiprofiles.none)] [System.ComponentModel.ToolboxItem (false)]    //To allow this Web service to be called from a script using ASP. NET AJAX, uncomment the following line. //[System.Web.Script.Services.ScriptService]     Public classWebService1:System.Web.Services.WebService { PublicMysoapheader MyHeader; [SoapHeader ("MyHeader")] [WebMethod (MessageName="GetTime")]         Public stringGetTime () {if(!Myheader.isvalid ()) {                return "Sorry, you do not have permission to access"; }            returnDateTime.Now.ToString (); } [WebMethod (Description="get all the students", enablesession=false, transactionoption=transactionoption.required,cacheduration=Ten)]         PublicList<student>getstudent () {return NewList<student>()            {                NewStudent () {Id =1, Name ="Zhang 31"},                NewStudent () {Id =2, Name ="Zhang 32"},                NewStudent () {Id =3, Name ="Zhang 33"}            }; } [WebMethod (Description="get all the students 1", enablesession =false, messagename="GetStudent2")]         PublicList<student> Getstudent (stringname) {            return NewList<student>()            {                NewStudent () {Id =1, Name ="Zhang 31"},                NewStudent () {Id =2, Name ="Zhang 32"},                NewStudent () {Id =3, Name ="Zhang 33"}            }; }    }     Public classStudent { Public intId {Get;Set; }  Public stringName {Get;Set; } }     Public classMysoapheader:soapheader { Public stringUserName {Get;Set; }  Public stringPassword {Get;Set; }  Public BOOLIsValid () {if(UserName = ="Admin"&& Password = ="123")            {                return true; }            Else            {                return false; }        }    }}

Post-release calls are implemented as follows:

usingShowstudentinfo.servicereference1;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;namespaceshowstudentinfo{ Public Partial class_default:page {protected voidPage_Load (Objectsender, EventArgs e) {            if(!IsPostBack)            {Loadinfo (); }        }         Public voidLoadinfo () {using(Mywebservicesoapclient client =Newmywebservicesoapclient ()) {                //student[] Students = client.                Getstudent (); //foreach (var student in students)//{                //Response.Write (student.                Name + "</br>"); //}                //Response.Write ("Mysoapheader MyHeader=NewMysoapheader (); Myheader.username="Admin"; Myheader.password="123";//user name and password are consistent with the calling web ServiceResponse.Write (client.            Gethello (MyHeader)); }        }    }}

WebService Simple Implementation

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.