Can Csla Help with an Asynchronous HTTP Post

Can Csla Help with an Asynchronous HTTP Post

Old forum URL: forums.lhotka.net/forums/t/11069.aspx


Jav posted on Wednesday, January 18, 2012

I am trying to do an HTTP Post of string data from within SL.  I create an HttpWebRequest and then do:

request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);

In the GetRequestStreamCallback  method when I try to read the data that I need to send from a TextBox control using

string postData = txtData.Text;

I get an error:

System.UnauthorizedAccessException: Invalid cross-thread access

Is there a way for me to create a Csla class that I can use to accomplish all that.

 

RockfordLhotka replied on Wednesday, January 18, 2012

Probably yes.

You'll probably want to create a command object to do this.

This particular object will use the local data portal even if the rest of the app doesn't. In the factory method when you call the data portal make sure to use the force local parameter.

Then implement the DataPortal_Execute method in the class. This method must accept a callback parameter like all client-side DataPortal_XYZ methods in Silverlight. Inside the DataPortal_Execute method you must ensure that the callback is invoked when the operation is complete - regardless of error or success.

With all that done, your app can just invoke the static factory method on the command class. The factory method will invoke the data portal, forcing it to run in local mode. The data portal will invoke your DataPortal_Execute, and in that method you'll do your async web request call. When the web request call completes (error or success) you will invoke the callback parameter.

The callback parameter tells the data portal that the operation is complete. The data portal will invoke the callback provided by the static factory method (typically the ExecuteCompleted event handler). That is probably a callback provided by the original calling code, so you know that the call is complete.

In short - as a consumer of the command you are just interacting with a normal CSLA business object. Nothing special. Inside the command object you are just using the local data portal - perhaps not super-common, but not complex or unusual.

Jav replied on Thursday, January 19, 2012

Great.  Thanks Rocky. I will try that.

Jav

Copyright (c) Marimer LLC