Search code examples
nuget-packageazure-cognitive-services

Unable to load DLL "Microsoft.CognitiveServices.Speech.core.dll", could not be found


I have a console application that I made in Visual Studio 2022, using C#, .NET Framework 4.8.1, and the Azure Cognitive Services Speech SDK (version 1.25) installed through NuGet. The application works fine in the development environment, but when I publish it and then install it on either the development computer, or a target computer, the following error message appears:

Unhandled exception: System.DllNotFoundException: Unable to load DLL 'Microsoft.CognitiveServices.Speech.core.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
at Microsoft.CognitiveServices.Speech.Internal.SpeechConfig.speech_config_from_subscription(IntPtr& config, String subscriptionKey, String region)
at Microsoft.CognitiveServices.Speech.SpeechConfig.FromSubscription(String, subscriptionKey, String region)
at CommandApp.Program.Main()

To be clear, both the development computer (Windows 11) and target computer (Windows 10) are using the same CPU architecture (64-bit). The target architecture is set to 'Any CPU'. I have also installed the Microsoft Visual C++ Redistributable (x64) (version 14.34.31938) on both machines, which I believe is used by the SDK.

To try to resolve the issue, I have tried the following:

  • Uninstalling and reinstalling the Microsoft.CognitiveServices.Speech package on my development computer, republishing, and then reinstalling on the computers.
  • Copying the 'Microsoft.CognitiveServices.Speech.core.dll' file directly from the NuGet package directory, and placing the copy in the same folder as the application file.
  • Adding a reference to the file using the Reference Manager, which causes another error to appear: 'A reference to C:\Users\trist\source\repos\ConsoleApp1\bin\Release\Microsoft.CognitiveServices.Speech.core.dll' could not be loaded. Please make sure that the file is accessible, and that it is a valid assembly or COM component.
  • Checking the Application Files section under Publish settings to see if the file was there (it isn't, but there was a similarly named file, Microsoft.CognitiveServices.Speech.csharp.dll, which is included).
  • Repairing the C++ redistributable installation.
  • Changing the solution and project configurations from 'AnyCPU' to 'x64', and re-publishing.

The advice to use a NuGet package was taken from here.

So, not quite sure what I am missing, here. Any help would be appreciated, even from individuals who have had publishing problems using other NuGet packages, and were then able to resolve them.

Thanks in advance.


Solution

  • You can configure your .NET Framework project to include the native dlls from the Speech SDK Nuget package by adding following changes to your application .csproj file:

    <PropertyGroup>
      <NuGetPackageRoot>$(USERPROFILE)\.nuget\packages</NuGetPackageRoot>
    </PropertyGroup>
    <ItemGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
      <Content Include="$(NuGetPackageRoot)\microsoft.cognitiveservices.speech\1.25.0\runtimes\win-x64\native\Microsoft.CognitiveServices.Speech.core.dll">
      </Content>
      <Content Include="$(NuGetPackageRoot)\microsoft.cognitiveservices.speech\1.25.0\runtimes\win-x64\native\Microsoft.CognitiveServices.Speech.extension.audio.sys.dll">
      </Content>
    </ItemGroup>
    

    Here it is assumed that Speech SDK gets installed under $(USERPROFILE)\.nuget\packages folder. Also note that Speech SDK Nuget contains more dlls than given in the example above. You can include all of them, however for most of the scenarios the example above is sufficient.

    Also seems that this problem does not happen with .NET Core applications, where current NuGet installation is sufficient for app publish. I will see if we will update the Speech SDK NuGet to support better the .NET Framework scenario.