Asp. NET Service side Operation ActiveX

Source: Internet
Author: User
Tags wrapper
Everyone is only ActiveX in the Web can be declared by the object tag, and through scripting code such as JavaScript to operate, but sometimes we want to operate on the server ActiveX, especially with some complex business logic or to operate on large amounts of data.

Usually some controls can be invoked by adding a reference = COM, using Visual Studio to generate a wrapper class, but some are not, the wrapper class can be initialized, but the error is run when accessing the property or method, which is a "catastrophic failure" (Exception from hresult:0 X8000FFFF (e_unexpected)) ". At this point, if you use the form of the drag control in the WinForm can be run, of course, do not drag the control in the form to write code in the background of the new object is certainly the same error. There are a lot of questions on the network, many said that the control is not registered, there are people say Win7 reasons, some people say that the point is that the control may need a container, but the problem is how to create a container and destroy the container? WinForm can be solved by a hidden form, not in the web.

In WinForm, when you drag a COM control to the interface, you add 2 references to the spread COM control as an example, to add Fpspreadado and Axfpspreadado two DLL references (newly generated under the Debug obj folder), Where Fpspreadado is the generated wrapper class, Axfpspreadado is a derived and AxHost class, and the role of AxHost is to wrap ActiveX controls and expose them as fully functional Windows forms controls.

The first thing to do on the web is to generate this DLL reference, which can be generated by using Visual Studio to build the DLL above in the WinForm drag control, or you can use the Windows Forms ActiveX Control Importer (Aximp.exe) to generate the Then add a reference to the Web.

Then the Axfpspreadado.axfpspread cell = new Axfpspreadado.axfpspread (), and the error message is "The current thread is not in a single-threaded apartment, so the ActiveX cannot be instantiated Control, workaround 1 Sets the STAThread property for the main function (the default is STAThread), and Method 2 sets the ApartmentState for the thread. You just started using System.Threading.Thread.CurrentThread.SetApartmentState (System.Threading.ApartmentState.STA) to set, but you are prompted not to change the state Because the main thread was already started at this time, and the STA and MTA could not be changed in operation. So I took a new thread for processing <!--

Code highlighting produced by Actipro Codehighlighter (freeware)
http://www. codehighlighter.com/

--> System.Threading.Thread th = new System.Threading.Thread (new System.Threading.ParameterizedThreadStart (Workmethod));
Th. Setapartmentstate (System.Threading.ApartmentState.STA);
Th. Start ();

Finally, because the control may have to take advantage of state and information after the control operation, I have synchronized the thread as follows: <!--

Code highlighting produced by Actipro Codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> System.Threading.AutoResetEvent autoevent = new System.Threading.AutoResetEvent (false);

System.Threading.Thread th = new System.Threading.Thread (new System.Threading.ParameterizedThreadStart (Workmethod);

Th. Setapartmentstate (System.Threading.ApartmentState.STA);

Th. Start (autoevent);

Autoevent.waitone ();

What I am worried about is that because the Web itself is multiple concurrent access, there is no problem in blocking threads, and whether invoking ActiveX on the server can cause a lot of pressure on the servers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.