Spring.net + FluorineFx 项目搭建 之 三、编写Flex接口代码

三、编写Flex的Remoting调用接口代码

3.1 编写默认Sample类的内容

当建立“FluorineFx ServiceLibrary”类库时,默认会生成一个Sample类,在此类添加方法,则就可以让Flex端调用。


3.1.1 首先得到Service对象

在Sample的构造方法中获取对象的引用。

public Sample()
{
    IApplicationContext context = WebApplicationContext.GetRootContext();
    this.userService = context.GetObject("userService") as IUserService;
}

  

3.1.2 通过调用Service方法然实现功能

public Boolean Logon(string userName, string password)
{
       return userService.Logon(userName, password);
}

  

3.2 自定义Flex端Remoting调用类

默认情况下此类是自动生成的Sample类,也可以自定义,这样可以使用Spring框架在XML中初始化需要用到的服务对象。

首先,定义三个类,①给前台Flex放接口的类FlexService;②继承IFlexFactory接口的类SpringFlexFactory;③继承FactoryInstance类的类SpringFactoryInstance;

3.2.1FlexService类写法:

using System;
using System.Collections.Generic;
using System.Text;
using FluorineFx;
using Li.Interfaces;

namespace Li.FlexService
{
    [RemotingService]
    public class FlexService
    {
        private IUserService UserService;
        public FlexService(IUserService UserService)
        {
            this.UserService = UserService;
        }
     
        /// <summary>
        /// 用户登陆
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Boolean Logon(string userName, string password)
        {
            return this.UserService.Logon(userName, password);
        }
    }
}

 

 

3.2.2SpringFlexFactory类写法:

 

using System;
using System.Collections.Generic;
using System.Text;
using FluorineFx.Messaging;

namespace Li.FlexService
{
    class SpringFlexFactory : IFlexFactory
    {
        #region IFlexFactory 成员
        public FactoryInstance CreateFactoryInstance(string id, System.Collections.Hashtable properties)
        {
            return new SpringFactoryInstance(this, id, properties);
        }

        public object Lookup(FactoryInstance factoryInstance)
        {
            return (factoryInstance as SpringFactoryInstance).Lookup();
        }

        #endregion
    }
}

 


3.2.3SpringFactoryInstance类的写法:

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using FluorineFx.Messaging;
using System.Collections;
using Spring.Context;
using Spring.Context.Support;

namespace Li.FlexService
{
    class SpringFactoryInstance : FactoryInstance
    {
        public SpringFactoryInstance(IFlexFactory factory, string id, Hashtable properties)
            : base(factory, id, properties)
        {
            
        }

        public override object Lookup()
        {
            IApplicationContext context = WebApplicationContext.GetRootContext();
            return context.GetObject(context.GetObjectNamesForType(typeof(FlexService)).FirstOrDefault());
        }
    }
}

 

 

3.3.4需要更改三个配置文件:

①在“FluorineFx ASP.NET Web Site”站点下“WEB-INF\flex\ services-config.xml”配置文件中,根services-config下添加:

 

<factories>
      <factory id="springFlexFactory" class="Li.FlexService.SpringFlexFactory" />
</factories>

 


②在“FluorineFx ASP.NET Web Site”站点下“WEB-INF\flex\remoting-config.xml”配置文件中,在destination下的properties 添加一个标签 <factory>springFlexFactory</factory>

<destination id="fluorine"> 
    <properties>
        <factory>springFlexFactory</factory>
        <source>*</source>
    </properties> ... 
</desination>

  


③在web.config配置文件中,实例化FlexService类,在spring下的objects添加一个object标签

<object type="Li.FlexService.FlexService, Li.FluorineFx">
        <constructor-arg ref="userService" />
</object>

 

 

3.3  Flex端调用C#接口

1、首先将“FluorineFx ASP.NET Web Site”网站站点,配置到IIS上。在IIS上新建虚拟目录。
2、在Flex端使用RemoteObject标签进行后台调用,
3、RemoteObject标签中的属性配置为:

①destination内容为:“FluorineFx ASP.NET Web Site”站点下“WEB-INF\flex\remoting-config.xml”配置文件下,destination标签的id内容;
②endpoint内容为:“FluorineFx ASP.NET Web Site”站点下“Gateway.aspx”页面的IIS地址。
③Source内容为:  “FluorineFx ServiceLibrary”类库下,编写Flex接口代码的类的包路径(默认为Sample类,自定义时按情况)。


3、 RemoteObject标签中需要添加的内容:

①fault事件:调用后台失败时激发的事件,进行调用失败处理;
②showBusyCursor="true":当调用后台未返回时,鼠标成加载状(钟表转动动画);
③method标签:name为要调用后台的方法名,result为调用此方法成功后激发的事件。


4、 需要调用时形如:调用remoteObject.UserLogin(参数1, 参数2);

<mx:RemoteObject id="remoteObject" destination="fluorine" endpoint="http://192.1.1.113/liWebService/Gateway.aspx"
     source=" Li.FlexService.FlexService" fault="remoteFaultHandler(event)" showBusyCursor="true">
     <mx:method name="UserLogin" result="UserLoginResultHandler(event)" />
</mx:RemoteObject>

  

 

登录功能简单代码:

 

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
				layout="absolute" minWidth="1003" minHeight="600" fontSize="12">
	<mx:Script>
		<![CDATA[
			import mx.events.CloseEvent;
			import mx.events.FlexEvent;
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			
			import mx.controls.Alert;
			
			/**
			 * 登陆按钮,事件处理
			 */
			private function buttonLogon_clickHandler(event:MouseEvent):void
			{
				this.generalRemoteObject.Logon( this.textInputUserName.text, this.textInputPassword.text );
			}
			
			private function logonResultHandler( event:ResultEvent ):void
			{
				if( event.result )
				{
					Alert.show("登陆成功");
				}
				else
				{
					Alert.show("登陆失败,用户名或密码错误");
				}
			}
			
			public function remoteFaultHandler( event:FaultEvent ):void
			{
				Alert.show("remote后台请求错误");
			}
			

		]]>
	</mx:Script>
	
	<mx:RemoteObject id="generalRemoteObject" destination="fluorine" showBusyCursor="true"
					 endpoint="http://192.1.1.113/liWebService/Gateway.aspx" 
					 source="Li.FlexService.FlexService" >
		
		<mx:method name="Logon" result="logonResultHandler(event)"/>
		
	</mx:RemoteObject>
	
	
		<mx:HBox horizontalCenter="0" verticalCenter="90" width="400"  verticalAlign="middle">
					
			<mx:Form >
				
				<mx:FormItem label="用户名:">
					<mx:TextInput id="textInputUserName" width="150"   />
				</mx:FormItem>
				
				<mx:FormItem label="密码:">
					<mx:TextInput id="textInputPassword" width="150" displayAsPassword="true"  />
				</mx:FormItem>
				
			</mx:Form>
			
			<mx:Button label="登陆" click="buttonLogon_clickHandler(event)" />
		</mx:HBox>	
			
</mx:Application>

 

你可能感兴趣的:(spring,.net,Flex,asp.net,asp)