简体   繁体   中英

How to create a threadsafe c# exe wrapper

I am attempting to convert HTML to PDF.

I have checked out PDFSharp and ITextSharp which both don't look too complicated, but there is an executable out there wkhtmltopdf which seems to by far have the best reviews.

I would like to use the wkhtmltopdf console application as a library.

I can imagine this situation has occurred in different formats but can't find any solutions specific enough to use.

There is an existing C# wkhtmltopdf wrapper library, but it has problems in deploying and the application hanging.

There is only one call that I need to pass this .exe file which is something like:

wkhtmltopdf.exe "www.adsf.com" convertedPdfOfAsdf.pdf

I would like to create a library that spins off a thread that:

  1. Copies an instance of wkhtmltopdf.exe to a temp location
  2. Calls wkhtmltopdf.exe via reflection
  3. Deletes the temp location containing the copies wkhtmltopdf.exe

I haven't attempted anything like this before so not sure if this is the best way of solving the problem, but surely there is a reproducable solution out there.

PS. My solution MUST be thread safe. It will be running as a web application.

It sounds like you're headed towards DLL hell. While your proposed solution would provide separation between instances, it can very quickly force you into a situation where you're having to deal with Unable to cast X to X exceptions.

I'd recommend locking access to the underlying library, and forcing your application to wait, OR look into operating in multiple AppDomains executing the same code linearly.

For one site I use ConcurrentQueue , which is a threadsafe datastructure, to make it simpler to add to a queue while my consumer pops off the next thing to process.

You can see an example in the question here:

Lock Free Concurrent Queue

So just have the upload return back a unique id to the javascript.

The javascript then queries the webserver to see if the file was processed, and until then it will let the user know the status (how many records may be ahead, for example).

Depending on how the site is made, which technologies, this may be a simpler approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM