InfoPath FormControl on STA worker thread - infopath

I have a .NET class that exposes two public methods: one to create an InfoPath form and another to Export to one of the supported formats. I’m using the Microsoft FormControl to do this. It’s hosted by a Form that does not get displayed. I get called by a Winforms app, but on an MTA worker thread instead of the UI thread. So I create an STA thread and execute on that, which works exactly once and then results in this exception:
System.InvalidOperationException was unhandled
Message="Unable to get the window handle for the 'FormControl' control. Windowless ActiveX controls are not supported."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.AxHost.EnsureWindowPresent()
at System.Windows.Forms.AxHost.InPlaceActivate()
at System.Windows.Forms.AxHost.TransitionUpTo(Int32 state)
at System.Windows.Forms.AxHost.CreateHandle()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.AxHost.EndInit()
at InfoPathCreateStaThreadTest.FormControlHost.InitializeComponent() in C:\Windows\Temp\InfoPathCreateStaThreadTest\InfoPathCreateStaThreadTest\FormControlHost.Designer.cs:line 65
After some experimenting, I started to suspect this is a message pumping problem. I then came across this, which makes me more strongly suspect so: http://blogs.msdn.com/cbrumme/archive/2004/02/02/66219.aspx
I tried various methods of pumping messages with no luck. I should mention that the alternative of automating the InfoPath app is not viable unless I can figure out how to hide the app.
Any help would be much appreciated.

Are you creating your worker thread (the one calling the control) after the form is loaded? I was having this problem until I moved all of my calls to the FormControl within the Load event of the parent form. Since doing that I have not come across this specific error (though I have seen many others)

I'm researching the same error and found this: http://social.msdn.microsoft.com/Forums/en-US/clr/thread/9c36e5d3-a75c-4760-9070-5dbaf188992b/ which attributes the problem to DEP being unexpectedly turned on and provides a fix. I don't know yet if this will solve my problem.

Thanks for your reply. Sorry for the long delayed reply on my end. DEP is not causing the problem for me. I ended up automating the InfoPath app vs. using the FormControl. Best of luck getting your issue resolved!
Rob

Related

System.Web.UI.Control.LoadRecursive() - Insufficient stack to continue executing the program safely.

I have an ASP.NET web application developed in C# and .Net v4. When you login in and leave for the page for some 15-20 minutes, I get the "Insufficient stack to continue executing the program safely."
According to my knowledge, there is no way you can try-catch the stackoverflow in .Net. I would imagine that recursive loading of web page and active session checking leading to this error (maybe a bad coding practice). I have also added the screenshot as i am able to figure it out from Stack trace. If anyone help me in this,it will be helpful.
I think I have found the solution. Still the test. Check this Link and this
they really looks promising. I will keep you guys updated when I succeed..
Thank you all for your help.
The problem is this callstack is right at the end of the stack.
A few things to look for:
Is there anywhere in your code where you are using .FindControl(string id)?
If so, is it locating a control and then trying to locate it further?
Are you dynamically creating controls, like for <asp:Placeholder/> controls.
It can be recursive when you reference the parent from within a nested control, which then gets re-referenced by the parent.

Is there an ObserveOnDispatcher for Windows Store Apps?

I'm using reactive extensions (well, trying, learning) in a windows store app. I have a series of async operations that will eventually navigate to an authorization web page for RTM. Given the reading I've done, I would expect to write something like the following:
var result = rtm
.GetFrob()
.Select(s => rtm.GetAuthenticationUrl(s))
.ObserveOnDispatcher()
.Do(uri => AuthWebView.Navigate(new Uri(uri)))
.First();
However, I can't seem to find a method ObserveOnDispatcher, and further there is no Scheduler.Dispatcher property either, leading me to think on a Windows Store application there is something deeper gong on with the main UI thread and how you reference it.
GetFrob, btw, creates an IObservable sequence using Observable.FromAsync (if that helps with this).
BTW, if I remove that line, I fail in the call to the Navigate as posting it from the wrong thread. So, what is the proper thing to do here to make sure I'm back on the correct thread?
BTW, Excuse the ".Do" operation here, I know it isn't "nice", however, I'm still experimenting in throw-away code.
Many thanks in advance!
Argh. Apparently I just am not good enough at search. :(
Turns out that adding the nuget package Rx-Xaml includes the proper assemblies for the scheduler, etc. Details can be found on the main Rx blog. Of course, my app is back to hanging now, but that may be due to something else dumb on my part.

Easy way to implement universal error handling in ASP.Net application

Folks,
I have an application that is primarily one page with a lot of user controls and custom controls. ASP.Net WebParts are being used. There is almost no code in the web page and its master page: it's all in various components.
The design requirements include an error message display panel. The client would like the app to display any errors occurring in any part of the app on the error message display rather than going to a custom error page. (This was a recent decision: when we started we were thinking custom error page.)
As far as I can tell, WebParts don't provide any default error handling (no capture of errors and displaying of messages) and user controls don't necessarily fire their OnError event when an internal error occurs.
So I'm looking at wrapping absolutely everything in Try ... Catch blocks, at least at the presentation level, and doing the handling. So my question is: is there any easy way to implement a kind of global, or at least class-wide, error handler, that will handle an error in the way I choose, interrupt the routine that was executing, but return (after handling) to the caller of the method that failed?
I'm willing to adapt code to verify that subroutines did what they were supposed to and returned appropriate values (ex: non-null data objects.) What I don't want is for the whole process to be aborted up to the very top, with rendering skipped, or (of course) the famous yellow and white screen.
I guess this is a case for aspect-oriented programming, but I don't think ASP.Net provides anything like that yet. I didn't get the impression that EntLib 5.0 did, either.
I would adore it if it were possible to decorate a method with an attribute that meant "On error call such-and-such a static method of such-and-such a class". But I doubt it is, yet.
Any suggestions are welcome.
Thanks,
Ann L.
For starters, it's bad practice to put Try Catch handlers around every piece of code in your application. There is a slight performance hit every time you set that up, and this could slow things down really fast.
In your Global.asax.cs (or .vb) file, there is a method in there called Application_Error which is the global error handler. This will catch any error you haven't previously caught (or thrown up) within the application.
You could easily put your error handling code here. It could be as simple as dumping the error message to a log for future review, or check for the exception type (SQL vs. File vs. HTTP) and do the error handling task related to the type.
You need to take a look at elmah. If you google 'elmah' it has an error module/handler that does pretty much what you are looking for (although it will take some extra tweaking..)
You might want to consider letting your errors 'bubble up' though.. There are some serious performance issues with writing a bunch of Try..Catch..Finally blocks...

ASP.NET Long Process and Sending updates to Browser

I've asked this before but I was hoping for another answer and perhaps some code samples because I've been having a difficult time with this. I have an asp.net page. The user hits the "Run" button and I have code IN AN ASSEMBLY, not in the APP_CODE folder that is called and runs a long process that moves product info from a file into the database. While the user waits, I would like them to see status updates like what product the import process in on and status info. I'm assuming I'd break off into another thread and use Ajax but I have no idea how to do this. Some code samples would be very helpful, thanks.
A simpler way to do this without needing to go into multi threading (which can cause all sorts of nasty, hard to track down bugs) is to use AsyncResults in .NET and AJAX which allow you to query a process.
A good example to start you off can be found here.
found it by using HttpResponse.Flush

Using Web Services in the Flex Mate Framework

I am currently trying to use the "Invocation tags" of Mate to call my web services and delegate the WS-responses to my fault/result handlers.
I want to use the generated proxies, provided by the Flex Builder, and not the plain <WebService> or <WebServiceInvoker> tags.
I actually failed using several techniques:
<WebServiceInvoker> does not work with the generated proxies.
<AsyncMethodInvoker> needs some complicated successType that I could not get to work with the WS-calls. And defining the events seems redundant to me. I want it simple and easy to read, the code will be touched by other people than me!
<MethodInvoker> can't use instances, and it also can't handle the proxies' AsyncToken
<DelegateInvoker> Looked fine at first. It calls the service but doesn't fire valid result events (infinite busy cursor). Even though i can successfully bind to the XYZ_lastResult of the WS-proxies, and a WS-call results in getting valid data from the WS-backend, the <faultHandlers> and <resulthandlers> are not executed. There is some solution for the DelegateInvoker that changes code in the generated proxies, which i definately do not want to do!
So here is my question: Is there a simple(!) way of using default Flexbuilder generated proxies with the Mate Invocation tags?
It appears that your request is not that uncommon to Mate. Check out this couple of threads in their forum:
http://mate.asfusion.com/forums/topic.php?id=424
http://mate.asfusion.com/forums/topic.php?id=421
The solution is to modify some bits of the auto-generated code... which in a way ruins the whole point of using code generation.

Resources