Silverlight using WebAPI with local DataPortal?

Silverlight using WebAPI with local DataPortal?

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


JonM posted on Wednesday, October 03, 2012

We've got an internal HTTP REST (using webapi) service.  I want to wrap that with a CSLA object in Silverlight so I get the fancy databinding and business rule tracking.  I've code running that can do the requests.  So my question is, Can I make silverlight csla use a "local" dataportal?  The local dataportal just makes webapi commands.  It is working great on my full .NET desktop app.

JonnyBee replied on Wednesday, October 03, 2012

Yes, you can do that in Silverlight (and WinRT).

Pre-Csla 4.5 you must specify ProxyModes.LocalOnly on the DataPortal call.

From Csla 4.5 you can use Task<T> and and the [RunLocal] attribute pretty much the same as in .NET.

JonM replied on Thursday, October 04, 2012

I've got the dataportal_fetch method to execute locally but now the callback won't fire.  It is like I'm not getting a FetchCompleted event.  I'm using 4.3.13.  Here is how I'm calling the local only portal.  Is there another/better way to do it? 

 

 

public static void GetMachineList(EventHandler<DataPortalResult<MachineList>> callback)

{

 

 

var dp = new DataPortal<MachineList>(DataPortal.ProxyModes.LocalOnly);

dp.FetchCompleted += callback;

 

 

dp.BeginFetch();

}

JonnyBee replied on Thursday, October 04, 2012

It is your responsibility to call the callback funtion from within the DataPortal_Fetch method after the async data access is completed.

Look at the SL samples for CSLA 4.3.13. 

In a .NET app you typically do sync DataAccess so the DataPortal will make the callback for you.
In a SL app the DataAccess is async so your DataAccess methods must accept a callback parameter and is responsible for making the callback.

Somewhat similar to this:

public void DataPortal_Fetch(object criteria Csla.DataPortalClient.LocalProxy<businessObject>.CompletedHandler handler)
{
   // Call webservice asynchronously,
   // and make sure to always invoke handler when completed or error!!
}

 

JonM replied on Friday, October 05, 2012

I can do that.  When doing SL apps in the past that called WCF dataportal I used the .FetchCompleted event and then raised by callback from there.  So the FetchCompleted apparently doesn't fire when you do a local running dataportal.  I also can't find anyway to raise it myself.  I can convert my dataportal calls to take a callback and do it there.  Thanks!

JonM replied on Friday, October 05, 2012

That worked!  I never did that on a remote dataportal with SL+WCF before.  Thanks again!

JonM replied on Friday, October 05, 2012

That worked for fetch and delete.  How do I get the insert and update to work?  I just call Save() on the Editable root object and I can errors about a missing ServiceReferences.ClientConfig file.  I had one of these in my last SL project talked to WCF.   Do I need to put something in here for a local dataportal or is that set somewhere else?

JonnyBee replied on Friday, October 05, 2012

Look for a Save/Delete method that accept ProxyMode as parameter.

JonM replied on Saturday, October 06, 2012

I don't see a BeginSave() overload that takes a proxy mode.  It looks like I can override the BeginSave in my business object class.  I could use the same type of code I'm using to call delete locally but install call update (I don't see an insert, so I don't know how that will work).  Is there another way or is the BeginSave override the way to go in 4.3?

JonnyBee replied on Sunday, October 07, 2012

Hmm,

Yes, it seems we have missed on that one - however you can add these overloads yourself to your baseclasses or business objects.

JonnyBee replied on Monday, October 08, 2012

If you do no want to mix remote and local proxy - ie: only run local proxy you only need to set: 

 Csla.DataPortal.ProxyTypeName == "Local";

and this will force SL DataPortal to only use local proxy mode.

JonM replied on Monday, October 08, 2012

That did it!  Thanks again for all of your help!

Jon

Copyright (c) Marimer LLC