System.InvalidOperationException when trying to bind a CefSharp.WPF event to ViewModel - caliburn.micro

I've got a View with a CefSharp.WPF component embedded in it, and I need to be notified when it's initialized, and when it's loading etc.
Because there are no conventions for this built into Caliburn.Micro I've used cal:Message.Attach like so:
<cefsharp:ChromiumWebBrowser Name="Browser" Visibility="Visible" TabIndex="3"
WebBrowser="{Binding WebBrowser, Mode=OneWayToSource}"
Address="http://localhost:8080/"
Title="{Binding Title, Mode=OneWayToSource}"
cal:Message.Attach="[Event FrameLoadStart] = [Action OnLoading()]"/>
In my View Model I then have a simple action:
public void OnLoading()
{
System.Windows.MessageBox.Show("Loading");
}
However, when I start the application it almost immediately throws a System.InvalidOperationException because it's trying to access something on another thread.
System.InvalidOperationException was unhandled
Message: An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll
Additional information: The calling thread cannot access this object because a different thread owns it.
Is there any way around this - to make sure the action is called on the proper thread (or vice versa), or is there a better way to achieve the same effect?

Related

spring : Input page forwarding when exception like struts

Business validations are implemented by throwing CustomeException(key) such that user will be displayed error messages when something goes wrong.
I have to forward to input jsp (like struts) when business exception raised, to correct the user actions then continue with application.
we have custom HandlerExceptionResolverImpl to handle the all exceptions at once place. when exception raised then we don't know the input page.
How to do this in spring4 like struts ?
Please suggest the way how to accomplish this?
Thanks
Dhorrairaajj
base requirement is explained in this
I have solved this, all controllers should extends CustomeWebReq Class, which has getter method to get the input page like String getInputPage(String path). Controllers are responsible to return the input page based on the servlet path argument. getInputPage(string path) method will be invoked from HandlerExceptionMapping where exceptions are handling.
Thanks
Dhorrairaajj

Asynchronous action for strongly-typed partial view in _Layout

So, here's the rundown...
I've got a master layout page for my MVC 4 app that has some dynamic information and needs to be strongly typed to a specific domain entity to get that information. To keep my files cleaner, I've extracted out the typed fields to a partial view.
To grab the entity I need and map it to the partial's view-model, I have a LayoutController with an action that returns a Task<PartialViewResult>. This action uses a service layer to make an async call out to a Web API project, awaiting the entity. It massages that entity into the view-model and then returns PartialView("_LayoutPartial", viewModel).
From within the _Layout page, the partial is called via:
#{Html.RenderAction("LayoutInfo", "Layout", new { /*entity primary key*/ });}
I've stepped through the code and it does indeed get the correct entity back, but then after returning the partial view task, I get everybody's favorite Server Error page with the following error:
HttpServerUtility.Execute blocked while waiting for an asynchronous operation to complete.
I've done some Googling and SO searching and have no idea what this actually means. Am I thinking through this correctly?
That error message means that you're trying to invoke (and wait on!) an asynchronous method from within a synchronous method. In your specific case, the synchronous method is HtmlHelper.RenderAction, and the asynchronous method is your Task-returning action method. The reason the error occurs is that the point of writing an asynchronous Task-returning method is presumably to avoid blocking a thread, but RenderAction can't return until the Task is complete, so RenderAction ends up blocking while waiting for the operation to finish.
One option is to make the method that RenderAction calls synchronous instead of asynchronous, keeping in mind that this will continue to block the original thread. Another option is to populate the layout data asynchronously from within the original action method, then to pass that via ViewData to the layout page.

.Net ObjectDataSource error: Object does not match target type

I have an ObjectDataSource on a page that is producing the error "Object does not match target type" when its Insert method is invoked. From Googling this message, I believe this the message is deceptive and I'm actually getting a null reference error on the object that the ObjectDataSource is trying to invoke the method on, but I'm darned if I can figure out why.
<asp:ObjectDataSource ID="dsAddComment" runat="server"
DataObjectTypeName="BookCatalogue.InteractionDocuments.UserComment"
SelectMethod="GetNewComment" TypeName="BookCatalogue.AddCommentPresenter"
InsertMethod="AddComment" OnObjectCreating="dsAddComment_ObjectCreating" />
The Type that is being called upon Insert is an AddCommentPresenter. The method AddComment is not static. If I change this to static, I don't get the error and the method is found without problem. When it's not static, the error occurs. That's whyI htink that the underlying problem is that somehow I'm not getting a valid instance of my Presenter class when the AddComment method is invoked.
My AddCommentPresenter class doesn't have a parameterless constructor. This will cause an error normally. To get around it I'm overriding the ObjectCreating event in my page's code-behind and assigning an instance of the Presenter class.
protected void dsAddComment_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
e.ObjectInstance = presenter;
}
I can step through my ObjectCreating method and it is a valid, non-null Presenter instance that's being passed into the e.ObjectInstance property.
My AddComment method has the correct signature.
public void AddComment(UserComment newComment)
{
...
}
I've also checked the obvious things, like misspelling the type name on the aspx page, but all is correct there.
Anyone have any ideas? I must say that I find the ObjectDataSource class very difficult to work with....
A colleague found the cause of my problem. The AddCommentPresenter class in my web app was defined in the website's App_Code directory. For some reason, that was causing the error. Move it out of there, into the main directory of the website and the code worked. I can't find any mention in any ObjectDataSource documentation of this being a potential gotcha for the control, but there you go.
I've also been told that it should be possible to keep the class in the App_Code folder, but include the syntax ",__code" at the end of the TypeName. E.g.
TypeName="MyNamespace.MyType,__code"
but personally, I didn't get that working when I tried it. Another post to an ASP.Net forum suggested changing the TypeName to
TypeName="_MyNamespace.MyType"
That didn't work for me, either. I ended up just pulling the class out of the App_Code folder.

ASP.Net SqlWebEventProvider - Raising custom event - Cannot access WebErrorEvent due its protection level

I use SqlWebEventProvider to log the exceptions to sql server, and it works fine.
I also want to log custom exceptions to aspnet_WebEvent_Events table programmatically. Similar to - http://fredrik.nsquared2.com/viewpost.aspx?PostID=107&showfeedback=true
WebBaseEvent.Raise(new WebErrorEvent("My Error message", null, 5000, e));
I get an error saying "Cannot access constructor 'WebErrorEvent' here due its protection level.
Appreciate your comments...
If you wish to log custom events, you may consider creating your own event by inheriting from one of the predefined event classes found in System.Web.Management.
Here is a good reference written by Scott Mitchell. It is pretty easy to follow
https://web.archive.org/web/20211020121454/https://www.4guysfromrolla.com/articles/062707-1.aspx
The constructor for class WebErrorEvent is private. You can't instantiate an object of this class.
Take a look at Using Access Modifier private with Constructor in the article titled What are constructors in CSharp - A Step Ahead Series?.

ASP.NET control events exception handling

Suppose I have a button in an aspx page that performs a save of the form data to the database. In the associated event handler, before sending the updates, I read something from a webservice, operation that might result in an exception. In case of an error I want to have an adequate message displayed on the page, and all the data in the form preserved. How can I achieve this? Also, all my pages inherit from a basepage, so I would like, if possible to have all the error handling code in the base class. I do not want, if possible, to surround any web service call with try-catch blocks, I would in case of any unhandled exception to call some method automatically, something like Page_error, but still preserve the data in my forms.
You can easily put a method that manages the display message (maybe setting the text of some errorMessageLabel) in a superclass called from any derived class (if you wanna use inheritance to setup a template for your pages) if an exception is thrown (you can put the call to the superclas method in a catch block if there's actually an exception being thrown or you can manage this manually if the webservice is unavailable depending on your programming style).
As far as preserving the data presented, if viewstate is on and you are not populating your page dynamically then you're ok - if not, you need to explicitly save state information in viewState or session entries and retrieve them back if something goes wrong.
This bit really depends on how your page is actually implemented.

Resources