Solved

WCF OnCompleted Event not firing

Posted on 2010-08-14
5
1,020 Views
Last Modified: 2013-11-12
I have a class in a SL4 app that consumes the WCF data.

My problem is, that when I call this class from the Page code, the OnCompleted Event isn't firing before the collection is returned, so the returned collection is nul.

I'm using the code below to perform this operation.
PAGE CODE-------------------

ElecDayData = new GetData().GetDailyData ("12", sDate, "Electricity", "D");
ElecPrevDayData = new GetData().GetDailyData("12", sDate2, "Electricity", "D");



CLASS-------------------------

public ObservableCollection<DailyData> GetDailyData(string MeterInstance, string sDate, string Utility, string report)
        {

            Uri address = new Uri(Application.Current.Host.Source, "../Services/SilverlightService.svc");
            dbAccess = new SilverlightServiceClient("CustomBinding_SilverlightService", address.AbsoluteUri);
            dbAccess.getDailyDataCompleted += new EventHandler<getDailyDataCompletedEventArgs>(dbAccess_getDailyDataCompleted);
            dbAccess.getDailyDataAsync(sDate, MeterInstance, 0, false, false);
            return dailyData;
        }

        void dbAccess_getDailyDataCompleted(object sender, getDailyDataCompletedEventArgs e)
        {
            dailyData = new ObservableCollection<DailyData>();
            dailyData = e.Result;
        }

Open in new window

0
Comment
Question by:wint100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 25

Expert Comment

by:apeter
ID: 33439857
1. You can't return "dailyData" from ur method GetDailyData since it is an Async Call. Also enclose inside a try catch to catch any exception

2. was there any exception in the service call ? did you check in the service error log ?
0
 
LVL 1

Author Comment

by:wint100
ID: 33440919
Hi,

There weren't any errors thrown, if I move everything into the page code, it all works.

I'm trying to keep things seperate though, so having a Data Access Layer for the service calls.
0
 
LVL 25

Expert Comment

by:apeter
ID: 33447710
But why are the parameters for the method are different from page code and Async Call ?
0
 
LVL 5

Accepted Solution

by:
berXpert earned 500 total points
ID: 33447862
Hello,

Your collection is empty because you are assigning it before the service asynch call and it is never assigned again.

What you need to do is to pass a function call back from your page code to the database service object, this way you can attach the asynch on complete event to the call back. When the event is fired it will execute the code in your page, instead of the void dbAccess_getDailyDataCompleted that is called in the database service.

See the modified code.

I hope this helps you.
PAGE CODE-------------------

	public ObservableCollection<DailyData> ElecDayData;

	...

        private void GetElecDayData()
        {
        	var dataService = new GetData().GetDailyData ("12", sDate, "Electricity", "D", OnDataRetrievedForElecDay);
        }



        void OnDataRetrievedForElecDay(ObservableCollection<DailyData> dataReturned)
        {
            ElecDayData.Clear();

            //Insert into collection to ensure property change is fired. 
            foreach( var item in dataReturned)
            {
            	ElecDayData.Add(item);
            }

            //Or You can try without the loop just assing ElecDayData = dataReturned;    sometimes it does not fire property change for me so I usually use the foreach loop
        }




CLASS-------------------------

public void GetDailyData(string MeterInstance, string sDate, string Utility, string report, Action<ObservableCollection<DailyData>> OnGetDataCompleteCallback )
        {

            Uri address = new Uri(Application.Current.Host.Source, "../Services/SilverlightService.svc");
            dbAccess = new SilverlightServiceClient("CustomBinding_SilverlightService", address.AbsoluteUri);

            // Link the asynchronous response to the caller, passing the results as arguments to the caller
            dbAccess.getDailyDataCompleted += (sender, e) => { OnGetDataCompleteCallback( e.Result ); } ;

            // Make the asynchronous call
            dbAccess.getDailyDataAsync(sDate, MeterInstance, 0, false, false);
        }

Open in new window

0
 
LVL 1

Author Comment

by:wint100
ID: 33548283
I'm getting this error using he code above:


Error      70      Cannot assign void to an implicitly-typed local variable
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Query performance - SQL Server 20 85
Visual Studio Debugging 3 151
Messagebox for a Web Site Application 4 81
"Video DataSource is Offline or not responding"  - why? 1 113
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question