Example #1
0
        /// <summary>
        /// Creates and initializes a private AppDomain and therein loads and initializes the
        /// wkhtmltopdf library. Attaches to the current AppDomain's DomainUnload event in IIS environments
        /// to ensure that on re-deploy, the library is freed so the new AppDomain will be able to use it.
        /// </summary>
        private static void SetupAppDomain()
        {
            var dirName = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var setup   = new AppDomainSetup()
            {
                ApplicationBase = dirName
            };

            Factory.operatingDomain = AppDomain.CreateDomain("pechkin_internal_domain", null, setup);

            Func <object> del = () =>
            {
                Factory.operatingDomain.SetData("useX11Graphics", Factory.UseX11Graphics);

                Factory.operatingDomain.DoCallBack(() =>
                {
                    PechkinBindings.wkhtmltopdf_init((bool)AppDomain.CurrentDomain.GetData("useX11Graphics") ? 1 : 0);
                });

                return(null);
            };

            SynchronizedDispatcher.Invoke(del);

            if (AppDomain.CurrentDomain.IsDefaultAppDomain() == false)
            {
                AppDomain.CurrentDomain.DomainUnload += Factory.TearDownAppDomain;
            }
        }
Example #2
0
        /// <summary>
        /// Unloads the private AppDomain and the wkhtmltopdf library, and if applicable, destroys
        /// the synchronization thread.
        /// </summary>
        /// <param name="sender">Typically a null value, not used in the method.</param>
        /// <param name="e">Typically EventArgs.Empty, not used in the method.</param>
        private static void TearDownAppDomain(object sender, EventArgs e)
        {
            if (Factory.operatingDomain != null)
            {
                Func <object> del = () =>
                {
                    Factory.operatingDomain.DoCallBack(() => PechkinBindings.wkhtmltopdf_deinit());

                    return(null);
                };

                SynchronizedDispatcher.Invoke(del);

                AppDomain.Unload(Factory.operatingDomain);

                foreach (ProcessModule module in Process.GetCurrentProcess().Modules)
                {
                    if (module.ModuleName == "wkhtmltox.dll")
                    {
                        while (WinApiHelper.FreeLibrary(module.BaseAddress))
                        {
                        }
                    }
                }

                Factory.operatingDomain = null;
            }

            SynchronizedDispatcher.Terminate();
        }
Example #3
0
 public byte[] Convert(Uri url)
 {
     return(SynchronizedDispatcher.Invoke(() => this.remoteInstance.Convert(url)));
 }
Example #4
0
 public byte[] Convert(string html)
 {
     return(SynchronizedDispatcher.Invoke(() => this.remoteInstance.Convert(html)));
 }
Example #5
0
 public byte[] Convert(ObjectSettings settings)
 {
     return(SynchronizedDispatcher.Invoke(() => this.remoteInstance.Convert(settings)));
 }
Example #6
0
 public byte[] Convert(HtmlToPdfDocument document)
 {
     return(SynchronizedDispatcher.Invoke(() => this.remoteInstance.Convert(document)));
 }