gmanny / Pechkin

.NET Wrapper for WkHtmlToPdf static DLL. Allows you to utilize full power of the library.
401 stars 128 forks source link

Cannot put libraray in the GAC #27

Open ChrisNetMatch opened 11 years ago

ChrisNetMatch commented 11 years ago

In the project I'm currently working on, the library (and any library) is stored in the GAC which has a couple of down-sides:

For the first issue I've signed the library with a company certificate, however it would be nice to have a signed version in NuGet as well. For the second issue, I have made the dlls embedded resources, then I can automatically 'deploy' them as soon as I start using the library. For now I used this code in the PechkinBindings class:

static PechkinBindings()
{
    // Get the Pechkin assembly.
    Assembly assembly = Assembly.GetExecutingAssembly();

    // Determine the target path for the dlls.
    string targetPath = Path.GetDirectoryName(assembly.GetName().CodeBase.Replace("file:///", ""));

    // Make sure the target directory exists.
    if (!Directory.Exists(targetPath))
        Directory.CreateDirectory(targetPath);

    // Write all the dlls.
    string[] resourceNames = assembly.GetManifestResourceNames();
    IEnumerable<string> dllResourceNames = resourceNames
    .Where(name => name.EndsWith(".dll"));
    foreach (string dllResourceName in dllResourceNames)
    {
        string fileName = Path.Combine(targetPath, dllResourceName.Substring(dllResourceName.LastIndexOf("/") + "/Pechkin.".Length));
        if (!File.Exists(fileName))
        {
            using (Stream inputStream = assembly.GetManifestResourceStream(dllResourceName))
            using (FileStream outputStream = File.Open(fileName, FileMode.Create))
            {
                byte[] buffer = new byte[8 * 1024];
                int len;
                while ((len = inputStream.Read(buffer, 0, buffer.Length)) > 0)
                    outputStream.Write(buffer, 0, len);
            }
        }
    }
}
gmanny commented 11 years ago

Thank you for the suggestions, I'll do it, when I have time.