Flex (flash builder) consumes Asp.net WebService

Source: Internet
Author: User

Service1.asmx. CS

 
Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. services; namespace testwebserviceforflex {// <summary> // Summary of service1 /// </Summary> [WebService (namespace = "http://tempuri.org/")] [webservicebinding (conformsto = wsiprofiles. basicprofile1_1)] [system. componentmodel. toolboxitem (false)] // to allow ASP. net Ajax calls this web service from the script. Please cancel the comments to the downstream. // [System. web. script. services. scriptservice] public class service1: system. web. services. webService {[webmethod] Public String helloworld () {return "Hello world, China";} [webmethod] public double calc (double radius) {double D = radius * Math. pi; return D ;}}}

Frontend flex call:

_ Super_service1.as

/*** This is a generated class and is not intended for modfication. to customize behavior * of this service wrapper you may modify the generated sub-class of this class-service1.as. */package services. service1 {import COM. adobe. fiber. core. model_internal; import COM. adobe. fiber. services. wrapper. webservicewrapper; import COM. adobe. serializers. utility. typeutility; import MX. RPC. abstractoperation; import MX. RPC. asynctoken; import MX. RPC. soap. mxml. operation; import MX. RPC. soap. mxml. webService; [excludeclass] internal class _ super_service1 extends COM. adobe. fiber. services. wrapper. webservicewrapper {// constructor public function _ super_service1 () {// initialize service control _ servicecontrol = new MX. RPC. soap. mxml. webService (); var operations: Object = new object (); var operation: MX. RPC. soap. mxml. bytes Ation; Operation = new MX. RPC. soap. mxml. operation (null, "helloworld"); operation. resulttype = string; operations ["helloworld"] = operation; Operation = new MX. RPC. soap. mxml. operation (null, "calc"); operation. resulttype = number; operations ["calc"] = operation; _ servicecontrol. operations = operations; try {_ servicecontrol. convertresulthandler = com. adobe. serializers. utility. typeutility. convertresu Lthandler;} catch (E: Error) {/* flex 3.4 and eralier does not support the convertresulthandler functionality. */} _ servicecontrol. service = "service1"; _ servicecontrol. port = "service1soap"; WSDL = "http: // localhost: 3856/service1.asmx? WSDL "; model_internal: loadwsdlifnecessary (); model_internal: Initialize () ;}/ *** this method is a generated wrapper used to call the 'helloworld' operation. it returns an MX. RPC. asynctoken whose * result property will be populated with the result of the operation when the server response is already ed. * To use this result from mxml code, define a callresponder component and assign its token property to this method's return value. * You can then bind to callresponder. lastresult or listen for the callresponder. result or fault events. ** @ see MX. RPC. asynctoken * @ see MX. RPC. callresponder ** @ return an MX. RPC. asynctoken whose result property will be populated with the result of the operation when the server response is already ed. */Public Function helloworld (): MX. RPC. asynctoken {model_internal: loadwsdlifnecessary (); VaR _ internal_operation: MX. RPC. required actoperation = _ servicecontrol. getoperation ("helloworld"); VaR _ internal_token: MX. RPC. asynctoken = _ internal_operation.send (); Return _ internal_token;}/*** this method is a generated wrapper used to call the 'calc' operation. it returns an MX. RPC. asynctoken whose * result property will be populated with the result of the operation when the server response is already ed. * To use this result from mxml code, define a callresponder component and assign its token property to this method's return value. * You can then bind to callresponder. lastresult or listen for the callresponder. result or fault events. ** @ see MX. RPC. asynctoken * @ see MX. RPC. callresponder ** @ return an MX. RPC. asynctoken whose result property will be populated with the result of the operation when the server response is already ed. */Public Function calc (radius: Number): MX. RPC. asynctoken {model_internal: loadwsdlifnecessary (); VaR _ internal_operation: MX. RPC. required actoperation = _ servicecontrol. getoperation ("calc"); VaR _ internal_token: MX. RPC. asynctoken = _ internal_operation.send (RADIUS); Return _ internal_token ;}}}

Interface file:

Testwebserviceaspnet. mxml

<? XML version = "1.0" encoding = "UTF-8"?> <S: Application xmlns: FX = "http://ns.adobe.com/mxml/2009" xmlns: S = "Library: // ns.adobe.com/flex/spark" xmlns: MX = "Library: // ns.adobe.com/flex/mx "minwidth =" 955 "minheight =" 600 "width =" 797 "Height =" 512 "xmlns: service1 =" services. service1. * "> <s: layout> <s: basiclayout/> </S: layout> <FX: SCRIPT> <! [CDATA [import MX. controls. alert; protected function btngetresult_clickhandler (Event: mouseevent): void {calcresult. token = service1.calc (parsefloat (this.txt radius. text);} protected function button_clickhandler (Event: mouseevent): void {helloworldresult. token = service1.helloworld () ;}]> </FX: SCRIPT> <FX: declarations> <service1: service1 id = "service1" fault = "alert. show (event. fault. faultstring + '\ n' + event. f Ault. faultdetail) "showbusycursor =" true "/> <s: callresponder id =" calcresult "/> <s: callresponder id =" helloworldresult "/> <! -- Place non-visual elements (such as services and value objects) Here --> </FX: declarations> <s: panel x = "20" Y = "24" width = "331" Height = "203" Title = "calculate the area of a circle"> <s: button x = "69" Y = "99" label = "Calculate" id = "btngetresult" Click = "btngetresult_clickhandler (event)"/> <s: textinput x = "69" Y = "25" id = "txtradius"/> <s: Label x = "34" Y = "25" text = "radius: "/> <s: Label x =" 36 "Y =" 69 "text =" Result: "/> <s: label x = "71" Y = "69" id = "lblresult" text = "{calcresult. lastresult} "/> </S: Panel> <s: panel x = "375" Y = "24" width = "300" Height = "203" Title = "sayhello"> <s: button x = "117" Y = "102" label = "button" id = "button" Click = "button_clickhandler (event)"/> <s: label x = "94" Y = "63" text = "{helloworldresult. lastresult} "/> </S: Panel> </S: Application>
Related Article

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.