Using Import Web Service wizard to connect Flex to .NET
Since I was deeply involved in Flex Builder support for working with .NET I thought that it would be a good idea to write some articles on how you can use these technologies together. I will create a “Hello World” project but the focus will be on how you can better make use of the available tools.
For tooling I will be using Flex Builder and Visual Web Developer 2008 Express Edition (VWD).
First let’s create a simple ASP.NET Web Service project using VWD:
I slightly modified the HelloWorld method in Service.cs to receive a parameter:
[WebMethod]
public string HelloWorld(String name) {
return "Hello " + name + "! Nice talking to you!";
}
Now let’s get into Flex Builder and try to get something from the .NET Server. First we will create a new Flex Project on top of the .NET one. To do that click on New->Flex Project and choose ASP.NET as the server technology:
Make sure the location is the same as the VWD .NET project. Now run the default MXML file to start the ASP.NET Developer Server.
Next we will generate the glue code for calling the Web Service using the Import Web Service Wizard. Choose Data -> Import Web Service (WSDL) from the Flex Builder menu. On the first page the main source folder is selected by default. You can leave it like this. On the second page you need to choose the WSDL URI. When you launched the MXML application the ASP.NET Developer Server should have started by default on port 3000 (see the tray icon). In this case the WSDL URL should be something like http://localhost:3000/testDotNet/Service.asmx?WSDL.
The third page should look like this:
Now you should have some typed AS classes that provide easy access the .NET Web Service methods. The cool thing about this is that Flex Builder knows now how to do autocompletion on Web Service calls. So making a little application that will showcase this is just too easy:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import generated.webservices.HelloWorldResultEvent;
import generated.webservices.Service;
public function sayHello():void {
var service:Service = new Service();
service.addhelloWorldEventListener(
function (event:HelloWorldResultEvent):void {
Alert.show(event.result);
});
service.helloWorld(myName.text);
}
]]>
</mx:Script>
<mx:TextInput x="176" y="171" id="myName"/>
<mx:Text x="70" y="173" text="Enter Your Name"/>
<mx:Button x="344" y="171" label="Click to receive greeting" click="sayHello()"/>
</mx:Application>
Excellent! Thanks
Cool Title…really helped me…
Any chance of you creating an example of binding a flex datagrid the same way? Better yet, an editable datagrid?
Thanks for this example! You rock!
Sweet man, i couldnt figure out why my project didnt work thanks to this simple example i understood why =P
Thanks!
excellent example!!
I tried this and no alert box poped up so I added a “FaultEventListener” to see what the error was. This is what I saw:
[RPC Fault faultString="Security error accessing url" faultCode="Channel.Security.Error" faultDetail="Destination: DefaultHTTP"]
I have the .Net web service and my Flex application on my local machine. The web service uses Anonymous access for authentication.
Any ideas?
OK, I found the issue. The stubs created from the WSDL had the default URL set to “http:///….” instead of “http://localhost/…..”
Changing it to localhost solved my problem and I can get back the data from the web services.
This works fine on my development machine, but fails on production server.
Where i can change the web service url in flex bin.
i have placed the whole flex bin folder, in .net web application and placed the object tag on aspx page, swf is being loaed properly but generate fault everytime calling the web service.
could you give me some link where i can learn the deployment of this type of application?
Hello,
I am having a strange issue – when I try to import a web service locally, it finds the service, but does not show me any methods. When I import a service running on the dev server, I see the methods, but get an error: Provider com.bea.xml.stream.MXParserFactory not found.
Any suggestions? Thanks in advance.
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\DefaultWsdlHelpGenerator.aspx
Uri TryPostUrl {
get {
if (postUrl == null) {
if (HttpPostOperationBinding == null) return null;
Port port = FindPort(HttpPostOperationBinding.Binding);
if (port == null) return null;
HttpAddressBinding httpAddress = (HttpAddressBinding)port.Extensions.Find(typeof(HttpAddressBinding));
HttpOperationBinding httpOperation = (HttpOperationBinding)HttpPostOperationBinding.Extensions.Find(typeof(HttpOperationBinding));
if (httpAddress == null || httpOperation == null) return null;
postUrl = new Uri(httpAddress.Location + httpOperation.Location);
postUrl = new Uri(myAdress + httpOperation.Location);
}
return postUrl;
}
}
changing
> postUrl = new Uri(myAdress + httpOperation.Location);
to
string myAdress = httpAddress.Location;
myAdress = myAdress.Replace(“localhost”, “anydomain”);
postUrl = new Uri(myAdress + httpOperation.Location);
hi
this is my first flex application and i m trying to make a login form using wsdl. My wsdl return string called “Success” if the user name and password is correct.
I call the web service at the click of submit button
when i try to get the return value into a variable for eg:
var info:String = service.lastResult
i always get a null value at first. When i click for the second time, it returns the string.
I m not able to understand. Please help.
Thanks in advance,
Samruddhi
@samruddhi.
Hi Samruddhi,
I am also facing the same issue. Were you able to resolve the problem you were facing.
Thanks,
-H