locked
ADO.Net Web Service from Silverlight RRS feed

  • Question

  • I have built an ADO.NET Web Service using the Entity Framework. The service works fine in IE browser and I can access the service from a Forms project using this code:-

    LiveTrackerData.TrackingEntities proxy = new TrackingEntities( new Uri("http://localhost:1111/LIveTrackerData.svc"));

    DataServiceQuery query = proxy.CreateQuery("Customer") ;

    query.BeginExecute( delegate(IAsyncResult ar) { foreach (Customer c in query) { Console.WriteLine(c.CustomerName); } }, null);


    This enumerates the customers as expected.
    I added the Customer class by adding a service reference.
    I reference System.Data.Services.Client.

    If I now begin a Silverlight Client using an automatically generated web app to host it, I run the same code from a button click event on the app. This dies with:-

    Failed to Invoke: callOpen.

    Again I reference the System.Data,Services.Client. The tutorial from which I copied this code references Microsoft.Data.WebClient but I gather this has been superseded in Framework 3.5 sp1. Any idea what the problem could be?

    The full error text is:-

    "System.NotSupportedException: Specified method is not supported.\r\n   at System.Data.Services.Client.DataServiceQuery`1.System.Collections.Generic.IEnumerable<TElement>.GetEnumerator()\r\n   at LiveTracker.Page.<>c__DisplayClass2.<LoadButton_Click>b__0(IAsyncResult ar)\r\n   at System.Data.Services.Client.BaseAsyncResult.HandleCompleted()\r\n   at System.Data.Services.Client.QueryAsyncResult.BeginExecute()\r\n   at System.Data.Services.Client.DataServiceRequest.BeginExecute(Object source, DataServiceContext context, AsyncCallback callback, Object state)\r\n   at System.Data.Services.Client.DataServiceQuery`1.BeginExecute(AsyncCallback callback, Object state)\r\n   at LiveTracker.Page.LoadButton_Click(Object sender, RoutedEventArgs e)"

    Bob
    bob
    Tuesday, July 21, 2009 2:26 PM

Answers

  • The code in the async query execution is incorrect:

    query.BeginExecute( delegate(IAsyncResult ar) { foreach (Customer c in query) { Console.WriteLine(c.CustomerName); } }, null);


    should be :

    query.BeginExecute( (ar)=>{
              foreach (Customer c in query.EndExecute(ar)) 
             {
                Console.WriteLine(c.CustomerName);
            }
         } , null);

    You need to call EndExecute on the Query to be able to consume the results.


    Phani Raj Astoria http://blogs.msdn.com/PhaniRaj
    Tuesday, July 21, 2009 4:45 PM
    Moderator
  • Hello,

    The code above is incomplete, in Silverlight you need to use the Dispatcher class in the callback to be able to return to the UI thread:

    query.BeginExecute( (ar)=>

    {
     Deployment.Current.Dispatcher.BeginInvoke(() =>

    {

         var result = query.EndExecute(ar) ;

    foreach ...)

    });

    });

    Hope this helps!

    Monica

     

     

     


    Monica Frintu
    Wednesday, April 20, 2011 7:34 PM
    Moderator

All replies

  • The code in the async query execution is incorrect:

    query.BeginExecute( delegate(IAsyncResult ar) { foreach (Customer c in query) { Console.WriteLine(c.CustomerName); } }, null);


    should be :

    query.BeginExecute( (ar)=>{
              foreach (Customer c in query.EndExecute(ar)) 
             {
                Console.WriteLine(c.CustomerName);
            }
         } , null);

    You need to call EndExecute on the Query to be able to consume the results.


    Phani Raj Astoria http://blogs.msdn.com/PhaniRaj
    Tuesday, July 21, 2009 4:45 PM
    Moderator
  • Thanks for the reply. I've tried your code but it still bombs with the error:-

      s "System.InvalidOperationException: An error occurred while saving changes. See the inner exception for details. ---> System.InvalidOperationException: Failed to Invoke: callOpen.\r\n   at System.Windows.Browser.ScriptObject.Invoke(String name, Object[] args)\r\n   at System.Data.Services.Http.ScriptObjectUtility.CallOpen(ScriptObject request, String method, String uri)\r\n   at System.Data.Services.Http.ScriptXmlHttpRequest.Open(String uri, String method, Action readyStateChangeCallback)\r\n   at System.Data.Services.Http.HttpWebRequest.InvokeRequest()\r\n   at System.Data.Services.Http.HttpWebRequest.BeginGetResponse(AsyncCallback callback, Object state)\r\n   at System.Data.Services.Client.QueryAsyncResult.BeginExecute()\r\n   at System.Data.Services.Client.DataServiceRequest.BeginExecute(Object source, DataServiceContext context, AsyncCallback callback, Object state)\r\n   at System.Data.Services.Client.DataServiceQuery`1.BeginExecute(AsyncCallback callback, Object state)\r\n   at LiveTracker.Page.LoadButton_Click(Object sender, RoutedEventArgs e)\r\n   --- End of inner exception stack trace ---\r\n   at System.Data.Services.Client.BaseAsyncResult.EndExecute[T](Object source, String method, IAsyncResult asyncResult)\r\n   at System.Data.Services.Client.QueryAsyncResult.EndExecute[TElement](Object source, IAsyncResult asyncResult)\r\n   at System.Data.Services.Client.DataServiceQuery`1.EndExecute(IAsyncResult asyncResult)\r\n   at LiveTracker.Page.<>c__DisplayClass2.<LoadButton_Click>b__0(IAsyncResult ar)\r\n   at System.Data.Services.Client.BaseAsyncResult.HandleCompleted()\r\n   at System.Data.Services.Client.QueryAsyncResult.BeginExecute()\r\n   at System.Data.Services.Client.DataServiceRequest.BeginExecute(Object source, DataServiceContext context, AsyncCallback callback, Object state)\r\n   at System.Data.Services.Client.DataServiceQuery`1.BeginExecute(AsyncCallback callback, Object state)\r\n   at LiveTracker.Page.LoadButton_Click(Object sender, RoutedEventArgs e)" string

    Regards
    Bob
    bob
    Wednesday, July 22, 2009 6:04 AM
  • The code in the async query execution is incorrect:

    query.BeginExecute( delegate(IAsyncResult ar) { foreach (Customer c in query) { Console.WriteLine(c.CustomerName); } }, null);


    should be :

    query.BeginExecute( (ar)=>{
    foreach (Customer c in query.EndExecute(ar))
    {
    Console.WriteLine(c.CustomerName);
    }
    } , null);

    You need to call EndExecute on the Query to be able to consume the results.


    Phani Raj Astoriahttp://blogs.msdn.com/PhaniRaj

    I'm a beginner, Your info is very helpful to my study.
    Friday, January 14, 2011 2:06 AM
  • Hello,

    The code above is incomplete, in Silverlight you need to use the Dispatcher class in the callback to be able to return to the UI thread:

    query.BeginExecute( (ar)=>

    {
     Deployment.Current.Dispatcher.BeginInvoke(() =>

    {

         var result = query.EndExecute(ar) ;

    foreach ...)

    });

    });

    Hope this helps!

    Monica

     

     

     


    Monica Frintu
    Wednesday, April 20, 2011 7:34 PM
    Moderator