Corey-M/NAudio.Lame

Issue with NAudio.Lame package in my asp.net core 3.1 project but got

Closed this issue · 3 comments

I have installed NAudio.Lame package into my asp.net core 3.1 (Web Project) but after running I got error

DllNotFoundException: Unable to load DLL 'libmp3lame.64.dll'

I checked and see libraries are in the folder

\bin\Debug\netcoreapp3.1:

  • NAudio.Lame.dll
  • NAudio.dll
  • libmp3lame.32.dll
  • libmp3lame.64.dll

I'm using NAudio.Lame 1.1.2 version

I also tried to call code (as recomended ) but it didn't help.

// find path to 'bin' folder
var binPath = Path.Combine(new string[] { AppDomain.CurrentDomain.BaseDirectory, "bin" });
// get current search path from environment
var path = Environment.GetEnvironmentVariable("PATH") ?? "";

        // add 'bin' folder to search path if not already present
        if (!path.Split(Path.PathSeparator).Contains(binPath, StringComparer.CurrentCultureIgnoreCase))
        {
            path = string.Join(Path.PathSeparator.ToString(), new string[] { path, binPath });
            Environment.SetEnvironmentVariable("PATH", path);
        }

But It's working if I'm running console application in the .net core 3.1

Any suggestion how to get it worked?

I haven't tried this with ASP.NET Core before. It does a lot of things different to ASP.NET so that solution - which is a bit of a hack really - is probably not going to work. .NET Core handles native DLL loading through some classes that are not part of .NET Standard so the solution isn't going to be part of the library.

After a bit of play it seems that AppDomain.CurrentDomain.BaseDirectory already points to the bin folder, so you don't need to add that to it. Try adding just that to the path (without appending the bin folder to it) and see if it works.

Alternatively you can manually locate the file via the file system, doing a search for the DLL starting at IWebHostEnvironment.ContentRootPath in the constructor of the page (for Razor Pages) or Controller that uses NAudio.Lame. For Razor Pages that looks like this:

public class IndexModel : PageModel
{
    private static IntPtr hLameNative = IntPtr.Zero;;
    
    public IndexModel(IWebHostEnvironment hostEnvironment)
    {
        if (hLameModule == IntPtr.Zero)
        {
            var dllInfo = new DirectoryInfo(hostEnvironment.ContentRootPath)
                .EnumerateFiles("libmp3lame.64.dll", SearchOption.AllDirectories)
                .FirstorDefault();
            
            if (dllInfo.Exists)
                System.RuntimeInteropServices.NativeLibrary.TryLoad(dllInfo.FullName, out hLameModule);
            else
                hLameModule = IntPtr.Add(IntPtr.Zero, 1);
        }
    }
    
    public void OnGet()
    {
        // use NAudio.Lame types here.
    }
}

Or move that to a static class, or whatever. Maybe check for 32-bit and load the appropriate DLL here too.

@NGancevich

I've added a new native library load mechanism that will work automatically for some cases and can be invoked with a set of search path. Check out v1.1.3 and see if it fixes the issue for you.

Housekeeping time, closing this one off as inactive. If the new changes don't do what you need then send me a new issue and I'll look into it.