Consume a WCF Service with Adobe ActionScript® 3

The challenge:

Create a WCF Service with Basic Authentication.
Consume the service with Adobe ActionScript® 3 (FLEX)

The outcome:


How I did it:
IIS Settings
IIS Setting

WCF code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.
public class Service : IService
	public string GetData(int value)
		return string.Format("You entered: {0}", value);

	public CompositeType GetDataUsingDataContract(CompositeType composite)
		if (composite == null)
			throw new ArgumentNullException("composite");
		if (composite.BoolValue)
			composite.StringValue += "Suffix";
		return composite;

WCF web.config

        <binding name="BasicHttpEndpointBinding">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Basic" />
      <service behaviorConfiguration="ServiceBehavior" name="Service">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpEndpointBinding"
          name="B" contract="IService">
            <dns value="localhost" />
        <!--<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />-->
        <behavior name="ServiceBehavior">
          <!-- To avoid disclosing metadata information, set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false" />

Adobe ActionScript® 3 code

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="" layout="absolute" applicationComplete="init()">
<mx:Label id="myLabel"/>
        import mx.utils.*;
        public function init():void
        private function URLRequestExample():void
            // Encode username and password
            var encoder:Base64Encoder = new Base64Encoder();        
            // Initialize headers
            var authorizationHeader:URLRequestHeader = new URLRequestHeader("Authorization", "Basic " + encoder.toString());
            var soapActionHeader:URLRequestHeader = new URLRequestHeader("SOAPAction","");
            // Initialze request
            var request:URLRequest = new URLRequest("http://localhost:8000/WCFServiceBasicHttp/Service.svc");
            request.contentType =  "text/xml;charset=UTF-8";
            request.method = URLRequestMethod.POST;
            // This is the actual soap envolope that will be send to the WCF service, generated with SOAPUI
   = '<soapenv:Envelope xmlns:soapenv="" xmlns:tem=""><soapenv:Header/><soapenv:Body><tem:GetData><tem:value>3</tem:value></tem:GetData></soapenv:Body></soapenv:Envelope>';
            // Initalize the URLLoader
            var loader:URLLoader = new URLLoader();
                // Send out the service call
                trace("Request sent");
            catch (error:Error)
                trace("Unable to load requested document.");
        private function configureListeners(dispatcher:IEventDispatcher):void
            dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
        private function completeHandler(event:Event):void
            var loader:URLLoader = URLLoader(;
            trace("completeHandler: " +;
            // Output response to myLabel
            myLabel.text =;
        private function openHandler(event:Event):void
            trace("openHandler: " + event);
        private function progressHandler(event:ProgressEvent):void
            trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
        private function securityErrorHandler(event:SecurityErrorEvent):void
            trace("securityErrorHandler: " + event);
        private function httpStatusHandler(event:HTTPStatusEvent):void
            trace("httpStatusHandler: " + event);
        private function ioErrorHandler(event:IOErrorEvent):void
            trace("ioErrorHandler: " + event);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s