WCF RIA Services - Loading multiple entities - c#

I'm looking for a pattern to solve the following problem, which I imagine is common.
I am using WCF RIA Services to return multiple entities to the client, on initial load. I want both entities to load asyncrhonously, so as not to lock the UI, and I'd like to leverage RIA Services to do this.
My solution, below, seems to work. Will I run into problems/limitations with this approach? Is there a better pattern for this?
Thanks!
//create proxy to Domain Service
var proxy = new RIAService.Web.DomainContext();
//call service; fire event when Presentation entities have been returned
var loadPresentations = proxy.Load(proxy.GetPresentationsQuery());
loadPresentations.Completed += new EventHandler(loadPresentations_Completed);
//call service; fire event when Topics entities have been returned
var loadTopics = proxy.Load(proxy.GetTopicsQuery());
loadTopics.Completed += new EventHandler(loadTopics_Completed);
void loadTopics_Completed(object sender, EventArgs e)
{
//bind topic entities to XAML
}
void loadPresentations_Completed(object sender, EventArgs e)
{
//bind presentation entities to XAML
}

Your solution should work as is. There is one little catch in your code - you are calling the async method on server, and after that you are binding the OnCompleted event. If the call is superfast and ends before the event is bound, you won't see the entities.
In my experience this has never been a problem (in 99.99% cases it works fine), but just to have clean code, you can provide the callback inside the Load method, like
proxy.Load(proxy.GetPresentationsQuery(), op => { here work with op.Value });
Hint: In order to load entities into ObservableCollection, I created custom class deriving from ObservableCollection, which takes DomainContext and DomainQuery as parameters in ctor and is able to load the items from server itself. In addition it is possible to bind the collection in XAML and loaded entities are automatically updated in GUI.

Not brilliant solution - but works.
Load all operation in sequential order. Next load start when previous load event is completed.
MyDomainContext proxy;
public void Initialize()
{
//create proxy to Domain Service
proxy = new RIAService.Web.DomainContext();
//load Presentation - LOAD STEP 1
Load(proxy.GetPresentationsQuery(), LoadPresentations_Completed, null);
}
void LoadPresentations_Completed(LoadOperation<PresentationEntity> loadOp)
{
if (loadOp.HasError)
{
//process error here
loadOp.MarkErrorAsHandled = true;
}
else
{
- LOAD STEP 2
var loadTopics = proxy.Load(proxy.GetTopicsQuery());
loadTopics.Completed += new EventHandler(loadTopics_Completed);
}
}
void loadTopics_Completed(object sender, EventArgs e)
{
//bind presentation entities to XAML
}
Good luck.

This is the same pattern I have been using on a Silverlight app that has been in production since June. It seems to work well for me. In particular, it takes advantage of multi-threaded back end servers quite well since each entity request will execute in parallel on the server.
Rlodina suggested loading the entities sequentially which also works. I avoided this in favor of the performance boost of parallel operations. But, there was a case when I was forced to use the sequential operations. That case was when the second query needed to be constrained by the results of the first.
So, to be blunt in answering your question, this pattern worked well for me. It is functional and simple. I always advocate doing the simplest thing that could possibly work.

Related

How to fix 'The current thread is not associated with the renderer's synchronization context'?

I am trying to change a string, being used for a title, in my blazor-server-side application. But I am having trouble getting the UI to update.
I tried using StateHasChanged(), but that didn't work so I looked around and found that on the FlightFinder Demo that was made, it has an OnChange event Action, so I am trying to implement that.
It works until I try to refresh the browser, then I am hit with this error
System.InvalidOperationException: 'The current thread is not associated with the renderer's synchronization context. Use Invoke() or InvokeAsync() to switch execution to the renderer's synchronization context when triggering rendering or modifying any state accessed during rendering.'
This is what I have:
private string _title = "TestSite";
public string Title => _title;
public event Action OnChange;
public void ChangePage(string pageName)
{
_title = pageName;
NotifyStateChanged();
}
private void NotifyStateChanged(int navigationType = 0)
{
OnChange?.Invoke();
}
All I have to do is call ChangePage("some Page Title") and it works, unless as I mentioned I try to refresh.
I am just trying to change a string on one component that through another component, which doesn't sound all that crazy. If there is a better way to do titles or change things from other components, I would love to hear about it.
So, what can I do to make sure that m invoke method is on the correct thread?
Or is there a different way to change the title that would be more effective?
Thank you in advance!
I have just implemented a State Container like this and ran into the same error - but my service needs to be a singleton.
So I found an example on the aspnetcore git that does exactly what the error message says to do.
Call InvokeAsync -- not from your state container but when you try to change the state of your razor component.
https://github.com/dotnet/aspnetcore/blob/321db9d99f84cf7a67d453384292d9339de748d1/src/Components/test/testassets/BasicTestApp/DispatchingComponent.razor
So your state container doesn't need to change, just your component event handler does.
#code{
protected override void OnInitialized()
{
_YourService.OnChange += OnMyChangeHandler;
}
public void Dispose()
{
_YourService.OnChange -= OnMyChangeHandler;
}
private async void OnMyChangeHandler(object sender, EventArgs e)
{
// InvokeAsync is inherited, it syncs the call back to the render thread
await InvokeAsync(() => {
DoStuff();
StateHasChanged();
});
}
}
Now your service (if it's a singleton) can notify ALL your users at once! Think about all hoops we had to jump through in past to do this.
I posted this first thing in the morning thinking that I wouldn't have the time to look into and thinking that by time someone was able to help me out, I would have found the time to look into it more. Though I have spent a couple of days going back and forth on this already.
I finally found this article that explains that what I am trying to do is called a State Container.
What they said is that I could inject the class as a singleton, which is what I was doing or a scoped service. Turns out all I needed to do was change it to a scoped service and it works great!
no need sophisticated solution, Blazor working perfectly if you will update GUI in your event handler by
this.InvokeAsync(() => this.StateHasChanged());
it worked using await InvokeAsync(stateHasChanged); in my case

Database remote events and asynchronous programming

I have a synchronous method that calls a method which collates a bunch of data on a custom object and stores it on a table entry on a Firebird database, located on a server.
On the server, a monitoring process keeps watching the first table for new entries using a database event (a table trigger raises an event which is captured by the monitor). When this event is raised, that data is sent to a third-party black-box service to be processed with the use of a proprietary library, that takes between near-naught and 1 minute to reply.
The third-party service replies with some data which is entered on a second table on the database. This second table has another trigger that the client's program monitors. The client's program must either wait until the third-party replies some data, or it times out (the same 1 minute).
I'm currently delving into the world of database events and I've reached an impasse:
Currently I have a key press that runs a synchronous method, which according to an application setting either runs another synchronous method, which runs flawlessly, or another method that inserts an entry on a Firebird database. This database is monitored by another process, which reads that entry, do some stuff, and inserts the new data on another table.
Back on the main program, what I currently have is the method has an event handler which is triggered when the new data is inserted. However, as it is an event, the rest of the method runs its course, ending prematurely, before the event handler has the chance to read the new data.
In pseudo code:
MainWindow_KeyDown(object sender, EventArgs e)
{
if (e.Key == X)
{
MakeADecision()
}
}
MakeADecision()
{
if (Properties.Settings.Default.MySetting) Console.Write(DoLocalStuff());
else Console.Write(DoRemoteStuff());
}
string DoRemoteStuff()
{
using (OldDataTableAdapter)
using (NewDataTableAdapter)
{
OldDataTableAdapter.Insert(OldData);
var revent = new FBRemoteEvent(MyConnectionString);
revent.RemoteEventCounts += (sender, e) =>
{
NewDataTableAdapter.Fill(NewDataDataTable);
NewData = NewDataDataTable[0].MYCOLUMN;
};
revent.QueueEvents("MY_FB_EVENT");
}
return NewData;
}
As you can see, the issue here is that DoRemoteStuff reaches its return before the event can be triggered. I tried turning DoRemoteStuff() into an async method, but I don't know how to use events with async methods. Can anyone please help me with this? Any tips or hints on how to work with async methods?
A possible solution would be to use a TaskCompletionSource so you can convert your method to an async method. This is based on Is it possible to await an event instead of another async method?.
MakeADecision()
{
if (Properties.Settings.Default.MySetting)
{
Console.Write(DoLocalStuff());
}
else
{
// Consider making MakeADecision async as well
NewData = DoRemoteStuff().Result;
Console.Write(NewData);
}
}
async Task<string> DoRemoteStuff()
{
Task<string> task;
using (OldDataTableAdapter)
{
OldDataTableAdapter.Insert(OldData);
task = WaitForEvent(MyConnectionString);
}
return await task;
}
private async Task<string> WaitForEvent(string connectionString)
{
var taskCompletionSource = new TaskCompletionSource<string>();
var revent = new FbRemoteEvent(connectionString);
revent.RemoteEventCounts += (sender, e) =>
{
using (NewDataTableAdapter)
{
NewDataTableAdapter.Fill(NewDataDataTable);
string newData = NewDataDataTable[0].MYCOLUMN;
taskCompletionSource.SetResult(newData);
}
sender.Dispose();
};
revent.QueueEvents("MY_FB_EVENT");
return await taskCompletionSource.Task;
}
Some things to point out:
You need to explicitly dispose the event to avoid a memory leak
The using for NewDataTableAdapter belongs within the event handler
The MakeADecision method seems like a candidate to be made async as well
A word of warning, my C# is a bit rusty (and I have never done much with async), so I'm not sure if this is the idiomatic way of doing it. I also did not test the code as written above (I wrote and tested a simpler version, but I may have introduced bugs while transforming your code to a similar solution).
This solution may also have the possibility of a race condition between inserting the new data triggering the event and registering for the event (unless the Dispose at the end of the using block is what commits the data), consider moving the WaitForEvent before inserting. Also consider the possibility of receiving the event from an update done for/by another change.

Using event handlers inside an akka.net Actor safely

I'm trying to build a file download actor, using Akka.net. It should send messages on download completion but also report download progress.
In .NET there are classes supporting asynchronous operations using more than one event. For example WebClient.DownloadFileAsync has two events: DownloadProgressChanged and DownloadFileCompleted.
Preferably, one would use the task based async version and use the .PipeTo extension method. But, I can't see how that would work with an async method exposing two events. As is the case with WebClient.DownloadFileAsync. Even with WebClient.DownloadFileTaskAsync you still need to handle DownloadProgressChanged using an event handler.
The only way I found to use this was to hook up two event handlers upon creation of my actor. Then in the handlers, I messages to Self and the Sender. For this, I must refer to some private fields of the actor from inside the event handlers. This feels wrong to me, but I cannot see another way out.
Is there a safer way to use multiple event handlers in an Actor?
Currently, my solution looks like this (_client is a WebClient instance created in the constructor of the actor):
public void HandleStartDownload(StartDownload message)
{
_self = Self;
_downloadRequestor = Sender;
_uri = message.Uri;
_guid = message.Guid;
_tempPath = Path.GetTempFileName();
_client.DownloadFileAsync(_uri, _tempPath);
}
private void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
var completedMessage = new DownloadCompletedInternal(_guid, _tempPath);
_downloadRequestor.Tell(completedMessage);
_self.Tell(completedMessage);
}
private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
var progressedMessage = new DownloadProgressed(_guid, e.ProgressPercentage);
_downloadRequestor.Tell(progressedMessage);
_self.Tell(progressedMessage);
}
So when the download starts, some fields are set. Additionally, I make sure I Become a state where further StartDownload messages are stashed, until the DownloadCompleted message is received by Self:
public void Ready()
{
Receive<StartDownload>(message => {
HandleStartDownload(message);
Become(Downloading);
});
}
public void Downloading()
{
Receive<StartDownload>(message => {
Stash.Stash();
});
Receive<DownloadCompleted>(message => {
Become(Ready);
Stash.UnstashAll();
});
}
For reference, here's the entire Actor, but I think the important stuff is in this post directly: https://gist.github.com/AaronLenoir/4ce5480ecea580d5d283c5d08e8e71b5
I must refer to some private fields of the actor from inside the event
handlers. This feels wrong to me, but I cannot see another way out.
Is there a safer way to use multiple event handlers in an Actor?
There's nothing inherently wrong with an actor having internal state, and members that are part of that state raising events which are handled within the actor. No more wrong than this would be if taking an OO approach.
The only real concern is if that internal state gets mixed between multiple file download requests, but I think your current code is sound.
A possibly more palatable approach may be to look at the FileDownloadActor as a single use actor, fire it up, download the file, tell the result to the sender and then kill the actor. Starting up actors is a cheap operation, and this completely sidesteps the possibility of sharing the internal state between multiple download requests.
Unless of course you specifically need to queue downloads to run sequentially as your current code does - but the queue could be managed by another actor altogether and still treat the download actors as temporary.
I don't know if that is your case, but I see people treating Actors as micro services when they are simply objects. Remember Actors have internal state.
Now think about scalability, you can't scale messages to one Actor in a distributed Actor System. The messages you're sending to one Actor will be executed in the node executing that Actor.
If you want to execute download operations in parallel (for example), you do as Patrick said and create one Actor per download operation and that Actor can be executed in any available node.

How do you update ASP.NET web forms from different threads?

In the last few days I've been trying to learn how to use ASP.NET Web Forms together with multithreading the hard way by building a simple applet using both and I've been struggling with aspects of interactions between different threads and the UI.
I've resolved some multithreading issues in some other questions (and also learned after waaaaaay too long that web forms and WPF are not the same thing) but now I'm running into trouble finding the best way to update UI elements based on data acquired in multiple threads.
Here's my code:
Default.aspx
public partial class _Default : System.Web.UI.Page
{
private NlSearch _search;
private static int _counter = 0;
private static SortedList<long, SearchResult> resultsList = new SortedList<long, SearchResult>();
protected void Page_Load(object sender, EventArgs e)
{
_search = new NlSearch();
}
protected void AddSearchMethod(object sender, EventArgs e)
{
var text = SearchForm.Text;
new Task(() => MakeRequest(text));
}
protected void UpdateMethod(object sender, EventArgs e)
{
resultsLabel.Text = "";
foreach (var v in resultsList.Values)
{
resultsLabel.Text += v.SearchTerm + ": " + v.Count + " occurances<br/>";
}
}
protected void ClearSearchMethod(object sender, EventArgs e)
{
resultsLabel.Text = "";
resultsList.Clear();
}
protected void MakeRequest(string text)
{
_counter++;
SearchResult s = new SearchResult
{
SearchTerm = text,
Count = _search.MakeRequests(text)
};
resultsList.Add(_counter, s);
}
}
I've tried quite a few versions of the same basic thing. NlSearch.MakeRequest (called by MakeRequests) sends an HTTP POST request to an outside web site imitating a search bar input, and then extracts an integer from the markup indicating how many results came back.
The current simple UI revolves around a SearchForm textfield, an "Add Search" button, an "Update Label" button a "Clear Search" method, and a ResultsLabel that displays results. The AddSearch button creates a new task that calls MakeRequest, which calls the method to send the HTTP request and then stores the results in the order they were sent in a static sorted list.
So now ideally in a good UI I would like to just update the label every time a thread returns, however I've tried using ContinueWhenAll and a few other task functions and the problem seems to be that other threads do not have the ability to change the UI.
I have also tried running a new thread on page load that updates the label every few seconds, but this likewise failed.
Because I haven't been able to implement this correctly, I've had to use the "Update Label" button which literally just tells the label to display what's currently in the static list. I would really like to get rid of this button but I can't figuer out how to get my threads to make UI changes.
In general, trying to do threading in a web app is a bad idea. Web servers are designed for this, but spinning off new threads or processes should be avoided if at all possible. While there used to be a mechanism (and maybe there still is) to "push" results to a client, there are better solutions available today.
What you're describing is exactly the problem that AJAX is intended to solve.
You mentioned WPF in your question -- are you perhaps instead looking for a Windows application, like WinForms? I think that perhaps the term "web forms" has confused the situation. Web forms are just webpages with some (okay, a lot) of added in Microsoft functionality.
It sounds like you're trying to send updates to a webpage from a thread in code. The web doesn't work that way. I'd suggest reading through the ASP.NET Page Life Cycle Overview if you're actually trying to design webpages. Other answers have suggested AJAX functionality (which is where the web page executes some JavaScript that goes out and talks to a web server).
Have you ever hear about AJAX before? I think you're a thinking as application dev instead of web dev.
If you want to run your code asynchonous you may want to use the Async Await keywords instead of managing threads yourself. See information about Asynchronous Programming with Async and Await
Do not let your threads get tangled up ;)

Attempt at an Asynchronous method is failing

I have an MVC3/.NET 4 application which uses Entity Framework (4.3.1 Code First)
I have wrapped EF into a Repository/UnitOfWork pattern as described here…
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
Typically, as it explains in the article, when I require the creation of a new record I’ve been doing this…
public ActionResult Create(Course course)
{
unitOfWork.CourseRepository.Add(course);
unitOfWork.Save();
return RedirectToAction("Index");
}
However, when more than simply saving a record to a database is required I wrap the logic into what I’ve called an IService. For example…
private ICourseService courseService;
public ActionResult Create(Course course)
{
courseService.ProcessNewCourse(course);
return RedirectToAction("Index");
}
In one of my services I have something like the following…
public void ProcessNewCourse(Course course)
{
// Save the course to the database…
unitOfWork.CourseRepository.Add(course);
unitOfWork.Save();
// Generate a PDF that email some people about the new course being created, which requires more use of the unitOfWork…
var someInformation = unitOfWork.AnotherRepository.GetStuff();
var myPdfCreator = new PdfCreator();
IEnumerable<People> people = unitOfWork.PeopleRepository.GetAllThatWantNotifiying(course);
foreach(var person in people)
{
var message = “Hi ” + person.FullName;
var attachment = myPdfCreator.CreatePdf();
etc...
smtpClient.Send();
}
}
The above isn’t the actual code (my app has nothing to do with courses, I’m using view models, and I have separated the PDF creation and email message out into other classes) but the gist of what is going on is as above!
My problem is that the generation of the PDF and emailing it out is taking some time. The user just needs to know that the record has been saved to the database so I thought I would put the code below the unitOfWork.Save(); into an asynchronous method. The user can then be redirected and the server can happily take its time processing the emails, and attachments and whatever else I require it to do post save.
This is where I’m struggling.
I’ve tried a few things, the current being the following in ICourseService…
public class CourseService : ICourseService
{
private delegate void NotifyDelegate(Course course);
private NotifyDelegate notifyDelegate;
public CourseService()
{
notifyDelegate = new NotifyDelegate(this.Notify);
}
public void ProcessNewCourse(Course course)
{
// Save the course to the database…
unitOfWork.CourseRepository.Add(course);
unitOfWork.Save();
notifyDelegate.BeginInvoke(course);
}
private void Notify(Course course)
{
// All the stuff under unitOfWork.Save(); moved here.
}
}
My Questions/Problems
I’m randomly getting the error: "There is already an open DataReader associated with this Command which must be closed first." in the Notify() method.
Is it something to do with the fact that I’m trying to share the unitOrWork and therefore a dbContext across threads?
If so, can someone be kind enough to explain why this is a problem?
Should I be giving a new instance of unitOfWork to the Notify method?
Am I using the right patterns/classes to invoke the method asynchronously? Or should I be using something along the lines of....
new System.Threading.Tasks.Task(() => { Notify(course); }).Start();
I must say I've become very confused with the terms asynchronous, parallel, and concurrent!!
Any links to articles (c# async for idiots) would be appreciated!!
Many thanks.
UPDATE:
A little more digging got me to this SO page: https://stackoverflow.com/a/5491978/192999 which says...
"Be aware though that EF contexts are not thread safe, i.e. you cannot use the same context in more than one thread."
...so am I trying to achieve the impossible? Does this mean I should be creating a new IUnitOfWork instance for my new thread?
You could create a polling background thread that does the lengthy operation separately from your main flow. This thread could scan the database for new items (or items marked to process). This solution is pretty simple and ensures that jobs get done even if you application crashes (it will be picked up when the polling thread is started again).
You could also use a Synchronised Queue if it's not terrible if the request is 'lost', in the case your application crashes after the doc is requested and before it's generated/sent.
One thing is almost sure - as rikitikitik said - you will need to use a new unit of work, which means a separate transaction.
You could also look at Best threading queue example / best practice .

Categories