0

I try to add the WMPLib nuget package as a package reference to my project, but in the code the classes could not be found.

This is in my csproj file:

<PackageReference Include="WMPLib">
  <Version>1.0.0</Version>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
  <PrivateAssets>all</PrivateAssets>
</PackageReference>

And in my cs file I have:

using WMPLib;

And this is the error:

error CS0246: The type or namespace name 'WMPLib' could not be found (are you missing a using directive or an assembly reference?)

Here I read that "the issue is due to the new NuGet format reference 'PackageReference' ... the folders and files aren't added".

Does someone know more about this? And is the solution using a package.config instead of a package reference?

ffonz
  • 1,304
  • 1
  • 11
  • 29
  • What do you mean by _but in the code the classes are not recognized_? Can you please add some sample code and an error message? – mu88 Aug 17 '22 at 06:54
  • You are right. Although it is the most simple code you could imagine, it makes the question more clear. – ffonz Aug 17 '22 at 07:22

1 Answers1

0

Okay, I was able to reproduce your problem and also found some kind of workaround.

Problem

It seems to me that the original WMPLib NuGet package is somehow broken/not really compliant to nowadays NuGet's expectations. I think so because of the following two indicators:

  • Normally nuget.org shows all the compatible target frameworks of a package, but for WMPLib, it shows only .NET Framework (see here) - but which version? Compare it e. g. with Newtonsoft.Json.
  • When analyzing the package with either dotPeek or 7-Zip, I found the assemblies inside the lib folder. But usually, they are inside lib\<<TFM>>, e. g. lib\net48.

Workaround

I've extracted the two assemblies AxInterop.WMPLib.dll and Interop.WMPLib.dll using dotPeek and saved them in C:\temp\NewWMPLib\lib\net35.
Then I've created the following file C:\temp\NewWMPLib.nuspec:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
        <id>NewWMPLib</id>
        <version>1.0.0</version>
        <authors>me</authors>
        <owners>me</owners>
        <description>Lorem ipsum</description>
    </metadata>
</package>

Finally I build a new NuGet package via nuget pack C:\temp\NewWMPLib.nuspec.

When consuming this package via NuGet, the following code now compiles:

using WMPLib;

namespace ConsoleApp4
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var windowsMediaPlayerClass = new WindowsMediaPlayerClass();
        }
    }
}

Disclaimer

I'm not a lawyer and I have no clue whether following my workaround is legally safe.

mu88
  • 4,156
  • 1
  • 23
  • 47
  • 1
    Thanks for your answer. I think you're right that the nuget package is not compliant for using with PackageReferences. I don't know either if your solution is legally safe or not. Since I will be spreading my software publicly, I will use another approach. I now use an old fashioned packages.config file and that solved my problem as well. – ffonz Aug 17 '22 at 12:31