<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>RANDOM SEED :: richard finn &#187; Coding</title>
	<atom:link href="http://www.random-seed.com/category/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.random-seed.com</link>
	<description>Random thoughts on programming, finance, and art.</description>
	<lastBuildDate>Sat, 28 Jan 2012 00:00:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Web Services in Flash and ActionScript 3</title>
		<link>http://www.random-seed.com/2009/07/24/web-services-in-flash-and-actionscript-3/</link>
		<comments>http://www.random-seed.com/2009/07/24/web-services-in-flash-and-actionscript-3/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 22:21:02 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[error]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[web services]]></category>
		<category><![CDATA[wsdl]]></category>

		<guid isPermaLink="false">http://www.random-seed.com/?p=8</guid>
		<description><![CDATA[For some reason Adobe removed web services from ActionScript 3 in Flash.  Here are some solutions to get around this brain-dead problem.]]></description>
			<content:encoded><![CDATA[<p>In some ways, I understand, but in most I do not.  For some reason Adobe removed web services from ActionScript 3 in Flash.  The web services classes we all used in AS 2 with abandon for no longer exist in the Flash API.  Wow.  This post offers up my research on getting around this issue.</p>
<p><span id="more-8"></span><br />
Time was you could import these classes:</p>
<pre>import mx.services.WebService;
import mx.services.SOAPCall;
import mx.services.PendingCall;</pre>
<p>And you still can, if you change your ActionScript version to 2.0 in the <em>Publish Settings</em> for your project.  However, if you&#8217;re like me and your project relies on new components only available in ActionScript 3, then that is not an option.  You&#8217;ll get an error, &#8220;1172: Definition mx.services:WebService could not be found.&#8221;  You may also be able to get past that and still get &#8220;1046: Type was not found or was not a compile-time constant: WebService.&#8221;  These classes are avialble to you if you&#8217;re developing in Flex or AIR with AS3.  Apparently, Adobe only wants you creating rich internet applications with Flex or AIR.  Of course, this also means that you cannot take any Flash app which uses these classes and upgrade them to AS3.</p>
<p>When I finally discovered this cause of this problem (BTW: I have only found it mentioned on other blogs, not from Adobe) I have to admit I paniced for a minute.  Then, I got creative&#8230;  I thought about moving the forms to individual Flex projects and embedding the resulting SWFs, I thought about calling out HTML popup windows, and I thought about trying to redevelop the application in Flex &#8211; big problem with that being that the Flash developer had already created a beautiful interface full of graphics.</p>
<p>Then, I disovered two workarounds which do not require opening up my Flex Builder.  One option is to include a custom component specifically developed to get around this problem.  You can find it <a href="http://www.communitymx.com/content/article.cfm?cid=2266C" target="_blank">here</a>.  This method is particular useful if you were used to dropping in the <em>WebServiceConnector</em> from the <em>Componets</em> panel.</p>
<p>The other method, and the one I chose because I prefer to use ActionScript, is to make use of the URLRequest, URLLoader, and URLVariables classes available in AS3.  I grabbed the open source <a href="http://code.google.com/p/as3corelib/downloads/list" target="_blank">AS3 CoreLib off of Google Code</a> and used its JSON serialization method.  I did end up dropping the strict WSDL, but you could still use one if you needed to.  The <a href="http://www.cakesgood.com/2009/04/flash-cs3-actionscript-30-json-keep-it_3277.html">tutorials</a> I found on this method centered on retrieving data from a web service, but I was submitting information with a form.</p>
<p>Here&#8217;s my JSONService class I created to help:</p>
<pre>package {
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.net.URLVariables;
	import flash.net.URLRequestMethod;

	/* JSON
   	used for communicating data with PHP tier
   	grab AS3 CoreLib from http://code.google.com/p/as3corelib/downloads/list
   	and make sure the "src" folder from the extracted zip is in your class path
	*/
	import com.adobe.serialization.json.JSON;

	public class JSONService {
		private var serviceAddress	:String;

		public function JSONService ( address:String ) {
			serviceAddress = address;
		}

		public function sendRequest (func:String, container:String, packet:Object) {
			/* encode and prepare the data for transfer */
			var jsonString:String		= JSON.encode(packet);
			var urlVars:URLVariables	= new URLVariables();
			urlVars[container]		= jsonString;

			var urlReq:URLRequest		= new URLRequest(serviceAddress + "?request=" + func);
			urlReq.method 			= URLRequestMethod.POST;
			urlReq.data			= urlVars;

			/* make the actual call */
			var urlLoader:URLLoader		= new URLLoader();
			urlLoader.addEventListener(Event.COMPLETE, this.sendCompleteEvent);
			urlLoader.load(urlReq);
		}

		public function sendCompleteEvent(evt:Event) {
			// trace(evt.target.data);
			if(evt.target.data != 1) {
				/* error reported by backend */
				var error:Object = JSON.decode(evt.target.data) as Object;
				trace(error.message);
			} else {
				/* send successful */
				trace('success');
			}
		}
	}
}</pre>
<p>When I use this class, I call it with:</p>
<pre>service = new JSONService(config.getLoadVar("wsdl"));

service.sendRequest("add_contact", "contact", contactObject);</pre>
<p>Which reminds me, they also got rid of <em>LoadVars</em>.  I combined the information in <a href="http://www.peterelst.com/blog/2007/11/28/actionscript-30-wheres-my-loadvars/" target="_blank">this post</a> and with my JSONService class to create a configData singleton.  At any rate, <em>contactObject </em>contains all the properties I am sending to the &#8220;add_contact&#8221; method in my RESTful controller (a PHP file in this case).  The contents of c<em>ontactObject</em> are JSON encoded and placed in the <em>contact</em> container URL variable as part of the request.</p>
<p>Behind the scenes, a PHP MVC (model-view-controller) application stores the data.  The PHP file which handles the request looks like:</p>
<pre>$request = @$_GET['request'];

switch ( $request ) {
    // lots of other request handlers...
    // handler for add_contact
    case 'add_contact':
        if ( !isset( $_POST['contact'] ) ) {
            echo jsonFault( -1000, 'Improperly Formatted Request' );
        } else {
            $response = $controller-&gt;addContact($_POST['contact']);
            if ( $response == 1) {
                echo json_encode( 1 );
            } else {
                echo jsonFault( -2000, model::getErrorMessage( $response ) );
            }
        }
        break;
}

function jsonFault( $code, $message ) {
    $fault = new stdClass();
    $fault-&gt;isFault = true;
    $fault-&gt;code = $code;
    $fault-&gt;message = $message;
    return json_encode( $fault );
}</pre>
<p>The <em>$controller</em> object decodes the JSON packets and performs some action, such as validation and handing them off to the <em>model</em> class for storage.</p>
<p>I should also note that I have since found <a href="http://alducente.wordpress.com/2007/10/27/web-service-in-as3-release-10/" target="_blank">this article</a>, which provides a nice AS3 object to download and use.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.random-seed.com/2009/07/24/web-services-in-flash-and-actionscript-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.Net User Control &#8216;does not exist in the current context&#8217;</title>
		<link>http://www.random-seed.com/2009/07/15/asp-net-user-control-field-does-not-exist-in-the-current-context/</link>
		<comments>http://www.random-seed.com/2009/07/15/asp-net-user-control-field-does-not-exist-in-the-current-context/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 22:27:59 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[ASP]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[error]]></category>

		<guid isPermaLink="false">http://www.random-seed.com/?p=34</guid>
		<description><![CDATA[I may be missing something, but a search around the Internet revealed several people having the same problem and not finding any good answers.  You have an ASP.Net user control with fields on it, but when you compile you get an error.  Intellisence knows the objects are there, but the compiler doesn&#8217;t.  What do you [...]]]></description>
			<content:encoded><![CDATA[<p>I may be missing something, but a search around the Internet revealed several people having the same problem and not finding any good answers.  You have an ASP.Net user control with fields on it, but when you compile you get an error.  Intellisence knows the objects are there, but the compiler doesn&#8217;t.  What do you do?</p>
<p><span id="more-34"></span>Like I said, I might be wrong &#8211; but after a lot of trial and error, and error, and error I settled upon this fix.  The error comes from the fact that the <em>.ascx.cs</em> file is compiled <em><strong>before</strong></em> the <em>.ascx</em> page.  So, if could could compile them separately it will actually work (I know, I managed to accomplish this by accident).</p>
<p>What does not work is to declare the form controls as protected variables in the code behind.  If you set them in place in the <em>.ascx</em> file, then they have already been declared and initialized.  However, if you did not place them in <em>.ascx</em> page, you could add them programatically at run time from the code behind and that would work &#8211; it&#8217;s not pretty, and to be avoided if you can, but it will work (assuming you know when you&#8217;re developing this control that the layout of the form controls is static, if its dynamic then please add them programatically).  I also tried data binding, which is likely the official solution &#8211; but I ran into problems along that route as well.</p>
<p>If you&#8217;re altering an <em>.aspx</em> page, make sure to change to <em>&lt;%@ Page</em> directive to a <em>&lt;%@ Control</em> directive, such as:</p>
<pre>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeBehind="myControl.ascx.cs" Inheirits="myControl" %&gt;</pre>
<p>What I then did was to add string fields to hold my values as protected members in the code behind:</p>
<pre>#region form strings
protected String firstName = "";
protected String lastName = "";
protected String phoneNumber = "";
protected String emailAddress = "";
#endregion</pre>
<p>The function that did pull the contents from the form variables, formerly called <em>btnSubmit_Click()</em>,  instead uses the holder variables and is now called <em>processForm()</em>.  The submit event handler is then created in the .ascx file directly:</p>
<pre>&lt;script language="cs" runat="server"&gt;
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        this.firstName = txtfName.Text;
        this.lastName = txtLname.Text;
        this.phoneNumber = txtPhone.Text;
        this.emailAddress = txtEmail.Text;

        this.processForm();
    }
&lt;/script&gt;</pre>
<p>There you have it.  Maybe not the most elegant, but when you&#8217;re hitting your head on the wall, this will save you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.random-seed.com/2009/07/15/asp-net-user-control-field-does-not-exist-in-the-current-context/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

