Flex(Flash Builder) 消費 asp.net WebService

來源:互聯網
上載者:User

Service1.asmx.cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;namespace TestWebServiceForFlex{    /// <summary>    /// Service1 的摘要說明    /// </summary>    [WebService(Namespace = "http://tempuri.org/")]    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]    [System.ComponentModel.ToolboxItem(false)]    // 若要允許使用 ASP.NET AJAX 從指令碼中調用此 Web 服務,請取消對下行的注釋。    // [System.Web.Script.Services.ScriptService]    public class Service1 : System.Web.Services.WebService    {        [WebMethod]        public string HelloWorld()        {            return "Hello World,中國";        }        [WebMethod]        public double Calc(double radius)        {            double d= radius * radius * Math.PI;            return d;        }    }}

前台Flex調用:

_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.Operation;                 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.convertResultHandler;        }        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 received.   * 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 received.  */          public function HelloWorld() : mx.rpc.AsyncToken{model_internal::loadWSDLIfNecessary();var _internal_operation:mx.rpc.AbstractOperation = _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 received.   * 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 received.  */          public function Calc(radius:Number) : mx.rpc.AsyncToken{model_internal::loadWSDLIfNecessary();var _internal_operation:mx.rpc.AbstractOperation = _serviceControl.getOperation("Calc");var _internal_token:mx.rpc.AsyncToken = _internal_operation.send(radius) ;return _internal_token;}    }}

介面檔案:

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.txtRadius.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.fault.faultDetail)" showBusyCursor="true"/><s:CallResponder id="CalcResult"/><s:CallResponder id="HelloWorldResult"/><!-- 將非可視元素(例如服務、值對象)放在此處 --></fx:Declarations><s:Panel x="20" y="24" width="331" height="203" title="計算圓的面積"><s:Button x="69" y="99" label="計算" id="btnGetResult" click="btnGetResult_clickHandler(event)"/><s:TextInput x="69" y="25" id="txtRadius"/><s:Label x="34" y="25" text="半徑:"/><s:Label x="36" y="69" text="結果:"/><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="按鈕" id="button" click="button_clickHandler(event)"/><s:Label x="94" y="63" text="{HelloWorldResult.lastResult}"/></s:Panel></s:Application>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.