Flex(Flash Builder) 消费 asp.net WebService

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>

你可能感兴趣的:(webservice)