Flex(Flash Builder)消費asp.net WebService

7306 ワード

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 + '
' + 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>