FLEX与.NET通信可以采用WebService与RemoteObject等方式,这两种方式都要指定.NET的通信地址,但因为软件发布后可能其端口或软件所在文件夹都有变化,往往要回flashbuilder中重新编译,这样很麻烦,下面分开说下怎样动态确定通信地址。
1. WebService方式
这种方式不需要配置环境,只需在.NET端建立web服务文件,然后在FLEX中WebService的定义处将wsdl设置为服务文件的地址,如下:
<s:WebService id="textService" wsdl="http://localhost:3231/FlexCode/WebService.asmx?wsdl"> <s:operation name="HelloWorld" result="onResult(event)" fault="onFault(event)"></s:operation> <s:operation name="GetDataTable"></s:operation> <s:operation name="CodeContentList"> </s:operation> </s:WebService>
动态确定方式:
<s:WebService id="textService" wsdl="对应Flash页面的相对路径/WebService.asmx?wsdl">
2. RemoteObject方式
这种方式可以使用第三方通信组件FluorineFx,安装好FluorineFx,在visualstudio中建立FluorineFx模板的解决方案和网站,编写处理FLEX请求的类库文件,这里假设文件名为Sample.cs
FLEX端也不需要配环境,只需声明RemoteObject对象,指定destination,source,以及endpoint,其中endpoint就是对应的通信地址,Gateway.aspx是入口页面,不具备实际意义,可以在配置过FluorineFx的.net里的WEB-INF\flex文件夹下的services-config.xml中修改
<s:RemoteObject id="Service" destination="fluorine" source="FlexDotnet.Sample" endpoint="http://localhost:3023/Web/Gateway.aspx"> <s:method name="GetPro" result="onGetProResult(event)" fault="onFault(event)"></s:method> <s:method name="GetSample" result="onSampleResult(event)" fault="onFault(event)"></s:method> </s:RemoteObject>
动态确定方式:
思路:在flex程序初始化时获取当前页面的url,取出需要的部分(如“http://localhost:3023/Web/”)
假设flex程序的creationComplete方法为initApp(),RemoteObject的id如上为“Service”,代码如下
import mx.managers.BrowserManager; import mx.managers.IBrowserManager; private function initApp():void{ var browserManager:IBrowserManager = BrowserManager.getInstance(); browserManager.init(); var url:String = browserManager.url.substring(0,url.lastIndexOf('/')); Service.endpoint=url.substring(0,url.lastIndexOf('/'))+"/Gateway.aspx"; }
RemoteObject组件中就不用写endpoint了
如此就不必每次端口号或者网站名称改变的时候重新进行编译了